Skip to content

Commit

Permalink
Merge branch 'master' into bugfix/issue-1961
Browse files Browse the repository at this point in the history
  • Loading branch information
gracekarina authored Feb 5, 2024
2 parents 3e7e97f + 285907c commit b88fcdc
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ protected void updateRefs(ApiResponse response, String pathRef) {
if(response.getContent() != null) {
Map<String, MediaType> content = response.getContent();
for (String key: content.keySet()) {
MediaType mediaType = content.get(key);
if (mediaType.getSchema() != null) {
updateRefs(mediaType.getSchema(), pathRef);
}
Map<String, Example> examples = content.get(key).getExamples();
if (examples != null) {
for( Example example:examples.values()){
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@
public class OpenAPIV3ParserTest {
List<AuthorizationValue> 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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
openapi: 3.0.3
info:
title: title
version: LATEST

paths:
/get:
$ref: 'paths/get.yaml#/endpoint'
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b88fcdc

Please sign in to comment.