use of io.swagger.parser.util.SwaggerDeserializer in project swagger-parser by swagger-api.
the class ResolverCache method loadRef.
public <T> T loadRef(String ref, RefFormat refFormat, Class<T> expectedType) {
if (refFormat == RefFormat.INTERNAL) {
// we don't need to go get anything for internal refs
Object loadedRef = loadInternalRef(ref);
try {
return expectedType.cast(loadedRef);
} catch (Exception e) {
return null;
}
}
final String[] refParts = ref.split("#/");
if (refParts.length > 2) {
throw new RuntimeException("Invalid ref format: " + ref);
}
final String file = refParts[0];
final String definitionPath = refParts.length == 2 ? refParts[1] : null;
// we might have already resolved this ref, so check the resolutionCache
Object previouslyResolvedEntity = resolutionCache.get(ref);
if (previouslyResolvedEntity != null) {
return expectedType.cast(previouslyResolvedEntity);
}
// we have not resolved this particular ref
// but we may have already loaded the file or url in question
String contents = externalFileCache.get(file);
if (contents == null) {
if (parentDirectory != null) {
contents = RefUtils.readExternalRef(file, refFormat, auths, parentDirectory);
} else if (rootPath != null) {
contents = RefUtils.readExternalUrlRef(file, refFormat, auths, rootPath);
}
externalFileCache.put(file, contents);
}
if (definitionPath == null) {
T result = DeserializationUtils.deserialize(contents, file, expectedType);
resolutionCache.put(ref, result);
return result;
}
// a definition path is defined, meaning we need to "dig down" through the JSON tree and get the desired entity
JsonNode tree = DeserializationUtils.deserializeIntoTree(contents, file);
String[] jsonPathElements = definitionPath.split("/");
for (String jsonPathElement : jsonPathElements) {
tree = tree.get(unescapePointer(jsonPathElement));
// if at any point we do find an element we expect, print and error and abort
if (tree == null) {
throw new RuntimeException("Could not find " + definitionPath + " in contents of " + file);
}
}
T result;
if (expectedType.equals(Model.class)) {
SwaggerDeserializer ser = new SwaggerDeserializer();
result = (T) ser.definition((ObjectNode) tree, definitionPath.replace("/", "."), null);
} else {
result = DeserializationUtils.deserialize(tree, file, expectedType);
}
updateLocalRefs(file, result);
resolutionCache.put(ref, result);
return result;
}
use of io.swagger.parser.util.SwaggerDeserializer in project swagger-parser by swagger-api.
the class Swagger20Parser method convertToSwagger.
private Swagger convertToSwagger(String data) throws IOException {
if (data != null) {
JsonNode rootNode;
if (data.trim().startsWith("{")) {
ObjectMapper mapper = Json.mapper();
rootNode = mapper.readTree(data);
} else {
rootNode = DeserializationUtils.readYamlTree(data);
}
if (System.getProperty("debugParser") != null) {
LOGGER.info("\n\nSwagger Tree: \n" + ReflectionToStringBuilder.toString(rootNode, ToStringStyle.MULTI_LINE_STYLE) + "\n\n");
}
if (rootNode == null) {
return null;
}
// must have swagger node set
JsonNode swaggerNode = rootNode.get("swagger");
if (swaggerNode == null) {
return null;
} else {
SwaggerDeserializationResult result = new SwaggerDeserializer().deserialize(rootNode);
Swagger convertValue = result.getSwagger();
if (System.getProperty("debugParser") != null) {
LOGGER.info("\n\nSwagger Tree convertValue : \n" + ReflectionToStringBuilder.toString(convertValue, ToStringStyle.MULTI_LINE_STYLE) + "\n\n");
}
return convertValue;
}
} else {
return null;
}
}
Aggregations