use of io.swagger.v3.parser.models.RefFormat in project swagger-parser by swagger-api.
the class ExternalRefProcessorTest method testProcessRefToExternalDefinition_NoNameConflict.
@Test
public void testProcessRefToExternalDefinition_NoNameConflict(@Injectable final Schema mockedModel) throws Exception {
final String ref = "http://my.company.com/path/to/file.json#/foo/bar";
final RefFormat refFormat = RefFormat.URL;
new StrictExpectations() {
{
cache.getRenamedRef(ref);
times = 1;
result = null;
cache.loadRef(ref, refFormat, Schema.class);
times = 1;
result = mockedModel;
openAPI.getComponents();
times = 1;
result = new Components();
openAPI.getComponents().getSchemas();
times = 1;
result = null;
cache.putRenamedRef(ref, "bar");
openAPI.getComponents().addSchemas("bar", mockedModel);
times = 1;
cache.addReferencedKey("bar");
times = 1;
result = null;
}
};
String newRef = new ExternalRefProcessor(cache, openAPI).processRefToExternalSchema(ref, refFormat);
assertEquals(newRef, "bar");
}
use of io.swagger.v3.parser.models.RefFormat in project swagger-parser by swagger-api.
the class ResolverCacheTest method testMock.
@Test
public void testMock() throws Exception {
final RefFormat format = RefFormat.URL;
final String ref = "http://my.company.com/path/to/file.json";
final String contentsOfExternalFile = "really good json";
ParseOptions parseOptions = new ParseOptions();
parseOptions.setResolve(true);
parseOptions.setValidateExternalRefs(true);
new Expectations(DeserializationUtils.class) {
{
RefUtils.readExternalUrlRef(ref, format, auths, "http://my.company.com/path/parent.json");
times = 1;
result = contentsOfExternalFile;
DeserializationUtils.deserializeIntoTree(contentsOfExternalFile, ref, parseOptions, (SwaggerParseResult) any);
times = 1;
result = new ObjectMapper().readTree("{\"type\": \"string\"}");
}
};
ResolverCache cache = new ResolverCache(openAPI, auths, "http://my.company.com/path/parent.json", new HashSet<>(), parseOptions);
Schema firstActualResult = cache.loadRef(ref, RefFormat.URL, Schema.class);
assertEquals(firstActualResult.getType(), "string");
}
use of io.swagger.v3.parser.models.RefFormat in project swagger-parser by swagger-api.
the class ResolverCacheTest method testLoadExternalRefResponseWithNoContent.
@Test
public void testLoadExternalRefResponseWithNoContent() throws Exception {
final RefFormat format = RefFormat.URL;
final String ref = "http://my.company.com/path/to/main.yaml";
final String contentsOfExternalFile = "openapi: 3.0.0\n" + "\n" + "info:\n" + " version: 1.0.0\n" + " title: Response include test case child\n" + "\n" + "components:\n" + " responses:\n" + " 200:\n" + " description: Success\n";
new Expectations() {
{
RefUtils.readExternalUrlRef(ref, format, auths, "http://my.company.com/path/parent.json");
times = 1;
result = contentsOfExternalFile;
}
};
ResolverCache cache = new ResolverCache(openAPI, auths, "http://my.company.com/path/parent.json");
ApiResponse response = cache.loadRef(ref + "#/components/responses/200", RefFormat.URL, ApiResponse.class);
assertNotNull(response);
assertEquals(response.getDescription(), "Success");
assertNull(response.getContent());
}
use of io.swagger.v3.parser.models.RefFormat in project swagger-parser by swagger-api.
the class HeaderProcessor method processHeader.
public void processHeader(Header header) {
if (header.get$ref() != null) {
RefFormat refFormat = computeRefFormat(header.get$ref());
String $ref = header.get$ref();
if (isAnExternalRefFormat(refFormat)) {
final String newRef = externalRefProcessor.processRefToExternalHeader($ref, refFormat);
if (newRef != null) {
header.set$ref(newRef);
}
}
}
if (header.getSchema() != null) {
schemaProcessor.processSchema(header.getSchema());
}
if (header.getExamples() != null) {
if (header.getExamples() != null) {
Map<String, Example> examples = header.getExamples();
for (String key : examples.keySet()) {
exampleProcessor.processExample(header.getExamples().get(key));
}
}
}
Schema schema = null;
if (header.getContent() != null) {
Map<String, MediaType> content = header.getContent();
for (String mediaName : content.keySet()) {
MediaType mediaType = content.get(mediaName);
if (mediaType.getSchema() != null) {
schema = mediaType.getSchema();
if (schema != null) {
schemaProcessor.processSchema(schema);
}
}
}
}
}
use of io.swagger.v3.parser.models.RefFormat in project swagger-parser by swagger-api.
the class ExternalRefProcessor method processRefToExternalLink.
public String processRefToExternalLink(String $ref, RefFormat refFormat) {
String renamedRef = cache.getRenamedRef($ref);
if (renamedRef != null) {
return renamedRef;
}
final Link link = cache.loadRef($ref, refFormat, Link.class);
if (link == null) {
// stop! There's a problem. retain the original ref
LOGGER.warn("unable to load model reference from `" + $ref + "`. It may not be available " + "or the reference isn't a valid model schema");
return $ref;
}
String newRef;
if (openAPI.getComponents() == null) {
openAPI.setComponents(new Components());
}
Map<String, Link> links = openAPI.getComponents().getLinks();
if (links == null) {
links = new LinkedHashMap<>();
}
final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
Link existingLink = links.get(possiblyConflictingDefinitionName);
if (existingLink != null) {
LOGGER.debug("A model for " + existingLink + " already exists");
if (existingLink.get$ref() != null) {
// use the new model
existingLink = null;
}
}
newRef = possiblyConflictingDefinitionName;
cache.putRenamedRef($ref, newRef);
if (existingLink == null) {
// don't overwrite existing model reference
openAPI.getComponents().addLinks(newRef, link);
cache.addReferencedKey(newRef);
String file = $ref.split("#/")[0];
if (link.get$ref() != null) {
RefFormat format = computeRefFormat(link.get$ref());
if (isAnExternalRefFormat(format)) {
link.set$ref(processRefToExternalLink(link.get$ref(), format));
} else {
processRefToExternalLink(file + link.get$ref(), RefFormat.RELATIVE);
}
}
}
return newRef;
}
Aggregations