Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added check to avoid removing referenced models #278 #333

Merged
merged 1 commit into from
Oct 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@

import java.io.File;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -41,6 +38,7 @@ public class ResolverCache {
private final String rootPath;
private Map<String, Object> resolutionCache = new HashMap<>();
private Map<String, String> externalFileCache = new HashMap<>();
private Set<String> referencedModelKeys = new HashSet<>();

/*
a map that stores original external references, and their associated renamed references
Expand Down Expand Up @@ -173,6 +171,17 @@ private Object getFromMap(String ref, Map map, Pattern pattern) {
return null;
}

public boolean hasReferencedKey(String modelKey) {
if(referencedModelKeys == null) {
return false;
}
return referencedModelKeys.contains(modelKey);
}

public void addReferencedKey(String modelKey) {
referencedModelKeys.add(modelKey);
}

public String getRenamedRef(String originalRef) {
return renameCache.get(originalRef);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@ public void processDefinitions(Set<String> modelKeys, Map<String, Model> definit

if (renamedRef != null) {
//we definitely resolved the referenced and shoved it in the definitions map
final Model resolvedModel = definitions.remove(renamedRef);
// because the referenced model may be in the definitions map, we need to remove old instances
final Model resolvedModel = definitions.get(renamedRef);

// ensure the reference isn't still in use
if(!cache.hasReferencedKey(renamedRef)) {
definitions.remove(renamedRef);
}

// add the new key
definitions.put(modelName, resolvedModel);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public String processRefToExternalDefinition(String $ref, RefFormat refFormat) {
if(existingModel == null) {
// don't overwrite existing model reference
swagger.addDefinition(newRef, model);
cache.addReferencedKey(newRef);

String file = $ref.split("#/")[0];
if (model instanceof RefModel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,10 @@ public void testLoadRelativeFileTree_Json() throws Exception {
public void testLoadExternalNestedDefinitions() throws Exception {
SwaggerParser parser = new SwaggerParser();
final Swagger swagger = parser.read("src/test/resources/nested-references/b.yaml");

Map<String, Model> definitions = swagger.getDefinitions();
assertTrue(definitions.containsKey("x"));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that y lives on, even though it is directly referencing all aspects of z

assertTrue(!definitions.containsKey("y"));
assertTrue(definitions.containsKey("y"));
assertTrue(definitions.containsKey("z"));
assertEquals(((RefModel) definitions.get("i")).get$ref(), "#/definitions/k");
}
Expand Down Expand Up @@ -223,15 +224,15 @@ public void testLoadRelativeFileTree_Yaml() throws Exception {
assertNotNull(Yaml.mapper().writeValueAsString(swagger));
}

@Test(enabled = false)
@Test
public void testLoadRecursiveExternalDef() throws Exception {
SwaggerParser parser = new SwaggerParser();
final Swagger swagger = parser.read("src/test/resources/file-reference-to-recursive-defs/b.yaml");

Map<String, Model> definitions = swagger.getDefinitions();
assertEquals(((RefProperty) ((ArrayProperty) definitions.get("v").getProperties().get("children")).getItems()).get$ref(), "#/definitions/v");
assertTrue(!definitions.containsKey("y"));
assertEquals(((RefProperty) ((ArrayProperty) definitions.get("x").getProperties().get("children")).getItems()).get$ref(), "#/definitions/x");
assertTrue(definitions.containsKey("y"));
assertEquals(((RefProperty) ((ArrayProperty) definitions.get("x").getProperties().get("children")).getItems()).get$ref(), "#/definitions/y");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of changing the reference to x, I'm leaving it as modeled as y

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public void testProcessRefToExternalDefinition_NoNameConflict(

cache.putRenamedRef(ref, "bar");
swagger.addDefinition("bar", mockedModel); times=1;

cache.addReferencedKey("bar");
times = 1;
result = null;
}};

String newRef = new ExternalRefProcessor(cache, swagger).processRefToExternalDefinition(ref, refFormat);
Expand Down