Search in sources :

Example 21 with RefFormat

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;
}
Also used : Components(io.swagger.v3.oas.models.Components) RefUtils.computeRefFormat(io.swagger.v3.parser.util.RefUtils.computeRefFormat) RefUtils.isAnExternalRefFormat(io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat) RefFormat(io.swagger.v3.parser.models.RefFormat) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme)

Example 22 with RefFormat

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;
}
Also used : Components(io.swagger.v3.oas.models.Components) RefUtils.computeRefFormat(io.swagger.v3.parser.util.RefUtils.computeRefFormat) RefUtils.isAnExternalRefFormat(io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat) RefFormat(io.swagger.v3.parser.models.RefFormat) Parameter(io.swagger.v3.oas.models.parameters.Parameter)

Example 23 with RefFormat

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;
}
Also used : Components(io.swagger.v3.oas.models.Components) RefUtils.computeRefFormat(io.swagger.v3.parser.util.RefUtils.computeRefFormat) RefUtils.isAnExternalRefFormat(io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat) RefFormat(io.swagger.v3.parser.models.RefFormat) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody)

Example 24 with RefFormat

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;
}
Also used : Components(io.swagger.v3.oas.models.Components) Header(io.swagger.v3.oas.models.headers.Header) RefUtils.computeRefFormat(io.swagger.v3.parser.util.RefUtils.computeRefFormat) RefUtils.isAnExternalRefFormat(io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat) RefFormat(io.swagger.v3.parser.models.RefFormat)

Example 25 with RefFormat

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;
}
Also used : Parameter(io.swagger.v3.oas.models.parameters.Parameter) LoggerFactory(org.slf4j.LoggerFactory) ResolverCache(io.swagger.v3.parser.ResolverCache) Operation(io.swagger.v3.oas.models.Operation) Header(io.swagger.v3.oas.models.headers.Header) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) StringUtils(org.apache.commons.lang3.StringUtils) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) RefUtils.computeDefinitionName(io.swagger.v3.parser.util.RefUtils.computeDefinitionName) RefUtils.computeRefFormat(io.swagger.v3.parser.util.RefUtils.computeRefFormat) LinkedHashMap(java.util.LinkedHashMap) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) RefUtils.getExternalPath(io.swagger.v3.parser.util.RefUtils.getExternalPath) Schema(io.swagger.v3.oas.models.media.Schema) URI(java.net.URI) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Discriminator(io.swagger.v3.oas.models.media.Discriminator) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) Collection(java.util.Collection) PathItem(io.swagger.v3.oas.models.PathItem) MediaType(io.swagger.v3.oas.models.media.MediaType) RefUtils.isAnExternalRefFormat(io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat) Example(io.swagger.v3.oas.models.examples.Example) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Callback(io.swagger.v3.oas.models.callbacks.Callback) File(java.io.File) Objects(java.util.Objects) List(java.util.List) RefFormat(io.swagger.v3.parser.models.RefFormat) RefType(io.swagger.v3.parser.models.RefType) Paths(java.nio.file.Paths) Components(io.swagger.v3.oas.models.Components) Link(io.swagger.v3.oas.models.links.Link) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) Schema(io.swagger.v3.oas.models.media.Schema) Operation(io.swagger.v3.oas.models.Operation) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) PathItem(io.swagger.v3.oas.models.PathItem) MediaType(io.swagger.v3.oas.models.media.MediaType) Parameter(io.swagger.v3.oas.models.parameters.Parameter) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody)

Aggregations

RefFormat (io.swagger.v3.parser.models.RefFormat)38 RefUtils.computeRefFormat (io.swagger.v3.parser.util.RefUtils.computeRefFormat)30 RefUtils.isAnExternalRefFormat (io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat)29 Components (io.swagger.v3.oas.models.Components)12 Schema (io.swagger.v3.oas.models.media.Schema)8 Test (org.testng.annotations.Test)7 ResolverCache (io.swagger.v3.parser.ResolverCache)6 Expectations (mockit.Expectations)6 Example (io.swagger.v3.oas.models.examples.Example)5 PathItem (io.swagger.v3.oas.models.PathItem)4 MediaType (io.swagger.v3.oas.models.media.MediaType)4 Parameter (io.swagger.v3.oas.models.parameters.Parameter)4 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)4 OpenAPI (io.swagger.v3.oas.models.OpenAPI)3 Callback (io.swagger.v3.oas.models.callbacks.Callback)3 Header (io.swagger.v3.oas.models.headers.Header)3 Link (io.swagger.v3.oas.models.links.Link)3 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)3 SecurityScheme (io.swagger.v3.oas.models.security.SecurityScheme)3 Map (java.util.Map)3