diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/ObjectMapperFactory.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/ObjectMapperFactory.java index 1df3fdd599..e6bea97061 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/ObjectMapperFactory.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/ObjectMapperFactory.java @@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; public class ObjectMapperFactory { public static ObjectMapper createJson() { @@ -28,6 +29,7 @@ protected static ObjectMapper createYaml(boolean includePathDeserializer, boolea private static ObjectMapper create(JsonFactory jsonFactory, boolean includePathDeserializer, boolean includeResponseDeserializer) { ObjectMapper mapper = new ObjectMapper(jsonFactory); + mapper.registerModule(new JavaTimeModule()); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/PathsProcessor.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/PathsProcessor.java index fdbbc48d57..467cb8538a 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/PathsProcessor.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/PathsProcessor.java @@ -172,6 +172,10 @@ protected void updateRefs(ApiResponse response, String pathRef) { if(response.getContent() != null) { Map content = response.getContent(); for (String key: content.keySet()) { + MediaType mediaType = content.get(key); + if (mediaType.getSchema() != null) { + updateRefs(mediaType.getSchema(), pathRef); + } Map examples = content.get(key).getExamples(); if (examples != null) { for( Example example:examples.values()){ @@ -285,10 +289,18 @@ protected boolean isAbsoluteRef(String ref) { return false; } + private boolean isInternalSchemaRef(String $ref) { + if($ref.startsWith("#/components/schemas")) { + return true; + } + return false; + } + protected String computeRef(String ref, String prefix) { - if(isLocalRef(ref)) return computeLocalRef(ref, prefix); - if(isAbsoluteRef(ref)) return ref; - return computeRelativeRef(ref, prefix); + if(isLocalRef(ref)&& !isInternalSchemaRef(ref)) return computeLocalRef(ref, prefix); + if(isAbsoluteRef(ref)) return ref; + if(isInternalSchemaRef(ref)) return ref; + return computeRelativeRef(ref, prefix); } protected String computeRelativeRef(String ref, String prefix) { diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/ObjectMapperTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/ObjectMapperTest.java new file mode 100644 index 0000000000..f16d46c776 --- /dev/null +++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/ObjectMapperTest.java @@ -0,0 +1,15 @@ +package io.swagger.v3.parser; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import org.junit.Assert; +import org.junit.Test; + +public class ObjectMapperTest { + @Test + public void testJavaTimeModule() { + ObjectMapper mapper = ObjectMapperFactory.createJson(); + Assert.assertTrue("JavaTimeModule found?", + mapper.getRegisteredModuleIds().contains(new JavaTimeModule().getTypeId())); + } +} diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java index 01f69e1b0c..9a6aa67f08 100644 --- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java +++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java @@ -43,6 +43,18 @@ public class OpenAPIV3ParserTest { List auths = new ArrayList<>(); + @Test + public void testFailedToResolveResponseReferences() { + OpenAPIV3Parser openApiParser = new OpenAPIV3Parser(); + ParseOptions options = new ParseOptions(); + options.setResolve(true); + SwaggerParseResult parseResult = openApiParser.readLocation("issue-2037/openapi.yaml", null, options); + OpenAPI openAPI = parseResult.getOpenAPI(); + + Assert.assertTrue(openAPI.getPaths().get("/get").get$ref() == null); + Assert.assertEquals(openAPI.getPaths().get("/get").getGet().getResponses().get("200").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/ResponsesRef"); + } + @Test public void testFailedToResolveExternalReferences() { diff --git a/modules/swagger-parser-v3/src/test/resources/issue-2037/openapi.yaml b/modules/swagger-parser-v3/src/test/resources/issue-2037/openapi.yaml new file mode 100644 index 0000000000..a356cb4532 --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/issue-2037/openapi.yaml @@ -0,0 +1,8 @@ +openapi: 3.0.3 +info: + title: title + version: LATEST + +paths: + /get: + $ref: 'paths/get.yaml#/endpoint' \ No newline at end of file diff --git a/modules/swagger-parser-v3/src/test/resources/issue-2037/paths/get.yaml b/modules/swagger-parser-v3/src/test/resources/issue-2037/paths/get.yaml new file mode 100644 index 0000000000..9f5b7b0738 --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/issue-2037/paths/get.yaml @@ -0,0 +1,20 @@ +endpoint: + get: + operationId: get + requestBody: + content: + application/json: + schema: + $ref: '#/RequestBodyRef' + responses: + '200': + content: + application/json: + schema: + $ref: '#/ResponsesRef' + +RequestBodyRef: + type: string + +ResponsesRef: + type: string \ No newline at end of file