use of io.swagger.v3.oas.annotations.media.Schema in project swagger-parser by swagger-api.
the class InlineModelResolver method createModelFromProperty.
public Schema createModelFromProperty(Schema schema, String path) {
String description = schema.getDescription();
String example = null;
List<String> requiredList = schema.getRequired();
Object obj = schema.getExample();
if (obj != null) {
example = obj.toString();
}
String name = schema.getName();
XML xml = schema.getXml();
Map<String, Schema> properties = schema.getProperties();
if (schema instanceof ComposedSchema && this.flattenComposedSchemas) {
ComposedSchema composedModel = (ComposedSchema) schema;
composedModel.setDescription(description);
composedModel.setExample(example);
composedModel.setName(name);
composedModel.setXml(xml);
composedModel.setType(schema.getType());
composedModel.setRequired(requiredList);
return composedModel;
} else {
Schema model = new Schema();
model.setAdditionalProperties(schema.getAdditionalProperties());
model.setDescription(description);
model.setDeprecated(schema.getDeprecated());
model.setDiscriminator(schema.getDiscriminator());
model.setEnum(schema.getEnum());
model.setExample(example);
model.setExclusiveMaximum(schema.getExclusiveMaximum());
model.setExclusiveMinimum(schema.getExclusiveMinimum());
model.setFormat(schema.getFormat());
model.setMinLength(schema.getMinLength());
model.setMaximum(schema.getMaximum());
model.setMaxItems(schema.getMaxItems());
model.setMaxProperties(schema.getMaxProperties());
model.setMaxLength(schema.getMaxLength());
model.setMinimum(schema.getMinimum());
model.setMinItems(schema.getMinItems());
model.setMinLength(schema.getMinLength());
model.setMinProperties(schema.getMinProperties());
model.setMultipleOf(schema.getMultipleOf());
model.setName(name);
model.setNullable(schema.getNullable());
model.setNot(schema.getNot());
model.setPattern(schema.getPattern());
model.setReadOnly(schema.getReadOnly());
model.setRequired(requiredList);
model.setUniqueItems(schema.getUniqueItems());
model.setTitle(schema.getTitle());
model.setType(schema.getType());
model.setXml(xml);
model.setWriteOnly(schema.getWriteOnly());
if (properties != null) {
flattenProperties(properties, path);
model.setProperties(properties);
}
return model;
}
}
use of io.swagger.v3.oas.annotations.media.Schema in project swagger-parser by swagger-api.
the class InlineModelResolver method flattenMapSchema.
private void flattenMapSchema(Schema innerProperty, String key, String pathname, Schema mediaSchema) {
if (innerProperty.getProperties() != null && innerProperty.getProperties().size() > 0) {
flattenProperties(innerProperty.getProperties(), pathname);
String modelName = resolveModelName(innerProperty.getTitle(), key);
String existing = matchGenerated(innerProperty);
if (existing != null) {
mediaSchema.setAdditionalProperties(new Schema().$ref(existing));
} else {
mediaSchema.setAdditionalProperties(new Schema().$ref(modelName));
addGenerated(modelName, innerProperty);
openAPI.getComponents().addSchemas(modelName, innerProperty);
}
} else if (innerProperty instanceof ComposedSchema && this.flattenComposedSchemas) {
flattenComposedSchema(innerProperty, key);
if (innerProperty.get$ref() == null) {
String modelName = resolveModelName(innerProperty.getTitle(), key);
mediaSchema.setAdditionalProperties(new Schema().$ref(modelName));
addGenerated(modelName, innerProperty);
openAPI.getComponents().addSchemas(modelName, innerProperty);
}
}
}
use of io.swagger.v3.oas.annotations.media.Schema 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.oas.annotations.media.Schema 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;
}
use of io.swagger.v3.oas.annotations.media.Schema in project swagger-parser by swagger-api.
the class OperationProcessor method processOperation.
public void processOperation(Operation operation) {
final List<Parameter> processedOperationParameters = parameterProcessor.processParameters(operation.getParameters());
if (processedOperationParameters != null) {
operation.setParameters(processedOperationParameters);
}
final RequestBody requestBody = operation.getRequestBody();
if (requestBody != null) {
requestBodyProcessor.processRequestBody(requestBody);
}
final Map<String, ApiResponse> responses = operation.getResponses();
if (responses != null) {
for (String responseCode : responses.keySet()) {
ApiResponse response = responses.get(responseCode);
if (response != null) {
// This part allows parser to put response schema inline without the resolveFully option set to true
if (response.get$ref() != null) {
responseProcessor.processResponse(response);
RefFormat refFormat = computeRefFormat(response.get$ref());
ApiResponse resolvedResponse = cache.loadRef(response.get$ref(), refFormat, ApiResponse.class);
if (resolvedResponse != null) {
response = resolvedResponse;
responses.put(responseCode, resolvedResponse);
}
}
responseProcessor.processResponse(response);
}
}
}
final Map<String, Callback> callbacks = operation.getCallbacks();
if (callbacks != null) {
for (String name : callbacks.keySet()) {
Callback callback = callbacks.get(name);
if (callback != null) {
if (callback.get$ref() != null) {
String $ref = callback.get$ref();
RefFormat refFormat = computeRefFormat($ref);
if (isAnExternalRefFormat(refFormat)) {
final String newRef = externalRefProcessor.processRefToExternalCallback($ref, refFormat);
if (newRef != null) {
callback.set$ref(newRef);
}
}
}
for (String callbackName : callback.keySet()) {
PathItem pathItem = callback.get(callbackName);
final Map<PathItem.HttpMethod, Operation> operationMap = pathItem.readOperationsMap();
for (PathItem.HttpMethod httpMethod : operationMap.keySet()) {
Operation op = operationMap.get(httpMethod);
processOperation(op);
}
List<Parameter> parameters = pathItem.getParameters();
if (parameters != null) {
for (Parameter parameter : parameters) {
parameterProcessor.processParameter(parameter);
}
}
}
}
}
}
}
Aggregations