Skip to content

Commit

Permalink
Default content type for wrapped types primitive (#1590)
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Kruger <[email protected]>
  • Loading branch information
phillip-kruger authored Oct 4, 2023
1 parent 0ad3014 commit 21cbaef
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import io.smallrye.openapi.api.OpenApiConfig;
import io.smallrye.openapi.api.constants.OpenApiConstants;
import io.smallrye.openapi.runtime.util.TypeUtil;

/**
* Abstract base class for annotation scanners
Expand Down Expand Up @@ -104,18 +105,19 @@ private static boolean profileIncluded(OpenApiConfig config, Set<String> profile
}

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))) {
if (isPrimimive(methodInfo.returnType())) {
return context.getConfig().getDefaultPrimitivesProduces().orElseGet(OpenApiConstants.DEFAULT_MEDIA_TYPES);
}
return context.getConfig().getDefaultProduces().orElseGet(OpenApiConstants.DEFAULT_MEDIA_TYPES);
}

private boolean isPrimimive(Type type) {
return type.kind().equals(Type.Kind.PRIMITIVE)
|| type.name().equals(DotName.createSimple(String.class))
|| (TypeUtil.isWrappedType(type) && isPrimimive(TypeUtil.unwrapType(type)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ void testJakartaConfigured() throws IOException, JSONException {
void testConfigured(Index i) throws IOException, JSONException {
System.setProperty(OpenApiConstants.DEFAULT_CONSUMES, "application/json");
System.setProperty(OpenApiConstants.DEFAULT_PRODUCES, "application/json");
System.setProperty(OpenApiConstants.DEFAULT_CONSUMES_PRIMITIVES, "text/plain");
System.setProperty(OpenApiConstants.DEFAULT_PRODUCES_PRIMITIVES, "text/plain");
Config config = ConfigProvider.getConfig();
OpenApiConfig openApiConfig = OpenApiConfig.fromConfig(config);

Expand All @@ -83,6 +85,8 @@ void testConfigured(Index i) throws IOException, JSONException {
} finally {
System.clearProperty(OpenApiConstants.DEFAULT_CONSUMES);
System.clearProperty(OpenApiConstants.DEFAULT_PRODUCES);
System.clearProperty(OpenApiConstants.DEFAULT_CONSUMES_PRIMITIVES);
System.clearProperty(OpenApiConstants.DEFAULT_PRODUCES_PRIMITIVES);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package test.io.smallrye.openapi.runtime.scanner.resources.jakarta;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import io.smallrye.mutiny.Uni;
import test.io.smallrye.openapi.runtime.scanner.entities.Greeting;

/**
Expand All @@ -28,6 +33,30 @@ public Greeting hello(Greeting greeting) {
return greeting;
}

@GET
@Path("/plain")
public String justString() {
return "Hello there";
}

@GET
@Path("/plainOptional")
public Optional<String> optionalString() {
return Optional.of("Hello there");
}

@GET
@Path("/plainUni")
public Uni<String> uniString() {
return Uni.createFrom().item("Hello there");
}

@GET
@Path("/plainList")
public List<String> listString() {
return Arrays.asList(new String[] { "Hello there" });
}

@GET
@Path("/goodbye")
@Produces(MediaType.APPLICATION_XML)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package test.io.smallrye.openapi.runtime.scanner.resources.javax;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import io.smallrye.mutiny.Uni;
import test.io.smallrye.openapi.runtime.scanner.entities.Greeting;

/**
Expand All @@ -28,11 +33,35 @@ public Greeting hello(Greeting greeting) {
return greeting;
}

@GET
@Path("/plain")
public String justString() {
return "Hello there";
}

@GET
@Path("/plainOptional")
public Optional<String> optionalString() {
return Optional.of("Hello there");
}

@GET
@Path("/plainUni")
public Uni<String> uniString() {
return Uni.createFrom().item("Hello there");
}

@GET
@Path("/plainList")
public List<String> listString() {
return Arrays.asList(new String[] { "Hello there" });
}

@GET
@Path("/goodbye")
@Produces(MediaType.APPLICATION_XML)
public Greeting byebye() {
return new Greeting("Good Bye !");
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,74 @@
}
}
}
},
"/greeting/plain" : {
"get" : {
"responses" : {
"200" : {
"description" : "OK",
"content" : {
"text/plain" : {
"schema" : {
"type" : "string"
}
}
}
}
}
}
},
"/greeting/plainList" : {
"get" : {
"responses" : {
"200" : {
"description" : "OK",
"content" : {
"application/json" : {
"schema" : {
"type" : "array",
"items" : {
"type" : "string"
}
}
}
}
}
}
}
},
"/greeting/plainOptional" : {
"get" : {
"responses" : {
"200" : {
"description" : "OK",
"content" : {
"text/plain" : {
"schema" : {
"type" : "string",
"nullable" : true
}
}
}
}
}
}
},
"/greeting/plainUni" : {
"get" : {
"responses" : {
"200" : {
"description" : "OK",
"content" : {
"text/plain" : {
"schema" : {
"type" : "string"
}
}
}
}
}
}
}
},
"components" : {
Expand All @@ -73,4 +141,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,74 @@
}
}
}
},
"/greeting/plain" : {
"get" : {
"responses" : {
"200" : {
"description" : "OK",
"content" : {
"*/*" : {
"schema" : {
"type" : "string"
}
}
}
}
}
}
},
"/greeting/plainList" : {
"get" : {
"responses" : {
"200" : {
"description" : "OK",
"content" : {
"*/*" : {
"schema" : {
"type" : "array",
"items" : {
"type" : "string"
}
}
}
}
}
}
}
},
"/greeting/plainOptional" : {
"get" : {
"responses" : {
"200" : {
"description" : "OK",
"content" : {
"*/*" : {
"schema" : {
"type" : "string",
"nullable" : true
}
}
}
}
}
}
},
"/greeting/plainUni" : {
"get" : {
"responses" : {
"200" : {
"description" : "OK",
"content" : {
"*/*" : {
"schema" : {
"type" : "string"
}
}
}
}
}
}
}
},
"components" : {
Expand All @@ -64,4 +132,4 @@
}
}
}
}
}

0 comments on commit 21cbaef

Please sign in to comment.