use of io.swagger.v3.parser.models.RefFormat in project swagger-parser by swagger-api.
the class ExternalRefProcessor method processRefToExternalSecurityScheme.
public String processRefToExternalSecurityScheme(String $ref, RefFormat refFormat) {
String renamedRef = cache.getRenamedRef($ref);
if (renamedRef != null) {
return renamedRef;
}
final SecurityScheme securityScheme = cache.loadRef($ref, refFormat, SecurityScheme.class);
if (securityScheme == 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, SecurityScheme> securitySchemeMap = openAPI.getComponents().getSecuritySchemes();
if (securitySchemeMap == null) {
securitySchemeMap = new LinkedHashMap<>();
}
final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
SecurityScheme existingSecurityScheme = securitySchemeMap.get(possiblyConflictingDefinitionName);
if (existingSecurityScheme != null) {
LOGGER.debug("A model for " + existingSecurityScheme + " already exists");
if (existingSecurityScheme.get$ref() != null) {
// use the new model
existingSecurityScheme = null;
}
}
newRef = possiblyConflictingDefinitionName;
cache.putRenamedRef($ref, newRef);
if (existingSecurityScheme == null) {
// don't overwrite existing model reference
openAPI.getComponents().addSecuritySchemes(newRef, securityScheme);
cache.addReferencedKey(newRef);
String file = $ref.split("#/")[0];
if (securityScheme.get$ref() != null) {
RefFormat format = computeRefFormat(securityScheme.get$ref());
if (isAnExternalRefFormat(format)) {
securityScheme.set$ref(processRefToExternalSecurityScheme(securityScheme.get$ref(), format));
} else {
processRefToExternalSecurityScheme(file + securityScheme.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 processRefToExternalParameter.
public String processRefToExternalParameter(String $ref, RefFormat refFormat) {
String renamedRef = cache.getRenamedRef($ref);
if (renamedRef != null) {
return renamedRef;
}
final Parameter parameter = cache.loadRef($ref, refFormat, Parameter.class);
if (parameter == 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, Parameter> parameters = openAPI.getComponents().getParameters();
if (parameters == null) {
parameters = new LinkedHashMap<>();
}
final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
Parameter existingParameters = parameters.get(possiblyConflictingDefinitionName);
if (existingParameters != null) {
LOGGER.debug("A model for " + existingParameters + " already exists");
if (existingParameters.get$ref() != null) {
// use the new model
existingParameters = null;
}
}
newRef = possiblyConflictingDefinitionName;
cache.putRenamedRef($ref, newRef);
if (existingParameters == null) {
// don't overwrite existing model reference
openAPI.getComponents().addParameters(newRef, parameter);
cache.addReferencedKey(newRef);
String file = $ref.split("#/")[0];
if (parameter.get$ref() != null) {
RefFormat format = computeRefFormat(parameter.get$ref());
if (isAnExternalRefFormat(format)) {
parameter.set$ref(processRefToExternalParameter(parameter.get$ref(), format));
} else {
processRefToExternalParameter(file + parameter.get$ref(), RefFormat.RELATIVE);
}
}
}
if (parameter != null) {
if (parameter.getContent() != null) {
processRefContent(parameter.getContent(), $ref);
}
if (parameter.getSchema() != null) {
processRefSchemaObject(parameter.getSchema(), $ref);
}
}
return newRef;
}
use of io.swagger.v3.parser.models.RefFormat in project swagger-parser by swagger-api.
the class ExternalRefProcessor method processRefToExternalRequestBody.
public String processRefToExternalRequestBody(String $ref, RefFormat refFormat) {
String renamedRef = cache.getRenamedRef($ref);
if (renamedRef != null) {
return renamedRef;
}
final RequestBody body = cache.loadRef($ref, refFormat, RequestBody.class);
if (body == 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, RequestBody> bodies = openAPI.getComponents().getRequestBodies();
if (bodies == null) {
bodies = new LinkedHashMap<>();
}
final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
RequestBody existingBody = bodies.get(possiblyConflictingDefinitionName);
if (existingBody != null) {
LOGGER.debug("A model for " + existingBody + " already exists");
if (existingBody.get$ref() != null) {
// use the new model
existingBody = null;
}
}
newRef = possiblyConflictingDefinitionName;
cache.putRenamedRef($ref, newRef);
if (existingBody == null) {
// don't overwrite existing model reference
openAPI.getComponents().addRequestBodies(newRef, body);
cache.addReferencedKey(newRef);
String file = $ref.split("#/")[0];
if (body.get$ref() != null) {
RefFormat format = computeRefFormat(body.get$ref());
if (isAnExternalRefFormat(format)) {
body.set$ref(processRefToExternalRequestBody(body.get$ref(), format));
} else {
processRefToExternalRequestBody(file + body.get$ref(), RefFormat.RELATIVE);
}
} else if (body.getContent() != null) {
processRefContent(body.getContent(), $ref);
}
}
return newRef;
}
use of io.swagger.v3.parser.models.RefFormat in project swagger-parser by swagger-api.
the class ExternalRefProcessor method processRefToExternalHeader.
public String processRefToExternalHeader(String $ref, RefFormat refFormat) {
String renamedRef = cache.getRenamedRef($ref);
if (renamedRef != null) {
return renamedRef;
}
final Header header = cache.loadRef($ref, refFormat, Header.class);
if (header == 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, Header> headers = openAPI.getComponents().getHeaders();
if (headers == null) {
headers = new LinkedHashMap<>();
}
final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
Header existingHeader = headers.get(possiblyConflictingDefinitionName);
if (existingHeader != null) {
LOGGER.debug("A model for " + existingHeader + " already exists");
if (existingHeader.get$ref() != null) {
// use the new model
existingHeader = null;
}
}
newRef = possiblyConflictingDefinitionName;
cache.putRenamedRef($ref, newRef);
if (existingHeader == null) {
// don't overwrite existing model reference
openAPI.getComponents().addHeaders(newRef, header);
cache.addReferencedKey(newRef);
String file = $ref.split("#/")[0];
if (header.get$ref() != null) {
RefFormat format = computeRefFormat(header.get$ref());
if (isAnExternalRefFormat(format)) {
header.set$ref(processRefToExternalHeader(header.get$ref(), format));
} else {
processRefToExternalHeader(file + header.get$ref(), RefFormat.RELATIVE);
}
}
}
if (header != null) {
if (header.getContent() != null) {
processRefContent(header.getContent(), $ref);
}
if (header.getSchema() != null) {
processRefSchemaObject(header.getSchema(), $ref);
}
}
return newRef;
}
use of io.swagger.v3.parser.models.RefFormat in project swagger-parser by swagger-api.
the class ExternalRefProcessor method processRefToExternalPathItem.
public PathItem processRefToExternalPathItem(String $ref, RefFormat refFormat) {
final PathItem pathItem = cache.loadRef($ref, refFormat, PathItem.class);
String newRef;
Map<String, PathItem> paths = openAPI.getPaths();
if (paths == null) {
paths = new LinkedHashMap<>();
}
final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
PathItem existingPathItem = paths.get(possiblyConflictingDefinitionName);
if (existingPathItem != null) {
LOGGER.debug("A model for " + existingPathItem + " already exists");
if (existingPathItem.get$ref() != null) {
// use the new model
existingPathItem = null;
}
}
newRef = possiblyConflictingDefinitionName;
cache.putRenamedRef($ref, newRef);
if (pathItem != null) {
if (pathItem.readOperationsMap() != null) {
final Map<PathItem.HttpMethod, Operation> operationMap = pathItem.readOperationsMap();
for (PathItem.HttpMethod httpMethod : operationMap.keySet()) {
Operation operation = operationMap.get(httpMethod);
if (operation.getResponses() != null) {
final Map<String, ApiResponse> responses = operation.getResponses();
if (responses != null) {
for (String responseCode : responses.keySet()) {
ApiResponse response = responses.get(responseCode);
if (response != null) {
Schema schema = null;
if (response.getContent() != null) {
Map<String, MediaType> content = response.getContent();
for (String mediaName : content.keySet()) {
MediaType mediaType = content.get(mediaName);
if (mediaType.getSchema() != null) {
schema = mediaType.getSchema();
if (schema != null) {
processRefSchemaObject(mediaType.getSchema(), $ref);
}
if (mediaType.getExamples() != null) {
processRefExamples(mediaType.getExamples(), $ref);
}
}
}
}
if (response.getLinks() != null) {
processRefLinks(response.getLinks(), $ref);
}
}
}
}
}
if (operation.getRequestBody() != null) {
RequestBody body = operation.getRequestBody();
if (body.getContent() != null) {
Schema schema;
Map<String, MediaType> content = body.getContent();
for (String mediaName : content.keySet()) {
MediaType mediaType = content.get(mediaName);
if (mediaType.getSchema() != null) {
schema = mediaType.getSchema();
if (schema != null) {
processRefSchemaObject(mediaType.getSchema(), $ref);
}
}
}
}
}
final List<Parameter> parameters = operation.getParameters();
if (parameters != null) {
parameters.stream().filter(parameter -> parameter.getSchema() != null).forEach(parameter -> this.processRefSchemaObject(parameter.getSchema(), $ref));
}
}
}
}
return pathItem;
}
Aggregations