Skip to content

Commit

Permalink
different default content type for pojo return and primitives
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Kruger <[email protected]>
  • Loading branch information
phillip-kruger committed Sep 28, 2023
1 parent 0cae54a commit b266568
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 9 deletions.
8 changes: 8 additions & 0 deletions core/src/main/java/io/smallrye/openapi/api/OpenApiConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,14 @@ default Optional<String[]> getDefaultConsumes() {
return getConfigValue(OpenApiConstants.DEFAULT_CONSUMES, String[].class, Optional::of, Optional::empty);
}

default Optional<String[]> getDefaultPrimitivesProduces() {
return getConfigValue(OpenApiConstants.DEFAULT_PRODUCES_PRIMITIVES, String[].class, Optional::of, Optional::empty);
}

default Optional<String[]> getDefaultPrimitivesConsumes() {
return getConfigValue(OpenApiConstants.DEFAULT_CONSUMES_PRIMITIVES, String[].class, Optional::of, Optional::empty);
}

default Optional<Boolean> allowNakedPathParameter() {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -99,4 +102,20 @@ private static boolean profileIncluded(OpenApiConfig config, Set<String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public boolean containsScannerAnnotations(List<AnnotationInstance> 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;
}
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Operation> maybeOperation = processOperation(context, resourceClass, method);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Operation> maybeOperation = processOperation(context, resourceClass, method);
Expand Down

0 comments on commit b266568

Please sign in to comment.