Search in sources :

Example 11 with RefFormat

use of io.swagger.v3.parser.models.RefFormat in project swagger-parser by swagger-api.

the class ExternalRefProcessor method processRefExample.

private void processRefExample(Example example, String externalFile) {
    RefFormat format = computeRefFormat(example.get$ref());
    if (!isAnExternalRefFormat(format)) {
        example.set$ref(RefType.SCHEMAS.getInternalPrefix() + processRefToExternalSchema(externalFile + example.get$ref(), RefFormat.RELATIVE));
        return;
    }
    String $ref = example.get$ref();
    String subRefExternalPath = getExternalPath(example.get$ref()).orElse(null);
    if (format.equals(RefFormat.RELATIVE) && !Objects.equals(subRefExternalPath, externalFile)) {
        $ref = join(externalFile, example.get$ref());
        example.set$ref($ref);
    } else {
        processRefToExternalExample($ref, format);
    }
}
Also used : 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 12 with RefFormat

use of io.swagger.v3.parser.models.RefFormat in project swagger-parser by swagger-api.

the class LinkProcessor method processLink.

public void processLink(Link link) {
    if (link.get$ref() != null) {
        RefFormat refFormat = computeRefFormat(link.get$ref());
        String $ref = link.get$ref();
        if (isAnExternalRefFormat(refFormat)) {
            final String newRef = externalRefProcessor.processRefToExternalLink($ref, refFormat);
            if (newRef != null) {
                link.set$ref(newRef);
            }
        }
    } else if (link.getHeaders() != null) {
        Map<String, Header> headers = link.getHeaders();
        for (String headerName : headers.keySet()) {
            Header header = headers.get(headerName);
            headerProcessor.processHeader(header);
        }
    }
}
Also used : Header(io.swagger.v3.oas.models.headers.Header) RefFormat(io.swagger.v3.parser.models.RefFormat) RefUtils.isAnExternalRefFormat(io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat) RefUtils.computeRefFormat(io.swagger.v3.parser.util.RefUtils.computeRefFormat) Map(java.util.Map)

Example 13 with RefFormat

use of io.swagger.v3.parser.models.RefFormat 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);
                        }
                    }
                }
            }
        }
    }
}
Also used : RefUtils.isAnExternalRefFormat(io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat) RefUtils.computeRefFormat(io.swagger.v3.parser.util.RefUtils.computeRefFormat) RefFormat(io.swagger.v3.parser.models.RefFormat) Operation(io.swagger.v3.oas.models.Operation) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) PathItem(io.swagger.v3.oas.models.PathItem) Callback(io.swagger.v3.oas.models.callbacks.Callback) Parameter(io.swagger.v3.oas.models.parameters.Parameter) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody)

Example 14 with RefFormat

use of io.swagger.v3.parser.models.RefFormat in project swagger-parser by swagger-api.

the class ParameterProcessor method processParameter.

public void processParameter(Parameter parameter) {
    String $ref = parameter.get$ref();
    if ($ref != null) {
        RefFormat refFormat = computeRefFormat(parameter.get$ref());
        if (isAnExternalRefFormat(refFormat)) {
            final String newRef = externalRefProcessor.processRefToExternalParameter($ref, refFormat);
            if (newRef != null) {
                parameter.set$ref(newRef);
            }
        }
    }
    if (parameter.getSchema() != null) {
        schemaProcessor.processSchema(parameter.getSchema());
    }
    if (parameter.getExamples() != null) {
        Map<String, Example> examples = parameter.getExamples();
        for (String exampleName : examples.keySet()) {
            final Example example = examples.get(exampleName);
            exampleProcessor.processExample(example);
        }
    }
    Schema schema = null;
    if (parameter.getContent() != null) {
        Map<String, MediaType> content = parameter.getContent();
        for (String mediaName : content.keySet()) {
            MediaType mediaType = content.get(mediaName);
            if (mediaType.getSchema() != null) {
                schema = mediaType.getSchema();
                if (schema != null) {
                    schemaProcessor.processSchema(schema);
                }
            }
        }
    }
}
Also used : Example(io.swagger.v3.oas.models.examples.Example) Schema(io.swagger.v3.oas.models.media.Schema) RefUtils.isAnExternalRefFormat(io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat) RefUtils.computeRefFormat(io.swagger.v3.parser.util.RefUtils.computeRefFormat) RefFormat(io.swagger.v3.parser.models.RefFormat) MediaType(io.swagger.v3.oas.models.media.MediaType)

Example 15 with RefFormat

use of io.swagger.v3.parser.models.RefFormat in project swagger-parser by swagger-api.

the class SchemaProcessor method processReferenceSchema.

private void processReferenceSchema(Schema schema) {
    /* if this is a URL or relative ref:
        1) we need to load it into memory.
        2) shove it into the #/components/schemas
        3) update the RefModel to point to its location in #/components/schemas
     */
    RefFormat refFormat = computeRefFormat(schema.get$ref());
    String $ref = schema.get$ref();
    if (isAnExternalRefFormat(refFormat)) {
        final String newRef = externalRefProcessor.processRefToExternalSchema($ref, refFormat);
        if (newRef != null) {
            schema.set$ref(RefType.SCHEMAS.getInternalPrefix() + newRef);
        }
    }
}
Also used : RefUtils.isAnExternalRefFormat(io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat) RefUtils.computeRefFormat(io.swagger.v3.parser.util.RefUtils.computeRefFormat) RefFormat(io.swagger.v3.parser.models.RefFormat)

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