use of io.swagger.v3.oas.models.security.SecurityScheme.In in project swagger-parser by swagger-api.
the class SwaggerConverter method convert.
public SwaggerParseResult convert(SwaggerDeserializationResult parse) {
if (parse == null) {
return null;
}
SwaggerParseResult output = new SwaggerParseResult().messages(parse.getMessages());
if (parse.getSwagger() == null) {
return output;
}
OpenAPI openAPI = new OpenAPI();
SwaggerInventory inventory = new SwaggerInventory().process(parse.getSwagger());
Swagger swagger = parse.getSwagger();
if (swagger.getVendorExtensions() != null) {
openAPI.setExtensions(convert(swagger.getVendorExtensions()));
}
// Set extension to retain original version of OAS document.
openAPI.addExtension("x-original-swagger-version", swagger.getSwagger());
if (swagger.getExternalDocs() != null) {
openAPI.setExternalDocs(convert(swagger.getExternalDocs()));
}
if (swagger.getInfo() != null) {
openAPI.setInfo(convert(swagger.getInfo()));
}
openAPI.setServers(convert(swagger.getSchemes(), swagger.getHost(), swagger.getBasePath()));
if (swagger.getTags() != null) {
openAPI.setTags(convertTags(swagger.getTags()));
}
if (swagger.getConsumes() != null) {
this.globalConsumes.addAll(swagger.getConsumes());
}
if (swagger.getProduces() != null) {
this.globalProduces.addAll(swagger.getProduces());
}
if (swagger.getSecurity() != null && swagger.getSecurity().size() > 0) {
openAPI.setSecurity(convertSecurityRequirements(swagger.getSecurity()));
}
List<Model> models = inventory.getModels();
// TODO until we have the example object working correctly in v3 pojos...
for (Model model : models) {
if (model instanceof RefModel) {
RefModel ref = (RefModel) model;
if (ref.get$ref().indexOf("#/definitions") == 0) {
String updatedRef = "#/components/schemas" + ref.get$ref().substring("#/definitions".length());
ref.set$ref(updatedRef);
}
}
}
for (Property property : inventory.getProperties()) {
if (property instanceof RefProperty) {
RefProperty ref = (RefProperty) property;
if (ref.get$ref().indexOf("#/definitions") == 0) {
String updatedRef = "#/components/schemas" + ref.get$ref().substring("#/definitions".length());
ref.set$ref(updatedRef);
}
}
if (property instanceof ComposedProperty) {
ComposedProperty comprop = (ComposedProperty) property;
if (comprop.getAllOf() != null) {
for (Property item : comprop.getAllOf()) {
if (item instanceof RefProperty) {
RefProperty ref = (RefProperty) item;
if (ref.get$ref().indexOf("#/definitions") == 0) {
String updatedRef = "#/components/schemas" + ref.get$ref().substring("#/definitions".length());
ref.set$ref(updatedRef);
}
}
}
}
}
}
if (swagger.getParameters() != null) {
globalV2Parameters.putAll(swagger.getParameters());
swagger.getParameters().forEach((k, v) -> {
if ("body".equals(v.getIn())) {
components.addRequestBodies(k, convertParameterToRequestBody(v));
} else if ("formData".equals(v.getIn())) {
// formData_ is added not to overwrite existing schemas
components.addSchemas("formData_" + k, convertFormDataToSchema(v));
} else {
components.addParameters(k, convert(v));
}
});
}
Paths v3Paths = new Paths();
Map<String, Path> pathMap = Optional.ofNullable(swagger.getPaths()).orElse(new HashMap<>());
for (String pathname : pathMap.keySet()) {
io.swagger.models.Path v2Path = swagger.getPath(pathname);
PathItem v3Path = convert(v2Path);
v3Paths.put(pathname, v3Path);
}
openAPI.setPaths(v3Paths);
if (swagger.getResponses() != null) {
swagger.getResponses().forEach((k, v) -> components.addResponses(k, convert(v)));
}
if (swagger.getDefinitions() != null) {
for (String key : swagger.getDefinitions().keySet()) {
Model model = swagger.getDefinitions().get(key);
Schema schema = convert(model);
components.addSchemas(key, schema);
}
}
if (swagger.getSecurityDefinitions() != null) {
swagger.getSecurityDefinitions().forEach((k, v) -> components.addSecuritySchemes(k, convert(v)));
}
openAPI.setComponents(components);
output.setOpenAPI(openAPI);
return output;
}
use of io.swagger.v3.oas.models.security.SecurityScheme.In in project swagger-parser by swagger-api.
the class ComponentsProcessor method processSchemas.
public void processSchemas(Set<String> schemaKeys, Map<String, Schema> schemas) {
schemaKeys.addAll(schemas.keySet());
for (String modelName : schemaKeys) {
final Schema model = schemas.get(modelName);
String originalRef = model.get$ref() != null ? model.get$ref() : null;
schemaProcessor.processSchema(model);
// if we process a RefModel here, in the #/components/schemas table, we want to overwrite it with the referenced value
if (model.get$ref() != null) {
final String renamedRef = cache.getRenamedRef(originalRef);
if (renamedRef != null) {
// we definitely resolved the referenced and shoved it in the components map
// because the referenced model may be in the components map, we need to remove old instances
final Schema resolvedModel = schemas.get(renamedRef);
// ensure the reference isn't still in use
if (!cache.hasReferencedKey(renamedRef)) {
schemas.remove(renamedRef);
}
// add the new key
schemas.put(modelName, resolvedModel);
}
}
}
// process schemas again to check properties that hasn't been solved
for (String modelName : schemaKeys) {
final Schema model = schemas.get(modelName);
Map<String, Schema> properties = model.getProperties();
if (properties != null) {
for (Map.Entry<String, Schema> propertyEntry : properties.entrySet()) {
Schema property = propertyEntry.getValue();
if (property.get$ref() != null) {
schemaProcessor.processSchema(model);
}
}
}
}
}
use of io.swagger.v3.oas.models.security.SecurityScheme.In in project swagger-parser by swagger-api.
the class V2ConverterTest method testIssue4.
@Test(description = "Contents in Responses")
public void testIssue4() throws Exception {
OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_4_JSON);
assertNotNull(oas.getPaths().get(PETS_PATH).getGet().getResponses().get("200").getContent());
}
use of io.swagger.v3.oas.models.security.SecurityScheme.In in project swagger-parser by swagger-api.
the class V2ConverterTest method testIssue762.
@Test(description = "OpenAPI v2 Converter: NPE when type is array and 'items' field is missing in array property")
public void testIssue762() throws Exception {
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_762_JSON);
assertNotNull(oas);
}
use of io.swagger.v3.oas.models.security.SecurityScheme.In in project swagger-parser by swagger-api.
the class V2ConverterTest method testIssue756.
@Test(description = "OpenAPI v2 converter - no model in body parameter")
public void testIssue756() throws Exception {
OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_756_JSON);
assertNotNull(oas);
}
Aggregations