Search in sources :

Example 6 with Header

use of io.swagger.v3.oas.annotations.headers.Header 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 7 with Header

use of io.swagger.v3.oas.annotations.headers.Header in project swagger-parser by swagger-api.

the class NetworkReferenceTest method testValidateExternalRefsTrueRemote.

@Test(description = "option true, adds Original Location to messages when ref is remote")
public void testValidateExternalRefsTrueRemote() throws Exception {
    ParseOptions options = new ParseOptions();
    options.setValidateExternalRefs(true);
    options.setResolve(true);
    new Expectations() {

        {
            remoteUrl.urlToString("http://localhost:8080/swos-443/root.yaml", new ArrayList<>());
            result = issue_443_yaml;
            remoteUrl.urlToString("http://localhost:8080/swos-443/ref.yaml", new ArrayList<>());
            result = issue_443_ref_yaml;
        }
    };
    SwaggerParseResult result = new OpenAPIV3Parser().readLocation("http://localhost:8080/swos-443/root.yaml", null, options);
    OpenAPI openAPI = result.getOpenAPI();
    assertNotNull(result.getMessages());
    assertEquals(result.getMessages().size(), 19);
    assertNotNull(openAPI);
    assertTrue(result.getMessages().contains("attribute components.requestBodies.NewItem.asdasd is unexpected (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.requestBodies.NewItem.descasdasdription is unexpected (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.responses.GeneralError.descrsaiption is unexpected (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.responses.GeneralError.asdas is unexpected (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.responses.GeneralError.description is missing (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.schemas.Examples.nonExpected is unexpected (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.parameters.skipParam.[skip].in is not of type `[query|header|path|cookie]` (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.securitySchemes.api_key.namex is unexpected (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.securitySchemes.api_key.name is missing (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.callbacks.webhookVerificationEvent.postx is unexpected (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.headers.X-Rate-Limit-Limit.descriptasdd is unexpected (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.links.unsubscribe.parametersx is unexpected (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.examples.response-example.summaryx is unexpected (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.examples.response-example. value and externalValue are both present (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.callbacks.failed.wrongField is not of type `object` (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute paths.~1refPet(get).responses is missing (./ref.yaml)"));
    // error message in main file
    assertTrue(result.getMessages().contains("attribute components.schemas.InvalidSchema.invalid is unexpected"));
}
Also used : Expectations(mockit.Expectations) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 8 with Header

use of io.swagger.v3.oas.annotations.headers.Header in project swagger-parser by swagger-api.

the class ExternalRefProcessor method processRefHeader.

private void processRefHeader(Header subRef, String externalFile) {
    RefFormat format = computeRefFormat(subRef.get$ref());
    if (!isAnExternalRefFormat(format)) {
        subRef.set$ref(RefType.SCHEMAS.getInternalPrefix() + processRefToExternalSchema(externalFile + subRef.get$ref(), RefFormat.RELATIVE));
        return;
    }
    String $ref = subRef.get$ref();
    String subRefExternalPath = getExternalPath(subRef.get$ref()).orElse(null);
    if (format.equals(RefFormat.RELATIVE) && !Objects.equals(subRefExternalPath, externalFile)) {
        $ref = join(externalFile, subRef.get$ref());
        subRef.set$ref($ref);
    } else {
        processRefToExternalHeader($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 9 with Header

use of io.swagger.v3.oas.annotations.headers.Header 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 10 with Header

use of io.swagger.v3.oas.annotations.headers.Header in project swagger-parser by swagger-api.

the class ResponseProcessor method processResponse.

public void processResponse(ApiResponse response) {
    if (response.get$ref() != null) {
        processReferenceResponse(response);
    }
    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) {
                    schemaProcessor.processSchema(schema);
                }
            }
            if (mediaType.getExamples() != null) {
                for (Example ex : mediaType.getExamples().values()) {
                    exampleProcessor.processExample(ex);
                }
            }
        }
    }
    if (response.getHeaders() != null) {
        Map<String, Header> headers = response.getHeaders();
        for (String headerName : headers.keySet()) {
            Header header = headers.get(headerName);
            headerProcessor.processHeader(header);
        }
    }
    if (response.getLinks() != null) {
        Map<String, Link> links = response.getLinks();
        for (String linkName : links.keySet()) {
            Link link = links.get(linkName);
            linkProcessor.processLink(link);
        }
    }
}
Also used : Header(io.swagger.v3.oas.models.headers.Header) Schema(io.swagger.v3.oas.models.media.Schema) Example(io.swagger.v3.oas.models.examples.Example) MediaType(io.swagger.v3.oas.models.media.MediaType) Link(io.swagger.v3.oas.models.links.Link)

Aggregations

Test (org.testng.annotations.Test)32 OpenAPI (io.swagger.v3.oas.models.OpenAPI)26 Header (io.swagger.v3.oas.models.headers.Header)25 Parameter (io.swagger.v3.oas.models.parameters.Parameter)16 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)13 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)11 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)11 Schema (io.swagger.v3.oas.models.media.Schema)10 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)10 MediaType (io.swagger.v3.oas.models.media.MediaType)8 QueryParameter (io.swagger.v3.oas.models.parameters.QueryParameter)8 Components (io.swagger.v3.oas.models.Components)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 StringSchema (io.swagger.v3.oas.models.media.StringSchema)6 AuthorizationValue (io.swagger.v3.parser.core.models.AuthorizationValue)6 PathItem (io.swagger.v3.oas.models.PathItem)5 Example (io.swagger.v3.oas.models.examples.Example)5 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)5 PathParameter (io.swagger.v3.oas.models.parameters.PathParameter)5 RefFormat (io.swagger.v3.parser.models.RefFormat)5