Search in sources :

Example 16 with In

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;
}
Also used : RefPath(io.swagger.models.RefPath) Path(io.swagger.models.Path) RefModel(io.swagger.models.RefModel) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) FileSchema(io.swagger.v3.oas.models.media.FileSchema) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) PathItem(io.swagger.v3.oas.models.PathItem) Path(io.swagger.models.Path) Swagger(io.swagger.models.Swagger) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ComposedModel(io.swagger.models.ComposedModel) ArrayModel(io.swagger.models.ArrayModel) Paths(io.swagger.v3.oas.models.Paths) OpenAPI(io.swagger.v3.oas.models.OpenAPI)

Example 17 with In

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);
                }
            }
        }
    }
}
Also used : Schema(io.swagger.v3.oas.models.media.Schema) Map(java.util.Map)

Example 18 with In

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());
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 19 with In

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);
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 20 with In

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);
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)130 OpenAPI (io.swagger.v3.oas.models.OpenAPI)108 Parameter (io.swagger.v3.oas.models.parameters.Parameter)51 Schema (io.swagger.v3.oas.models.media.Schema)49 StringSchema (io.swagger.v3.oas.models.media.StringSchema)44 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)40 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)39 QueryParameter (io.swagger.v3.oas.models.parameters.QueryParameter)39 Operation (io.swagger.v3.oas.annotations.Operation)36 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)36 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)31 Operation (io.swagger.v3.oas.models.Operation)28 PathItem (io.swagger.v3.oas.models.PathItem)27 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)27 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)25 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)25 Map (java.util.Map)25 HashMap (java.util.HashMap)23 PathParameter (io.swagger.v3.oas.models.parameters.PathParameter)22 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)21