Skip to content

Commit

Permalink
Merge pull request #1566 from swagger-api/1518
Browse files Browse the repository at this point in the history
Fix for 1518 with no SOF
  • Loading branch information
gracekarina committed May 27, 2021
2 parents 4ccec46 + 65844d2 commit 4556080
Show file tree
Hide file tree
Showing 17 changed files with 639 additions and 7 deletions.
Expand Up @@ -162,7 +162,6 @@ else if (rootPath != null) {
}

updateLocalRefs(file, result);

resolutionCache.put(ref, result);

return result;
Expand Down
Expand Up @@ -244,5 +244,18 @@ public void processSchemas(Set<String> schemaKeys, Map<String, Schema> schemas)
}
}
}
//process schemas again to check properties that hasn't been solved
for (String modelName : schemaKeys) {
final Schema model = schemas.get(modelName);
Map<String, Schema> properties = model.getProperties();
if (properties != null) {
for (Map.Entry<String, Schema> propertyEntry : properties.entrySet()) {
Schema property = propertyEntry.getValue();
if(property.get$ref() != null) {
schemaProcessor.processSchema(model);
}
}
}
}
}
}
Expand Up @@ -49,7 +49,7 @@ public ExternalRefProcessor(ResolverCache cache, OpenAPI openAPI) {
this.openAPI = openAPI;
}

private String finalNameRec(Map<String, Schema> schemas, String possiblyConflictingDefinitionName, Schema newScema,
private String finalNameRec(Map<String, Schema> schemas, String possiblyConflictingDefinitionName, Schema newSchema,
int iteration) {
String tryName =
iteration == 0 ? possiblyConflictingDefinitionName : possiblyConflictingDefinitionName + "_" + iteration;
Expand All @@ -58,9 +58,23 @@ private String finalNameRec(Map<String, Schema> schemas, String possiblyConflict
if (existingModel.get$ref() != null) {
// use the new model
existingModel = null;
} else if (!newScema.equals(existingModel)) {
} else if (!newSchema.equals(existingModel)) {
if(cache.getResolutionCache().get(newSchema.get$ref())!= null){
return tryName;
}
LOGGER.debug("A model for " + existingModel + " already exists");
return finalNameRec(schemas, possiblyConflictingDefinitionName, newScema, ++iteration);
return finalNameRec(schemas, possiblyConflictingDefinitionName, newSchema, ++iteration);
}
}else{
// validate the name
if(existingModel == null){
for(String name: schemas.keySet()){
if(name.toLowerCase().equals(tryName.toLowerCase())){
existingModel = schemas.get(name);
tryName = name;
break;
}
}
}
}
return tryName;
Expand Down Expand Up @@ -95,7 +109,7 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) {
newRef = finalNameRec(schemas, possiblyConflictingDefinitionName, schema, 0);
cache.putRenamedRef($ref, newRef);
Schema existingModel = schemas.get(newRef);
if(existingModel != null && existingModel.get$ref() != null) {
if(existingModel != null && existingModel.get$ref() != null) {
// use the new model
existingModel = null;
}
Expand Down Expand Up @@ -192,7 +206,6 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) {
}
}
}

return newRef;
}

Expand Down
Expand Up @@ -116,7 +116,6 @@ public void processPropertySchema(Schema schema) {
}else {
processSchemaType(property);
}

}
}
}
Expand Down
Expand Up @@ -188,6 +188,10 @@ String getRenamedRef(String ref) {

schemaProcessor.processSchema(refModel);
times = 1;

resolvedModel.getProperties();
times = 1;

}};

new ComponentsProcessor(openAPI, mockResolverCache).processComponents();
Expand Down
Expand Up @@ -83,6 +83,30 @@ public class OpenAPIV3ParserTest {
protected int serverPort = getDynamicPort();
protected WireMockServer wireMockServer;

@Test
public void testIssue1518() {
ParseOptions options = new ParseOptions();
options.setResolve(true);
SwaggerParseResult result = new OpenAPIV3Parser().readLocation("issue-1518/api.json", null, options);
OpenAPI openAPI = result.getOpenAPI();
assertTrue(((Schema)openAPI.getComponents().getSchemas().get("Analemmata").getProperties().get("tashotSipe")).get$ref().equals("#/components/schemas/TashotSipe"));
assertTrue(openAPI.getComponents().getSchemas().get("analemmata") == null);
}

@Test
public void testIssue1518StackOverFlow() {
ParseOptions options = new ParseOptions();
options.setResolve(true);
try {
SwaggerParseResult result = new OpenAPIV3Parser().readLocation("example0/api.json", null, options);
Yaml.prettyPrint(result.getOpenAPI().getComponents().getSchemas());
}catch (StackOverflowError stackOverflowError){
assertTrue(false);
}


}

@Test
public void testIssue251() throws IOException {
String pathFile = FileUtils.readFileToString(new File("src/test/resources/domain.yaml"), "UTF-8");
Expand Down
87 changes: 87 additions & 0 deletions modules/swagger-parser-v3/src/test/resources/example0/api.json
@@ -0,0 +1,87 @@
{
"openapi": "3.0.0",
"info": {
"version": "v3.1.1",
"title": "Foo API",
"x-serviceName": "foo",
"description": "Blah blah blah",
"contact": {
"name": "Foo Team",
"email": "bar@foo.com"
}
},
"servers": [
{
"url": "http://localhost:8080"
}
],
"paths": {
"/v4/foo": {
"get": {
"description": "Blah blah blah",
"operationId": "getFoos",
"tags": [
"Foo"
],
"parameters": [
{
"name": "my_id",
"description": "Blah blah blah",
"in": "query",
"required": false,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "Blah blah blah",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Combination"
}
}
}
},
"default": {
"description": "Blah blah blah",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Combination": {
"$ref": "./models/combination.json"
},
"Combinations": {
"$ref": "./models/combinations.json"
},
"Raygun": {
"$ref": "./models/target/raygun.json"
},
"Dingy": {
"$ref": "./models/target/dingy.json"
},
"Headlight": {
"$ref": "./models/target/headlight.json"
},
"Stamp": {
"$ref": "./models/target/stamp.json"
},
"Stripe": {
"$ref": "./models/target/stripe.json"
}
}
}
}
@@ -0,0 +1,3 @@
{
"artifactVersion": "1.1.1"
}
@@ -0,0 +1,50 @@
{
"type": "object",
"description": "Blah blah blah",
"properties": {
"file": {
"$ref": "#/components/schemas/Link",
"description": "Blah blah blah",
"readOnly": true
},
"fileVersion": {
"$ref": "#/components/schemas/Link",
"description": "Blah blah blah"
},
"target": {
"oneOf": [
{
"$ref": "#/components/schemas/Raygun"
},
{
"$ref": "#/components/schemas/Dingy"
},
{
"$ref": "#/components/schemas/Headlight"
}
],
"description": "Blah blah blah"
},
"createdBy": {
"$ref": "#/components/schemas/Link",
"description": "Blah blah blah",
"readOnly": true
}
},
"components": {
"schemas": {
"Link": {
"$ref": "./common/link.json"
},
"Raygun": {
"$ref": "./target/raygun.json"
},
"Dingy": {
"$ref": "./target/dingy.json"
},
"Headlight": {
"$ref": "./target/headlight.json"
}
}
}
}
@@ -0,0 +1,14 @@
{
"type": "object",
"required": [
"data"
],
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "./combination.json"
}
}
}
}
@@ -0,0 +1,4 @@
{
"type": "object",
"description": "Blah blah blah"
}
@@ -0,0 +1,12 @@
{
"properties": {
"type": {
"description": "Blah blah blah",
"type": "string"
},
"id": {
"description": "Blah blah blah",
"type": "string"
}
}
}

0 comments on commit 4556080

Please sign in to comment.