Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring clean up, remove use of deprecated methods #2138

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public boolean isWrapperType(Type type) {

@Override
public boolean isAsyncResponse(final MethodInfo method) {
// TODO: Implement this.
return false;
}

Expand Down Expand Up @@ -89,13 +88,13 @@ public boolean isScannerInternalResponse(Type returnType) {

@Override
public boolean isMultipartOutput(Type returnType) {
// TODO: Check this
// XXX: Check this
return SpringConstants.MULTIPART_OUTPUTS.contains(returnType.name());
}

@Override
public boolean isMultipartInput(Type inputType) {
// TODO: Check this
// XXX: Check this
return SpringConstants.MULTIPART_INPUTS.contains(inputType.name());
}

Expand All @@ -105,6 +104,7 @@ public boolean isFrameworkContextType(Type type) {
}

@Override
@SuppressWarnings("deprecation")
public boolean containsScannerAnnotations(List<AnnotationInstance> instances,
List<AnnotationScannerExtension> extensions) {
for (AnnotationInstance instance : instances) {
Expand Down Expand Up @@ -144,7 +144,7 @@ private boolean hasRequestMappingMethod(final MethodInfo method, final String re

/**
* Find and process all Spring Controllers
* TODO: Also support org.springframework.stereotype.Controller annotations ?
* Future: Also support org.springframework.stereotype.Controller annotations ?
*
* @param context the scanning context
* @param openApi the openAPI model
Expand Down Expand Up @@ -279,11 +279,11 @@ private void processControllerMethod(final ClassInfo resourceClass,
// Figure out the current @Produces and @Consumes (if any)
String[] defaultConsumes = getDefaultConsumes(context, method, getResourceParameters(resourceClass, method));
context.setDefaultConsumes(defaultConsumes);
context.setCurrentConsumes(getMediaTypes(method, SpringConstants.MAPPING_CONSUMES, defaultConsumes).orElse(null));
context.setCurrentConsumes(getMediaTypes(method, SpringConstants.MAPPING_CONSUMES).orElse(null));

String[] defaultProduces = getDefaultProduces(context, method);
context.setDefaultProduces(defaultProduces);
context.setCurrentProduces(getMediaTypes(method, SpringConstants.MAPPING_PRODUCES, defaultProduces).orElse(null));
context.setCurrentProduces(getMediaTypes(method, SpringConstants.MAPPING_PRODUCES).orElse(null));

// Process any @Operation annotation
Optional<Operation> maybeOperation = processOperation(context, resourceClass, method);
Expand Down Expand Up @@ -368,11 +368,10 @@ private ResourceParameters getResourceParameters(final ClassInfo resourceClass,
final MethodInfo method) {
Function<AnnotationInstance, Parameter> reader = t -> context.io().parameterIO().read(t);
return SpringParameterProcessor.process(context, currentAppPath, resourceClass,
method, reader,
context.getExtensions());
method, reader);
}

Optional<String[]> getMediaTypes(MethodInfo resourceMethod, String property, String[] defaultValue) {
Optional<String[]> getMediaTypes(MethodInfo resourceMethod, String property) {
Set<DotName> annotationNames = new HashSet<>(SpringConstants.HTTP_METHODS);
annotationNames.add(SpringConstants.REQUEST_MAPPING);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package io.smallrye.openapi.spring;

import static java.lang.invoke.MethodHandles.lookup;

import org.jboss.logging.Logger;
import org.jboss.logging.annotations.LogMessage;
import org.jboss.logging.annotations.Message;
import org.jboss.logging.annotations.MessageLogger;

@MessageLogger(projectCode = "SROAP", length = 5)
interface SpringLogging {
SpringLogging log = Logger.getMessageLogger(SpringLogging.class, SpringLogging.class.getPackage().getName());
SpringLogging log = Logger.getMessageLogger(lookup(), SpringLogging.class, SpringLogging.class.getPackage().getName());

@LogMessage(level = Logger.Level.WARN)
@Message(id = 11000, value = "Ignoring %s annotation that is not on a class")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.jboss.jandex.Type;

import io.smallrye.openapi.runtime.io.Names;
import io.smallrye.openapi.runtime.scanner.AnnotationScannerExtension;
import io.smallrye.openapi.runtime.scanner.ResourceParameters;
import io.smallrye.openapi.runtime.scanner.dataobject.TypeResolver;
import io.smallrye.openapi.runtime.scanner.spi.AbstractParameterProcessor;
Expand All @@ -45,9 +44,8 @@ public class SpringParameterProcessor extends AbstractParameterProcessor {

private SpringParameterProcessor(AnnotationScannerContext scannerContext,
String contextPath,
Function<AnnotationInstance, Parameter> reader,
List<AnnotationScannerExtension> extensions) {
super(scannerContext, contextPath, reader, extensions);
Function<AnnotationInstance, Parameter> reader) {
super(scannerContext, contextPath, reader, scannerContext.getExtensions());
}

/**
Expand All @@ -63,18 +61,16 @@ private SpringParameterProcessor(AnnotationScannerContext scannerContext,
* Spring HTTP annotations
* @param reader callback method for a function producing {@link Parameter} from a
* {@link org.eclipse.microprofile.openapi.annotations.parameters.Parameter}
* @param extensions scanner extensions
* @return scanned parameters and modified path contained in a {@link ResourceParameters}
* object
*/
public static ResourceParameters process(AnnotationScannerContext context,
String contextPath,
ClassInfo resourceClass,
MethodInfo resourceMethod,
Function<AnnotationInstance, Parameter> reader,
List<AnnotationScannerExtension> extensions) {
Function<AnnotationInstance, Parameter> reader) {

SpringParameterProcessor processor = new SpringParameterProcessor(context, contextPath, reader, extensions);
SpringParameterProcessor processor = new SpringParameterProcessor(context, contextPath, reader);
return processor.process(resourceClass, resourceMethod);
}

Expand All @@ -99,53 +95,51 @@ protected void readAnnotatedType(AnnotationInstance annotation, AnnotationInstan
FrameworkParameter frameworkParam = SpringParameter.forName(name);

if (frameworkParam != null) {
AnnotationTarget target = annotation.target();
Type targetType = getType(target);

if (frameworkParam.style == Style.FORM) {
// Store the @FormParam for later processing
formParams.put(paramName(annotation), annotation);
readFrameworkParameter(annotation, frameworkParam, overriddenParametersOnly);
} else if (frameworkParam.style == Style.MATRIX) {
// Store the @MatrixParam for later processing
List<String> pathSegments = beanParamAnnotation != null
? lastPathSegmentsOf(beanParamAnnotation.target())
: lastPathSegmentsOf(target);

for (String pathSegment : pathSegments) {
matrixParams
.computeIfAbsent(pathSegment, k -> new HashMap<>())
.put(paramName(annotation), annotation);
}

// Do this in Spring ?
//}else if (frameworkParam.location == In.PATH && targetType != null
// && SpringConstants.REQUEST_MAPPING.equals(targetType.name())) {
// String pathSegment = JandexUtil.value(annotation, ParameterConstant.PROP_VALUE);

// if (!matrixParams.containsKey(pathSegment)) {
// matrixParams.put(pathSegment, new HashMap<>());
// }
} else if (frameworkParam.location != null) {
readFrameworkParameter(annotation, frameworkParam, overriddenParametersOnly);
} else if (target != null && annotatesHttpGET(target)) {
// This is a SpringDoc @ParameterObject
setMediaType(frameworkParam);
targetType = TypeUtil.unwrapType(targetType);

if (targetType != null) {
ClassInfo beanParam = index.getClassByName(targetType.name());

/*
* Since the properties of the bean are probably not annotated (supported in Spring),
* here we process them with a generated Spring @RequestParam annotation attached.
*/
for (var entry : TypeResolver.getAllFields(scannerContext, targetType, beanParam, null).entrySet()) {
var syntheticQuery = AnnotationInstance.builder(SpringConstants.QUERY_PARAM)
.buildWithTarget(entry.getValue().getAnnotationTarget());
readAnnotatedType(syntheticQuery, beanParamAnnotation, overriddenParametersOnly);
}
}
readSpringParameter(frameworkParam, annotation, beanParamAnnotation, overriddenParametersOnly);
}
}
}

private void readSpringParameter(FrameworkParameter frameworkParam,
AnnotationInstance annotation,
AnnotationInstance beanParamAnnotation,
boolean overriddenParametersOnly) {
AnnotationTarget target = annotation.target();
Type targetType = getType(target);

if (frameworkParam.style == Style.FORM) {
// Store the @FormParam for later processing
formParams.put(paramName(annotation), annotation);
readFrameworkParameter(annotation, frameworkParam, overriddenParametersOnly);
} else if (frameworkParam.style == Style.MATRIX) {
// Store the @MatrixParam for later processing
List<String> pathSegments = beanParamAnnotation != null
? lastPathSegmentsOf(beanParamAnnotation.target())
: lastPathSegmentsOf(target);

for (String pathSegment : pathSegments) {
matrixParams
.computeIfAbsent(pathSegment, k -> new HashMap<>())
.put(paramName(annotation), annotation);
}
} else if (frameworkParam.location != null) {
readFrameworkParameter(annotation, frameworkParam, overriddenParametersOnly);
} else if (target != null && annotatesHttpGET(target)) {
// This is a SpringDoc @ParameterObject
setMediaType(frameworkParam);
targetType = TypeUtil.unwrapType(targetType);

if (targetType != null) {
ClassInfo beanParam = index.getClassByName(targetType.name());

/*
* Since the properties of the bean are probably not annotated (supported in Spring),
* here we process them with a generated Spring @RequestParam annotation attached.
*/
for (var entry : TypeResolver.getAllFields(scannerContext, targetType, beanParam, null).entrySet()) {
var syntheticQuery = AnnotationInstance.builder(SpringConstants.QUERY_PARAM)
.buildWithTarget(entry.getValue().getAnnotationTarget());
readAnnotatedType(syntheticQuery, beanParamAnnotation, overriddenParametersOnly);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;

import org.eclipse.microprofile.openapi.models.OpenAPI;
import org.eclipse.microprofile.openapi.models.media.Schema;
import org.jboss.jandex.Index;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -43,10 +39,8 @@ public ResponseEntity<?> getD2() {
}

@Test
void testDeprecatedClassSetsOperationsDeprecated() throws IOException, JSONException {
Index index = Index.of(DeprecatedResource.class);
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index);
OpenAPI result = scanner.scan();
void testDeprecatedClassSetsOperationsDeprecated() {
OpenAPI result = scan(DeprecatedResource.class); // NOSONAR
assertTrue(result.getPaths().getPathItem("/deprecated/d1").getGET().getDeprecated());
assertTrue(result.getPaths().getPathItem("/deprecated/d2").getGET().getDeprecated());
}
Expand All @@ -71,10 +65,8 @@ public ResponseEntity<?> getD1() {
}

@Test
void testDeprecatedMethodSetsOperationsDeprecated() throws IOException, JSONException {
Index index = Index.of(MixedDeprecationResource.class);
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index);
OpenAPI result = scanner.scan();
void testDeprecatedMethodSetsOperationsDeprecated() {
OpenAPI result = scan(MixedDeprecationResource.class);
assertNull(result.getPaths().getPathItem("/mixed/m1").getGET().getDeprecated());
assertTrue(result.getPaths().getPathItem("/mixed/d1").getGET().getDeprecated());
}
Expand All @@ -98,11 +90,8 @@ public ResponseEntity<?> getO3(@MatrixVariable("p1") @Deprecated String p1, @Mat
}

@Test
void testDeprecatedParametersSetDeprecated() throws IOException, JSONException {
Index index = Index.of(DeprecatedParamResource.class);
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index);
OpenAPI result = scanner.scan();
printToConsole(result);
void testDeprecatedParametersSetDeprecated() {
OpenAPI result = scan(DeprecatedParamResource.class);

assertTrue(result.getPaths().getPathItem("/params/o1").getGET().getParameters().get(0).getDeprecated());
assertNull(result.getPaths().getPathItem("/params/o1").getGET().getParameters().get(1).getDeprecated());
Expand Down
Loading
Loading