diff --git a/core/src/main/java/io/smallrye/openapi/api/OpenApiConfig.java b/core/src/main/java/io/smallrye/openapi/api/OpenApiConfig.java index b0b02b501..13d056efb 100644 --- a/core/src/main/java/io/smallrye/openapi/api/OpenApiConfig.java +++ b/core/src/main/java/io/smallrye/openapi/api/OpenApiConfig.java @@ -217,6 +217,14 @@ default Optional getDefaultConsumes() { return getConfigValue(OpenApiConstants.DEFAULT_CONSUMES, String[].class, Optional::of, Optional::empty); } + default Optional getDefaultPrimitivesProduces() { + return getConfigValue(OpenApiConstants.DEFAULT_PRODUCES_PRIMITIVES, String[].class, Optional::of, Optional::empty); + } + + default Optional getDefaultPrimitivesConsumes() { + return getConfigValue(OpenApiConstants.DEFAULT_CONSUMES_PRIMITIVES, String[].class, Optional::of, Optional::empty); + } + default Optional allowNakedPathParameter() { return Optional.empty(); } diff --git a/core/src/main/java/io/smallrye/openapi/api/constants/OpenApiConstants.java b/core/src/main/java/io/smallrye/openapi/api/constants/OpenApiConstants.java index 7666730cc..eb92426cd 100644 --- a/core/src/main/java/io/smallrye/openapi/api/constants/OpenApiConstants.java +++ b/core/src/main/java/io/smallrye/openapi/api/constants/OpenApiConstants.java @@ -62,6 +62,9 @@ public final class OpenApiConstants { public static final String DUPLICATE_OPERATION_ID_BEHAVIOR = SMALLRYE_PREFIX + "duplicateOperationIdBehavior"; public static final String DEFAULT_PRODUCES = SMALLRYE_PREFIX + "defaultProduces"; public static final String DEFAULT_CONSUMES = SMALLRYE_PREFIX + "defaultConsumes"; + public static final String DEFAULT_PRODUCES_PRIMITIVES = SMALLRYE_PREFIX + "defaultPrimitivesProduces"; + public static final String DEFAULT_CONSUMES_PRIMITIVES = SMALLRYE_PREFIX + "defaultPrimitivesConsumes"; + public static final String MAXIMUM_STATIC_FILE_SIZE = SMALLRYE_PREFIX + "maximumStaticFileSize"; public static final String AUTO_INHERITANCE = SMALLRYE_PREFIX + "auto-inheritance"; 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..fc0708123 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); } + + public String[] getDefaultConsumes(AnnotationScannerContext context, MethodInfo methodInfo) { + if (methodInfo.returnType().kind().equals(Type.Kind.PRIMITIVE) + || methodInfo.returnType().name().equals(DotName.createSimple(String.class))) { + return context.getConfig().getDefaultPrimitivesConsumes().orElseGet(OpenApiConstants.DEFAULT_MEDIA_TYPES); + } + return context.getConfig().getDefaultConsumes().orElseGet(OpenApiConstants.DEFAULT_MEDIA_TYPES); + } + + public String[] getDefaultProduces(AnnotationScannerContext context, MethodInfo methodInfo) { + if (methodInfo.returnType().kind().equals(Type.Kind.PRIMITIVE) + || methodInfo.returnType().name().equals(DotName.createSimple(String.class))) { + return context.getConfig().getDefaultPrimitivesProduces().orElseGet(OpenApiConstants.DEFAULT_MEDIA_TYPES); + } + return context.getConfig().getDefaultProduces().orElseGet(OpenApiConstants.DEFAULT_MEDIA_TYPES); + } } diff --git a/core/src/main/java/io/smallrye/openapi/runtime/scanner/spi/AnnotationScanner.java b/core/src/main/java/io/smallrye/openapi/runtime/scanner/spi/AnnotationScanner.java index 72a9c8263..d5626f17a 100644 --- a/core/src/main/java/io/smallrye/openapi/runtime/scanner/spi/AnnotationScanner.java +++ b/core/src/main/java/io/smallrye/openapi/runtime/scanner/spi/AnnotationScanner.java @@ -101,6 +101,10 @@ public boolean containsScannerAnnotations(List instances, // Allow runtimes to set the context root path public void setContextRoot(String path); + public String[] getDefaultConsumes(AnnotationScannerContext context, MethodInfo methodInfo); + + public String[] getDefaultProduces(AnnotationScannerContext context, MethodInfo methodInfo); + default boolean isMultipartOutput(Type returnType) { return false; } @@ -496,7 +500,7 @@ default void createResponseFromRestMethod(final AnnotationScannerContext context String[] produces = context.getCurrentProduces(); if (produces == null || produces.length == 0) { - produces = context.getConfig().getDefaultProduces().orElse(OpenApiConstants.DEFAULT_MEDIA_TYPES.get()); + produces = getDefaultProduces(context, method); } if (schema != null && schema.getNullable() == null && TypeUtil.isOptional(returnType)) { 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);