diff --git a/core/src/main/java/io/smallrye/openapi/runtime/scanner/spi/AbstractAnnotationScanner.java b/core/src/main/java/io/smallrye/openapi/runtime/scanner/spi/AbstractAnnotationScanner.java index d196f91cf..c41b4873f 100644 --- a/core/src/main/java/io/smallrye/openapi/runtime/scanner/spi/AbstractAnnotationScanner.java +++ b/core/src/main/java/io/smallrye/openapi/runtime/scanner/spi/AbstractAnnotationScanner.java @@ -6,6 +6,9 @@ import java.util.Set; import org.eclipse.microprofile.openapi.models.Extensible; +import org.jboss.jandex.DotName; +import org.jboss.jandex.MethodInfo; +import org.jboss.jandex.Type; import io.smallrye.openapi.api.OpenApiConfig; import io.smallrye.openapi.api.constants.OpenApiConstants; @@ -99,4 +102,20 @@ private static boolean profileIncluded(OpenApiConfig config, Set profile return config.getScanProfiles().stream().anyMatch(profiles::contains); } + + protected String[] getDefaultConsumes(AnnotationScannerContext context, MethodInfo methodInfo) { + if (methodInfo.returnType().kind().equals(Type.Kind.PRIMITIVE) + || methodInfo.returnType().name().equals(DotName.createSimple(String.class))) { + return OpenApiConstants.DEFAULT_MEDIA_TYPES.get(); + } + return context.getConfig().getDefaultConsumes().orElseGet(OpenApiConstants.DEFAULT_MEDIA_TYPES); + } + + protected String[] getDefaultProduces(AnnotationScannerContext context, MethodInfo methodInfo) { + if (methodInfo.returnType().kind().equals(Type.Kind.PRIMITIVE) + || methodInfo.returnType().name().equals(DotName.createSimple(String.class))) { + return OpenApiConstants.DEFAULT_MEDIA_TYPES.get(); + } + return context.getConfig().getDefaultProduces().orElseGet(OpenApiConstants.DEFAULT_MEDIA_TYPES); + } } diff --git a/extension-jaxrs/src/main/java/io/smallrye/openapi/jaxrs/JaxRsAnnotationScanner.java b/extension-jaxrs/src/main/java/io/smallrye/openapi/jaxrs/JaxRsAnnotationScanner.java index af0d458d5..f01d8e9cb 100644 --- a/extension-jaxrs/src/main/java/io/smallrye/openapi/jaxrs/JaxRsAnnotationScanner.java +++ b/extension-jaxrs/src/main/java/io/smallrye/openapi/jaxrs/JaxRsAnnotationScanner.java @@ -438,10 +438,10 @@ private void processResourceMethod(final AnnotationScannerContext context, JaxRsLogging.log.processingMethod(method.toString()); // Figure out the current @Produces and @Consumes (if any) - String[] defaultConsumes = context.getConfig().getDefaultConsumes().orElseGet(OpenApiConstants.DEFAULT_MEDIA_TYPES); + String[] defaultConsumes = getDefaultConsumes(context, method); context.setCurrentConsumes(getMediaTypes(context, method, JaxRsConstants.CONSUMES, defaultConsumes)); - String[] defaultProduces = context.getConfig().getDefaultProduces().orElseGet(OpenApiConstants.DEFAULT_MEDIA_TYPES); + String[] defaultProduces = getDefaultProduces(context, method); context.setCurrentProduces(getMediaTypes(context, method, JaxRsConstants.PRODUCES, defaultProduces)); // Process any @Operation annotation diff --git a/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringAnnotationScanner.java b/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringAnnotationScanner.java index 31fb8c4c9..3de75efc9 100644 --- a/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringAnnotationScanner.java +++ b/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringAnnotationScanner.java @@ -289,11 +289,11 @@ private void processControllerMethod(final AnnotationScannerContext context, SpringLogging.log.processingMethod(method.toString()); // Figure out the current @Produces and @Consumes (if any) - context.setCurrentConsumes(getMediaTypes(method, SpringConstants.MAPPING_CONSUMES, - context.getConfig().getDefaultConsumes().orElse(OpenApiConstants.DEFAULT_MEDIA_TYPES.get())).orElse(null)); + String[] defaultConsumes = getDefaultConsumes(context, method); + context.setCurrentConsumes(getMediaTypes(method, SpringConstants.MAPPING_CONSUMES, defaultConsumes).orElse(null)); - context.setCurrentProduces(getMediaTypes(method, SpringConstants.MAPPING_PRODUCES, - context.getConfig().getDefaultProduces().orElse(OpenApiConstants.DEFAULT_MEDIA_TYPES.get())).orElse(null)); + String[] defaultProduces = getDefaultProduces(context, method); + context.setCurrentProduces(getMediaTypes(method, SpringConstants.MAPPING_PRODUCES, defaultProduces).orElse(null)); // Process any @Operation annotation Optional maybeOperation = processOperation(context, resourceClass, method); diff --git a/extension-vertx/src/main/java/io/smallrye/openapi/vertx/VertxAnnotationScanner.java b/extension-vertx/src/main/java/io/smallrye/openapi/vertx/VertxAnnotationScanner.java index d061c6756..885b4ad32 100644 --- a/extension-vertx/src/main/java/io/smallrye/openapi/vertx/VertxAnnotationScanner.java +++ b/extension-vertx/src/main/java/io/smallrye/openapi/vertx/VertxAnnotationScanner.java @@ -233,10 +233,12 @@ private void processRouteMethod(final AnnotationScannerContext context, VertxLogging.log.processingMethod(method.toString()); // Figure out the current @Produces and @Consumes (if any) + String[] defaultConsumes = getDefaultConsumes(context, method); context.setCurrentConsumes(getMediaTypes(method, VertxConstants.ROUTE_CONSUMES, - context.getConfig().getDefaultConsumes().orElse(OpenApiConstants.DEFAULT_MEDIA_TYPES.get())).orElse(null)); + defaultConsumes).orElse(null)); + String[] defaultProduces = getDefaultProduces(context, method); context.setCurrentProduces(getMediaTypes(method, VertxConstants.ROUTE_PRODUCES, - context.getConfig().getDefaultProduces().orElse(OpenApiConstants.DEFAULT_MEDIA_TYPES.get())).orElse(null)); + defaultProduces).orElse(null)); // Process any @Operation annotation Optional maybeOperation = processOperation(context, resourceClass, method);