use of io.swagger.v3.parser.models.RefFormat in project swagger-parser by swagger-api.
the class ExternalRefProcessor method processRefHeader.
private void processRefHeader(Header subRef, String externalFile) {
RefFormat format = computeRefFormat(subRef.get$ref());
if (!isAnExternalRefFormat(format)) {
subRef.set$ref(RefType.SCHEMAS.getInternalPrefix() + processRefToExternalSchema(externalFile + subRef.get$ref(), RefFormat.RELATIVE));
return;
}
String $ref = subRef.get$ref();
String subRefExternalPath = getExternalPath(subRef.get$ref()).orElse(null);
if (format.equals(RefFormat.RELATIVE) && !Objects.equals(subRefExternalPath, externalFile)) {
$ref = join(externalFile, subRef.get$ref());
subRef.set$ref($ref);
} else {
processRefToExternalHeader($ref, format);
}
}
use of io.swagger.v3.parser.models.RefFormat in project swagger-parser by swagger-api.
the class ExternalRefProcessor method processRefToExternalCallback.
public String processRefToExternalCallback(String $ref, RefFormat refFormat) {
String renamedRef = cache.getRenamedRef($ref);
if (renamedRef != null) {
return renamedRef;
}
final Callback callback = cache.loadRef($ref, refFormat, Callback.class);
if (callback == 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, Callback> callbacks = openAPI.getComponents().getCallbacks();
if (callbacks == null) {
callbacks = new LinkedHashMap<>();
}
final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
Callback existingCallback = callbacks.get(possiblyConflictingDefinitionName);
if (existingCallback != null) {
LOGGER.debug("A model for " + existingCallback + " already exists");
if (existingCallback.get$ref() != null) {
// use the new model
existingCallback = null;
}
}
newRef = possiblyConflictingDefinitionName;
cache.putRenamedRef($ref, newRef);
if (existingCallback == null) {
// don't overwrite existing model reference
openAPI.getComponents().addCallbacks(newRef, callback);
cache.addReferencedKey(newRef);
String file = $ref.split("#/")[0];
if (callback.get$ref() != null) {
if (callback.get$ref() != null) {
RefFormat format = computeRefFormat(callback.get$ref());
if (isAnExternalRefFormat(format)) {
callback.set$ref(processRefToExternalCallback(callback.get$ref(), format));
} else {
processRefToExternalCallback(file + callback.get$ref(), RefFormat.RELATIVE);
}
}
}
}
return newRef;
}
use of io.swagger.v3.parser.models.RefFormat in project swagger-parser by swagger-api.
the class ExternalRefProcessor method processRefToExternalResponse.
public String processRefToExternalResponse(String $ref, RefFormat refFormat) {
String renamedRef = cache.getRenamedRef($ref);
if (renamedRef != null) {
return renamedRef;
}
final ApiResponse response = cache.loadRef($ref, refFormat, ApiResponse.class);
String newRef;
if (openAPI.getComponents() == null) {
openAPI.setComponents(new Components());
}
Map<String, ApiResponse> responses = openAPI.getComponents().getResponses();
if (responses == null) {
responses = new LinkedHashMap<>();
}
final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
ApiResponse existingResponse = responses.get(possiblyConflictingDefinitionName);
if (existingResponse != null) {
LOGGER.debug("A model for " + existingResponse + " already exists");
if (existingResponse.get$ref() != null) {
// use the new model
existingResponse = null;
}
}
newRef = possiblyConflictingDefinitionName;
openAPI.getComponents().addResponses(newRef, response);
cache.putRenamedRef($ref, newRef);
if (response != null) {
if (response.getContent() != null) {
processRefContent(response.getContent(), $ref);
}
if (response.getHeaders() != null) {
processRefHeaders(response.getHeaders(), $ref);
}
if (response.getLinks() != null) {
processRefLinks(response.getLinks(), $ref);
}
}
return newRef;
}
use of io.swagger.v3.parser.models.RefFormat in project swagger-parser by swagger-api.
the class ExternalRefProcessor method processRefLink.
private void processRefLink(Link subRef, String externalFile) {
RefFormat format = computeRefFormat(subRef.get$ref());
if (!isAnExternalRefFormat(format)) {
subRef.set$ref(RefType.SCHEMAS.getInternalPrefix() + processRefToExternalSchema(externalFile + subRef.get$ref(), RefFormat.RELATIVE));
return;
}
String $ref = subRef.get$ref();
String subRefExternalPath = getExternalPath(subRef.get$ref()).orElse(null);
if (format.equals(RefFormat.RELATIVE) && !Objects.equals(subRefExternalPath, externalFile)) {
$ref = join(externalFile, subRef.get$ref());
subRef.set$ref($ref);
} else {
processRefToExternalLink($ref, format);
}
}
use of io.swagger.v3.parser.models.RefFormat in project swagger-parser by swagger-api.
the class ExternalRefProcessor method processRefToExternalSchema.
public String processRefToExternalSchema(String $ref, RefFormat refFormat) {
String renamedRef = cache.getRenamedRef($ref);
if (renamedRef != null) {
return renamedRef;
}
final Schema schema = cache.loadRef($ref, refFormat, Schema.class);
if (schema == 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, Schema> schemas = openAPI.getComponents().getSchemas();
if (schemas == null) {
schemas = new LinkedHashMap<>();
}
final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
newRef = finalNameRec(schemas, possiblyConflictingDefinitionName, schema, 0);
cache.putRenamedRef($ref, newRef);
Schema existingModel = schemas.get(newRef);
if (existingModel != null && existingModel.get$ref() != null) {
// use the new model
existingModel = null;
}
if (existingModel == null) {
// don't overwrite existing model reference
openAPI.getComponents().addSchemas(newRef, schema);
cache.addReferencedKey(newRef);
String file = $ref.split("#/")[0];
if (schema.get$ref() != null) {
RefFormat ref = computeRefFormat(schema.get$ref());
if (isAnExternalRefFormat(ref)) {
if (!ref.equals(RefFormat.URL)) {
String schemaFullRef = schema.get$ref();
String parent = file.substring(0, file.lastIndexOf('/'));
if (!parent.isEmpty()) {
if (schemaFullRef.contains("#/")) {
String[] parts = schemaFullRef.split("#/");
String schemaFullRefFilePart = parts[0];
String schemaFullRefInternalRefPart = parts[1];
schemaFullRef = Paths.get(parent, schemaFullRefFilePart).normalize().toString() + "#/" + schemaFullRefInternalRefPart;
} else {
schemaFullRef = Paths.get(parent, schemaFullRef).normalize().toString();
}
}
schema.set$ref(processRefToExternalSchema(schemaFullRef, ref));
}
} else {
processRefToExternalSchema(file + schema.get$ref(), RefFormat.RELATIVE);
}
}
if (schema instanceof ComposedSchema) {
ComposedSchema composedSchema = (ComposedSchema) schema;
if (composedSchema.getAllOf() != null) {
for (Schema item : composedSchema.getAllOf()) {
if (item.get$ref() != null) {
processRefSchema(item, file);
} else if (item.getProperties() != null) {
processProperties(item.getProperties(), file);
}
}
}
if (composedSchema.getOneOf() != null) {
for (Schema item : composedSchema.getOneOf()) {
if (item.get$ref() != null) {
if (item.get$ref() != null) {
processRefSchema(item, file);
}
}
}
}
if (composedSchema.getAnyOf() != null) {
for (Schema item : composedSchema.getAnyOf()) {
if (item.get$ref() != null) {
if (item.get$ref() != null) {
processRefSchema(item, file);
}
}
}
}
}
// Loop the properties and recursively call this method;
Map<String, Schema> subProps = schema.getProperties();
processProperties(subProps, file);
processDiscriminator(schema.getDiscriminator(), file);
if (schema.getAdditionalProperties() != null && schema.getAdditionalProperties() instanceof Schema) {
Schema additionalProperty = (Schema) schema.getAdditionalProperties();
if (additionalProperty.get$ref() != null) {
processRefSchema(additionalProperty, file);
} else if (additionalProperty instanceof ArraySchema) {
ArraySchema arrayProp = (ArraySchema) additionalProperty;
if (arrayProp.getItems() != null && arrayProp.getItems().get$ref() != null && StringUtils.isNotBlank(arrayProp.get$ref())) {
processRefSchema(arrayProp.getItems(), file);
}
} else if (additionalProperty.getAdditionalProperties() != null && additionalProperty.getAdditionalProperties() instanceof Schema) {
Schema mapProp = (Schema) additionalProperty.getAdditionalProperties();
if (mapProp.get$ref() != null) {
processRefSchema(mapProp, file);
} else if (mapProp.getAdditionalProperties() instanceof ArraySchema && ((ArraySchema) mapProp).getItems() != null && ((ArraySchema) mapProp).getItems().get$ref() != null && StringUtils.isNotBlank(((ArraySchema) mapProp).getItems().get$ref())) {
processRefSchema(((ArraySchema) mapProp).getItems(), file);
}
}
}
if (schema instanceof ArraySchema && ((ArraySchema) schema).getItems() != null) {
ArraySchema arraySchema = (ArraySchema) schema;
if (StringUtils.isNotBlank(arraySchema.getItems().get$ref())) {
processRefSchema(((ArraySchema) schema).getItems(), file);
} else {
processProperties(arraySchema.getItems().getProperties(), file);
}
}
}
return newRef;
}
Aggregations