Search in sources :

Example 1 with ApiResponse

use of io.swagger.v3.oas.annotations.responses.ApiResponse in project swagger-parser by swagger-api.

the class OpenAPIDeserializer method getResponse.

public ApiResponse getResponse(ObjectNode node, String location, ParseResult result) {
    if (node == null) {
        return null;
    }
    ApiResponse apiResponse = new ApiResponse();
    JsonNode ref = node.get("$ref");
    if (ref != null) {
        if (ref.getNodeType().equals(JsonNodeType.STRING)) {
            String mungedRef = mungedRef(ref.textValue());
            if (mungedRef != null) {
                apiResponse.set$ref(mungedRef);
            } else {
                apiResponse.set$ref(ref.textValue());
            }
            return apiResponse;
        } else {
            result.invalidType(location, "$ref", "string", node);
            return null;
        }
    }
    String value = getString("description", node, true, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        apiResponse.description(value);
    }
    ObjectNode headerObject = getObject("headers", node, false, location, result);
    if (headerObject != null) {
        Map<String, Header> headers = getHeaders(headerObject, location, result, false);
        if (headers != null && headers.size() > 0) {
            apiResponse.setHeaders(headers);
        }
    }
    ObjectNode linksObj = getObject("links", node, false, location, result);
    if (linksObj != null) {
        Map<String, Link> links = getLinks(linksObj, location, result, false);
        if (links != null && links.size() > 0) {
            apiResponse.setLinks(links);
        }
    }
    ObjectNode contentObject = getObject("content", node, false, location, result);
    if (contentObject != null) {
        apiResponse.setContent(getContent(contentObject, String.format("%s.%s", location, "content"), result));
    }
    Map<String, Object> extensions = getExtensions(node);
    if (extensions != null && extensions.size() > 0) {
        apiResponse.setExtensions(extensions);
    }
    Set<String> keys = getKeys(node);
    for (String key : keys) {
        if (!RESPONSE_KEYS.contains(key) && !key.startsWith("x-")) {
            result.extra(location, key, node.get(key));
        }
    }
    return apiResponse;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Header(io.swagger.v3.oas.models.headers.Header) JsonNode(com.fasterxml.jackson.databind.JsonNode) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Link(io.swagger.v3.oas.models.links.Link)

Example 2 with ApiResponse

use of io.swagger.v3.oas.annotations.responses.ApiResponse in project swagger-parser by swagger-api.

the class NetworkReferenceTest method testIssue411.

@Test
public void testIssue411() throws Exception {
    final List<AuthorizationValue> auths = new ArrayList<>();
    AuthorizationValue auth = new AuthorizationValue("Authorization", "OMG_SO_SEKR3T", "header");
    auths.add(auth);
    new Expectations() {

        {
            remoteUrl.urlToString("http://remote1/resources/swagger.yaml", auths);
            result = issue_411_server;
            remoteUrl.urlToString("http://remote2/resources/foo", auths);
            result = issue_411_components;
        }
    };
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult result = parser.readLocation("http://remote1/resources/swagger.yaml", auths, options);
    OpenAPI swagger = result.getOpenAPI();
    assertNotNull(swagger.getPaths().get("/health"));
    PathItem health = swagger.getPaths().get("/health");
    assertTrue(health.getGet().getParameters().size() == 0);
    Schema responseRef = health.getGet().getResponses().get("200").getContent().get("*/*").getSchema();
    assertTrue(responseRef.get$ref() != null);
    assertEquals(responseRef.get$ref(), "#/components/schemas/Success");
    assertNotNull(swagger.getComponents().getSchemas().get("Success"));
    Parameter param = swagger.getPaths().get("/stuff").getGet().getParameters().get(0);
    assertEquals(param.getIn(), "query");
    assertEquals(param.getName(), "skip");
    ApiResponse response = swagger.getPaths().get("/stuff").getGet().getResponses().get("200");
    assertNotNull(response);
    assertTrue(response.getContent().get("*/*").getSchema() instanceof StringSchema);
    ApiResponse error = swagger.getPaths().get("/stuff").getGet().getResponses().get("400");
    assertNotNull(error);
    Schema errorProp = error.getContent().get("*/*").getSchema();
    assertNotNull(errorProp);
    assertTrue(errorProp.get$ref() != null);
    assertEquals(errorProp.get$ref(), "#/components/schemas/Error");
    assertTrue(swagger.getComponents().getSchemas().get("Error") instanceof Schema);
}
Also used : Expectations(mockit.Expectations) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Schema(io.swagger.v3.oas.models.media.Schema) ArrayList(java.util.ArrayList) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) AuthorizationValue(io.swagger.v3.parser.core.models.AuthorizationValue) PathItem(io.swagger.v3.oas.models.PathItem) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) Parameter(io.swagger.v3.oas.models.parameters.Parameter) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 3 with ApiResponse

use of io.swagger.v3.oas.annotations.responses.ApiResponse in project swagger-parser by swagger-api.

the class ResolverFully method resolvePath.

public void resolvePath(PathItem pathItem) {
    for (Operation op : pathItem.readOperations()) {
        // inputs
        if (op.getParameters() != null) {
            for (Parameter parameter : op.getParameters()) {
                parameter = parameter.get$ref() != null ? resolveParameter(parameter) : parameter;
                if (parameter.getSchema() != null) {
                    Schema resolved = resolveSchema(parameter.getSchema());
                    if (resolved != null) {
                        parameter.setSchema(resolved);
                    }
                }
                if (parameter.getContent() != null) {
                    Map<String, MediaType> content = parameter.getContent();
                    for (String key : content.keySet()) {
                        if (content.get(key) != null && content.get(key).getSchema() != null) {
                            Schema resolvedSchema = resolveSchema(content.get(key).getSchema());
                            if (resolvedSchema != null) {
                                content.get(key).setSchema(resolvedSchema);
                            }
                        }
                    }
                }
            }
        }
        if (op.getCallbacks() != null) {
            Map<String, Callback> callbacks = op.getCallbacks();
            for (String name : callbacks.keySet()) {
                Callback callback = callbacks.get(name);
                callback = callback.get$ref() != null ? resolveCallback(callback) : callback;
                if (callback != null) {
                    for (String callbackName : callback.keySet()) {
                        PathItem path = callback.get(callbackName);
                        if (path != null) {
                            resolvePath(path);
                        }
                    }
                }
                op.getCallbacks().put(name, callback);
            }
        }
        RequestBody refRequestBody = op.getRequestBody();
        if (refRequestBody != null) {
            RequestBody requestBody = refRequestBody.get$ref() != null ? resolveRequestBody(refRequestBody) : refRequestBody;
            op.setRequestBody(requestBody);
            if (requestBody.getContent() != null) {
                Map<String, MediaType> content = requestBody.getContent();
                for (String key : content.keySet()) {
                    if (content.get(key) != null && content.get(key).getSchema() != null) {
                        Schema resolved = resolveSchema(content.get(key).getSchema());
                        if (resolved != null) {
                            content.get(key).setSchema(resolved);
                        }
                    }
                }
            }
        }
        // responses
        ApiResponses responses = op.getResponses();
        if (responses != null) {
            for (String code : responses.keySet()) {
                ApiResponse response = responses.get(code);
                if (response.getContent() != null) {
                    Map<String, MediaType> content = response.getContent();
                    for (String mediaType : content.keySet()) {
                        if (content.get(mediaType).getSchema() != null) {
                            Schema resolved = resolveSchema(content.get(mediaType).getSchema());
                            response.getContent().get(mediaType).setSchema(resolved);
                        }
                        if (content.get(mediaType).getExamples() != null) {
                            Map<String, Example> resolved = resolveExample(content.get(mediaType).getExamples());
                            response.getContent().get(mediaType).setExamples(resolved);
                        }
                    }
                }
                resolveHeaders(response.getHeaders());
                Map<String, Link> links = response.getLinks();
                if (links != null) {
                    for (Map.Entry<String, Link> link : links.entrySet()) {
                        Link value = link.getValue();
                        Link resolvedValue = value.get$ref() != null ? resolveLink(value) : value;
                        link.setValue(resolvedValue);
                    }
                }
            }
        }
    }
}
Also used : 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) Example(io.swagger.v3.oas.models.examples.Example) Parameter(io.swagger.v3.oas.models.parameters.Parameter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) Link(io.swagger.v3.oas.models.links.Link) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody)

Example 4 with ApiResponse

use of io.swagger.v3.oas.annotations.responses.ApiResponse in project swagger-parser by swagger-api.

the class ExternalRefProcessor method processRefToExternalResponse.

public String processRefToExternalResponse(String $ref, RefFormat refFormat) {
    String renamedRef = cache.getRenamedRef($ref);
    if (renamedRef != null) {
        return renamedRef;
    }
    final ApiResponse response = cache.loadRef($ref, refFormat, ApiResponse.class);
    String newRef;
    if (openAPI.getComponents() == null) {
        openAPI.setComponents(new Components());
    }
    Map<String, ApiResponse> responses = openAPI.getComponents().getResponses();
    if (responses == null) {
        responses = new LinkedHashMap<>();
    }
    final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
    ApiResponse existingResponse = responses.get(possiblyConflictingDefinitionName);
    if (existingResponse != null) {
        LOGGER.debug("A model for " + existingResponse + " already exists");
        if (existingResponse.get$ref() != null) {
            // use the new model
            existingResponse = null;
        }
    }
    newRef = possiblyConflictingDefinitionName;
    openAPI.getComponents().addResponses(newRef, response);
    cache.putRenamedRef($ref, newRef);
    if (response != null) {
        if (response.getContent() != null) {
            processRefContent(response.getContent(), $ref);
        }
        if (response.getHeaders() != null) {
            processRefHeaders(response.getHeaders(), $ref);
        }
        if (response.getLinks() != null) {
            processRefLinks(response.getLinks(), $ref);
        }
    }
    return newRef;
}
Also used : Components(io.swagger.v3.oas.models.Components) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse)

Example 5 with ApiResponse

use of io.swagger.v3.oas.annotations.responses.ApiResponse 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)

Aggregations

Operation (io.swagger.v3.oas.annotations.Operation)113 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)99 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)84 Test (org.testng.annotations.Test)53 Operation (io.swagger.v3.oas.models.Operation)50 ProtectedApi (org.gluu.oxtrust.service.filter.ProtectedApi)48 OpenAPI (io.swagger.v3.oas.models.OpenAPI)47 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)43 PathItem (io.swagger.v3.oas.models.PathItem)39 Schema (io.swagger.v3.oas.models.media.Schema)38 MediaType (io.swagger.v3.oas.models.media.MediaType)31 Path (javax.ws.rs.Path)30 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)29 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)27 StringSchema (io.swagger.v3.oas.models.media.StringSchema)27 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)25 Content (io.swagger.v3.oas.models.media.Content)25 ArrayList (java.util.ArrayList)24 Components (io.swagger.v3.oas.models.Components)19 Parameter (io.swagger.v3.oas.models.parameters.Parameter)18