diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java index 533405bdc0..390ed44e1d 100644 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java @@ -1094,8 +1094,10 @@ public static Optional getSchema(io.swagger.v3.oas.annotations schemaMap.forEach((key, schema) -> { components.addSchemas(key, schema); }); - if (resolvedSchema.schema != null) { + if (resolvedSchema.schema != null && StringUtils.isNotBlank(resolvedSchema.schema.getName())) { schemaObject.set$ref(COMPONENTS_REF + resolvedSchema.schema.getName()); + } else if (resolvedSchema.schema != null){ + schemaObject = resolvedSchema.schema; } } } diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/PrimitiveType.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/PrimitiveType.java index e9f01bd7fd..95c81e1ed3 100644 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/PrimitiveType.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/PrimitiveType.java @@ -155,6 +155,12 @@ public Schema createProperty() { */ private static final Map EXTERNAL_CLASSES; + /** + * Allows to exclude specific classes from KEY_CLASSES mappings to primitive + * Joda lib. + */ + private static Set customExcludedClasses = new ConcurrentHashMap().newKeySet(); + /** * Adds support for custom mapping of classes to primitive types */ @@ -263,6 +269,17 @@ private PrimitiveType(Class keyClass, String commonName) { this.commonName = commonName; } + + /** + * Adds support for custom mapping of classes to primitive types + * + * @return Map of custom classes to primitive type + * @since 2.0.6 + */ + public static Set customExcludedClasses() { + return customExcludedClasses; + } + /** * Adds support for custom mapping of classes to primitive types * @@ -307,7 +324,9 @@ public static PrimitiveType fromType(Type type) { final Class raw = TypeFactory.defaultInstance().constructType(type).getRawClass(); final PrimitiveType key = KEY_CLASSES.get(raw); if (key != null) { - return key; + if (!customExcludedClasses.contains(raw.getName())) { + return key; + } } final PrimitiveType custom = customClasses.get(raw.getName()); diff --git a/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/ReaderTest.java b/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/ReaderTest.java index d9a3abd5ff..e86c017ea0 100644 --- a/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/ReaderTest.java +++ b/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/ReaderTest.java @@ -9,6 +9,7 @@ import io.swagger.v3.core.filter.SpecFilter; import io.swagger.v3.core.jackson.ModelResolver; import io.swagger.v3.core.model.ApiDescription; +import io.swagger.v3.core.util.PrimitiveType; import io.swagger.v3.jaxrs2.matchers.SerializationMatchers; import io.swagger.v3.jaxrs2.resources.BasicFieldsResource; import io.swagger.v3.jaxrs2.resources.BookStoreTicket2646; @@ -53,6 +54,7 @@ import io.swagger.v3.jaxrs2.resources.Ticket2806Resource; import io.swagger.v3.jaxrs2.resources.Ticket2818Resource; import io.swagger.v3.jaxrs2.resources.Ticket2848Resource; +import io.swagger.v3.jaxrs2.resources.Ticket3015Resource; import io.swagger.v3.jaxrs2.resources.UserAnnotationResource; import io.swagger.v3.jaxrs2.resources.extensions.ExtensionsResource; import io.swagger.v3.jaxrs2.resources.extensions.OperationExtensionsResource; @@ -93,6 +95,7 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.lang.reflect.Type; +import java.net.URI; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; @@ -1829,4 +1832,86 @@ public void testCallbackWithRef() { " description: Post Path Item\n"; SerializationMatchers.assertEqualsToYaml(openAPI, yaml); } + + @Test + public void testTicket3015() { + Reader reader = new Reader(new OpenAPI()); + + OpenAPI openAPI = reader.read(Ticket3015Resource.class); + String yaml = "openapi: 3.0.1\n" + + "paths:\n" + + " /test/test:\n" + + " get:\n" + + " operationId: schemaImpl\n" + + " responses:\n" + + " 200:\n" + + " description: OK\n" + + " content:\n" + + " '*/*':\n" + + " schema:\n" + + " type: string\n" + + " format: uri\n" + + " 400:\n" + + " description: Bad Request\n" + + " 500:\n" + + " description: Internal Server Error\n"; + SerializationMatchers.assertEqualsToYaml(openAPI, yaml); + PrimitiveType.customExcludedClasses().add(URI.class.getName()); + openAPI = reader.read(Ticket3015Resource.class); + yaml = "openapi: 3.0.1\n" + + "paths:\n" + + " /test/test:\n" + + " get:\n" + + " operationId: schemaImpl_1\n" + + " responses:\n" + + " 200:\n" + + " description: OK\n" + + " content:\n" + + " '*/*':\n" + + " schema:\n" + + " type: object\n" + + " properties:\n" + + " scheme:\n" + + " type: string\n" + + " fragment:\n" + + " type: string\n" + + " authority:\n" + + " type: string\n" + + " userInfo:\n" + + " type: string\n" + + " host:\n" + + " type: string\n" + + " port:\n" + + " type: integer\n" + + " format: int32\n" + + " path:\n" + + " type: string\n" + + " query:\n" + + " type: string\n" + + " schemeSpecificPart:\n" + + " type: string\n" + + " rawSchemeSpecificPart:\n" + + " type: string\n" + + " rawAuthority:\n" + + " type: string\n" + + " rawUserInfo:\n" + + " type: string\n" + + " rawPath:\n" + + " type: string\n" + + " rawQuery:\n" + + " type: string\n" + + " rawFragment:\n" + + " type: string\n" + + " absolute:\n" + + " type: boolean\n" + + " opaque:\n" + + " type: boolean\n" + + " 400:\n" + + " description: Bad Request\n" + + " 500:\n" + + " description: Internal Server Error\n"; + SerializationMatchers.assertEqualsToYaml(openAPI, yaml); + PrimitiveType.customExcludedClasses().remove(URI.class.getName()); + } + } \ No newline at end of file diff --git a/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/resources/Ticket3015Resource.java b/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/resources/Ticket3015Resource.java new file mode 100644 index 0000000000..921750af31 --- /dev/null +++ b/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/resources/Ticket3015Resource.java @@ -0,0 +1,24 @@ +package io.swagger.v3.jaxrs2.resources; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import java.net.URI; + +@Path("test") +public class Ticket3015Resource { + + @Operation(responses = { + @ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = URI.class))), + @ApiResponse(responseCode = "400", description = "Bad Request"), + @ApiResponse(responseCode = "500", description = "Internal Server Error") + }) + @GET + @Path("/test") + public void schemaImpl() { + } +}