Skip to content

Commit

Permalink
Merge branch 'master' into fix_ref
Browse files Browse the repository at this point in the history
  • Loading branch information
gracekarina authored Apr 5, 2023
2 parents bf01547 + 4e31c73 commit d163a6c
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

public class SwaggerParseResult {
private List<String> messages = null;
Expand Down Expand Up @@ -66,4 +67,17 @@ public SwaggerParseResult openapi31(boolean openapi31) {
public boolean isOpenapi31() {
return this.openapi31;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SwaggerParseResult result = (SwaggerParseResult) o;
return openapi31 == result.openapi31 && Objects.equals(messages, result.messages) && Objects.equals(openAPI, result.openAPI);
}

@Override
public int hashCode() {
return Objects.hash(messages, openAPI, openapi31);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ public class OpenAPIDeserializer {

protected static Map<String, Map<String, Set<String>>> KEYS = new LinkedHashMap<>();

protected static Set<JsonNodeType> validNodeTypes = new LinkedHashSet<>(
Arrays.asList(JsonNodeType.OBJECT, JsonNodeType.STRING));


static {
Map<String, Set<String>> keys30 = new LinkedHashMap<>();
Map<String, Set<String>> keys31 = new LinkedHashMap<>();
Expand Down Expand Up @@ -3235,9 +3239,17 @@ public Map<String, Example> getExamples(ObjectNode obj, String location, ParseRe
}

JsonNode exampleValue = obj.get(exampleName);
if (!exampleValue.getNodeType().equals(JsonNodeType.OBJECT)) {
if (!validNodeTypes.contains(exampleValue.getNodeType())) {
result.invalidType(location, exampleName, "object", exampleValue);
} else {
} else if (exampleValue.getNodeType().equals(JsonNodeType.STRING)) {
TextNode stringExample = (TextNode) exampleValue;
if (stringExample != null) {
Example exampleObj = getTextExample(stringExample);
if (exampleObj != null) {
examples.put(exampleName, exampleObj);
}
}
} else {
ObjectNode example = (ObjectNode) exampleValue;
if (example != null) {
Example exampleObj = getExample(example, String.format("%s.%s", location, exampleName),
Expand All @@ -3251,6 +3263,13 @@ public Map<String, Example> getExamples(ObjectNode obj, String location, ParseRe
return examples;
}

private Example getTextExample(TextNode textNode) {
if (textNode == null) return null;
Example example = new Example();
example.setValue(textNode.textValue());
return example;
}

public Example getExample(ObjectNode node, String location, ParseResult result) {
if (node == null)
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ private void tearDownWireMockServer() {
@Test
public void test30(@Injectable final List<AuthorizationValue> auths) throws Exception{

String pathFile = FileUtils.readFileToString(new File("src/test/resources/oas3.yaml.template"));
String pathFile = FileUtils.readFileToString(new File("src/test/resources/oas3.yaml.template"));
pathFile = pathFile.replace("${dynamicPort}", String.valueOf(this.serverPort));
ParseOptions options = new ParseOptions();
options.setResolve(true);
Expand Down Expand Up @@ -2994,7 +2994,7 @@ public void testIssue1003_ExtensionsClassloader() {
try {
// Temporarily switch tccl to an unproductive cl
final ClassLoader tcclTemp = new java.net.URLClassLoader(new java.net.URL[] {},
ClassLoader.getSystemClassLoader());
ClassLoader.getSystemClassLoader());
Thread.currentThread().setContextClassLoader(tcclTemp);
api = new OpenAPIV3Parser().read("src/test/resources/test.yaml");
} finally {
Expand Down Expand Up @@ -3357,7 +3357,7 @@ public void testRelativePathIssue1543() {
SwaggerParseResult readResult = parser.readLocation("src/test/resources/issue-1543/openapi.yaml", null, options);

if (readResult.getMessages().size() > 0) {
Assert.assertFalse(readResult.getMessages().get(0).contains("end -1"));
Assert.assertFalse(readResult.getMessages().get(0).contains("end -1"));
}
}

Expand Down Expand Up @@ -3498,4 +3498,18 @@ public void testInternalRefsValidation() throws Exception {
SwaggerParseResult parseResult = new OpenAPIV3Parser().readContents(yamlString, null, options);
assertEquals(parseResult.getMessages().size(), 1);
}
}

@Test
public void testIssue1902_component_example_not_parse_ordinal_json() {
ParseOptions options = new ParseOptions();
options.setResolve(true);
SwaggerParseResult result = new OpenAPIV3Parser()
.readLocation("src/test/resources/issue1902/1902-string-example-in-component.yaml",
null, options);
Assert.assertNotNull(result);
Assert.assertNotNull(result.getOpenAPI());
OpenAPI openAPI = result.getOpenAPI();
Example expectedExample = openAPI.getComponents().getExamples().get("Things");
assertNotNull(expectedExample);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
openapi: "3.0.0"

info:
title: Test API spec
version: '1.0'

servers:
- url: http://example.mocklab.io
paths:
/things:
get:
description: Simple GET with no parameters

responses:
'200':
description: things response
content:
application/json:
example:
$ref: '#/components/examples/Things'

components:
examples:
Things: |
{
"things": [
{
id: 1
},
{
id: 2
}
]
}

0 comments on commit d163a6c

Please sign in to comment.