Search in sources :

Example 51 with Header

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

the class HeaderProcessor method processHeader.

public void processHeader(Header header) {
    if (header.get$ref() != null) {
        RefFormat refFormat = computeRefFormat(header.get$ref());
        String $ref = header.get$ref();
        if (isAnExternalRefFormat(refFormat)) {
            final String newRef = externalRefProcessor.processRefToExternalHeader($ref, refFormat);
            if (newRef != null) {
                header.set$ref(newRef);
            }
        }
    }
    if (header.getSchema() != null) {
        schemaProcessor.processSchema(header.getSchema());
    }
    if (header.getExamples() != null) {
        if (header.getExamples() != null) {
            Map<String, Example> examples = header.getExamples();
            for (String key : examples.keySet()) {
                exampleProcessor.processExample(header.getExamples().get(key));
            }
        }
    }
    Schema schema = null;
    if (header.getContent() != null) {
        Map<String, MediaType> content = header.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) 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) MediaType(io.swagger.v3.oas.models.media.MediaType)

Example 52 with Header

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

use of io.swagger.v3.oas.models.headers.Header in project swagger-core by swagger-api.

the class JsonDeserializationTest method deserializeHeaderWithStyle.

@Test(description = "it should deserialize a Header with style")
public void deserializeHeaderWithStyle() throws IOException {
    final String json = "{\"description\":\"aaaa\",\"style\":\"simple\"}";
    final Header p = m.readValue(json, Header.class);
    SerializationMatchers.assertEqualsToJson(p, json);
}
Also used : Header(io.swagger.v3.oas.models.headers.Header) Test(org.testng.annotations.Test)

Example 54 with Header

use of io.swagger.v3.oas.models.headers.Header in project swagger-core by swagger-api.

the class OpenAPI3_1DeserializationTest method testDeserializationOnOAS31.

@Test
public void testDeserializationOnOAS31() throws IOException {
    final String jsonString = ResourceUtils.loadClassResource(getClass(), "specFiles/3.1.0/petstore-3.1_sample.yaml");
    OpenAPI openAPI = Yaml31.mapper().readValue(jsonString, OpenAPI.class);
    assertNotNull(openAPI);
    assertEquals(openAPI.getInfo().getTitle(), "Swagger Petstore");
    assertEquals(openAPI.getInfo().getVersion(), "1.0.0");
    assertEquals(openAPI.getInfo().getSummary(), "petstore sample for OAS 3.1.0");
    assertEquals(openAPI.getInfo().getLicense().getName(), "MIT");
    assertEquals(openAPI.getInfo().getLicense().getIdentifier(), "test");
    assertNotNull(openAPI.getWebhooks());
    assertFalse(openAPI.getWebhooks().isEmpty());
    assertNotNull(openAPI.getWebhooks().get("newPet"));
    assertNotNull(openAPI.getWebhooks().get("newPet").getPost());
    assertNotNull(openAPI.getComponents().getPathItems());
    assertNotNull(openAPI.getComponents().getPathItems().get("/pet"));
    assertEquals(openAPI.getComponents().getPathItems().get("/pet").getDescription(), "get a pet");
    assertNotNull(openAPI.getComponents().getPathItems().get("/pet").getGet());
    assertEquals(openAPI.getComponents().getPathItems().get("/pet").getGet().getOperationId(), "getPet");
    assertNotNull(openAPI.getComponents().getSchemas());
    assertNotNull(openAPI.getComponents().getSchemas().get("Pet"));
    assertNotNull(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator());
    assertNotNull(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator().getExtensions());
    assertEquals(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator().getExtensions().get("x-test-extension"), "extended");
    assertNotNull(openAPI.getComponents().getResponses());
    assertNotNull(openAPI.getComponents().getResponses().get("201"));
    assertEquals(openAPI.getComponents().getResponses().get("201").getDescription(), "api response description");
    assertNotNull(openAPI.getComponents().getParameters());
    assertNotNull(openAPI.getComponents().getParameters().get("param"));
    assertEquals(openAPI.getComponents().getParameters().get("param").getIn(), "query");
    assertEquals(openAPI.getComponents().getParameters().get("param").getName(), "param0");
    assertEquals(openAPI.getComponents().getParameters().get("param").getDescription(), "parameter description");
    assertNotNull(openAPI.getComponents().getExamples());
    assertNotNull(openAPI.getComponents().getExamples().get("example"));
    assertEquals(openAPI.getComponents().getExamples().get("example").getDescription(), "example description");
    assertEquals(openAPI.getComponents().getExamples().get("example").getSummary(), "example summary");
    assertEquals(openAPI.getComponents().getExamples().get("example").getValue(), "This is an example");
    assertNotNull(openAPI.getComponents().getRequestBodies());
    assertNotNull(openAPI.getComponents().getRequestBodies().get("body"));
    assertNotNull(openAPI.getComponents().getHeaders());
    assertNotNull(openAPI.getComponents().getHeaders().get("test-head"));
    assertEquals(openAPI.getComponents().getHeaders().get("test-head").getDescription(), "test header description");
    assertNotNull(openAPI.getComponents().getSecuritySchemes());
    assertNotNull(openAPI.getComponents().getSecuritySchemes().get("basic"));
    assertEquals(openAPI.getComponents().getSecuritySchemes().get("basic").getDescription(), "security description");
    assertEquals(openAPI.getComponents().getSecuritySchemes().get("basic").getType().toString(), "http");
    assertNotNull(openAPI.getComponents().getLinks());
    assertNotNull(openAPI.getComponents().getLinks().get("Link"));
    assertEquals(openAPI.getComponents().getLinks().get("Link").getOperationRef(), "#/paths/~12.0~1repositories~1{username}/get");
    assertNotNull(openAPI.getComponents().getCallbacks());
    assertNotNull(openAPI.getComponents().getCallbacks().get("TestCallback"));
    openAPI = Yaml.mapper().readValue(jsonString, OpenAPI.class);
    assertNotNull(openAPI);
    assertNull(openAPI.getInfo().getSummary());
    assertNull(openAPI.getWebhooks());
    assertNull(openAPI.getComponents().getPathItems());
    assertNull(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator().getExtensions());
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 55 with Header

use of io.swagger.v3.oas.models.headers.Header in project swagger-core by swagger-api.

the class OpenAPI3_1DeserializationTest method testDeserializationOnOAS30.

@Test
public void testDeserializationOnOAS30() throws IOException {
    final String jsonString = ResourceUtils.loadClassResource(getClass(), "specFiles/3.1.0/petstore-3.1_sample.yaml");
    OpenAPI openAPI = Yaml.mapper().readValue(jsonString, OpenAPI.class);
    assertNotNull(openAPI);
    assertEquals(openAPI.getInfo().getTitle(), "Swagger Petstore");
    assertEquals(openAPI.getInfo().getVersion(), "1.0.0");
    assertNull(openAPI.getInfo().getSummary());
    assertEquals(openAPI.getInfo().getLicense().getName(), "MIT");
    assertNull(openAPI.getInfo().getLicense().getIdentifier());
    assertNull(openAPI.getWebhooks());
    assertNull(openAPI.getComponents().getPathItems());
    assertNotNull(openAPI.getComponents().getSchemas());
    assertNotNull(openAPI.getComponents().getSchemas().get("Pet"));
    assertNotNull(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator());
    assertNull(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator().getExtensions());
    assertNotNull(openAPI.getComponents().getResponses());
    assertNotNull(openAPI.getComponents().getResponses().get("201"));
    assertEquals(openAPI.getComponents().getResponses().get("201").getDescription(), "api response description");
    assertNotNull(openAPI.getComponents().getParameters());
    assertNotNull(openAPI.getComponents().getParameters().get("param"));
    assertEquals(openAPI.getComponents().getParameters().get("param").getIn(), "query");
    assertEquals(openAPI.getComponents().getParameters().get("param").getName(), "param0");
    assertEquals(openAPI.getComponents().getParameters().get("param").getDescription(), "parameter description");
    assertNotNull(openAPI.getComponents().getExamples());
    assertNotNull(openAPI.getComponents().getExamples().get("example"));
    assertEquals(openAPI.getComponents().getExamples().get("example").getDescription(), "example description");
    assertEquals(openAPI.getComponents().getExamples().get("example").getSummary(), "example summary");
    assertEquals(openAPI.getComponents().getExamples().get("example").getValue(), "This is an example");
    assertNotNull(openAPI.getComponents().getRequestBodies());
    assertNotNull(openAPI.getComponents().getRequestBodies().get("body"));
    assertNotNull(openAPI.getComponents().getHeaders());
    assertNotNull(openAPI.getComponents().getHeaders().get("test-head"));
    assertEquals(openAPI.getComponents().getHeaders().get("test-head").getDescription(), "test header description");
    assertNotNull(openAPI.getComponents().getSecuritySchemes());
    assertNotNull(openAPI.getComponents().getSecuritySchemes().get("basic"));
    assertEquals(openAPI.getComponents().getSecuritySchemes().get("basic").getDescription(), "security description");
    assertEquals(openAPI.getComponents().getSecuritySchemes().get("basic").getType().toString(), "http");
    assertNotNull(openAPI.getComponents().getLinks());
    assertNotNull(openAPI.getComponents().getLinks().get("Link"));
    assertEquals(openAPI.getComponents().getLinks().get("Link").getOperationRef(), "#/paths/~12.0~1repositories~1{username}/get");
    assertNotNull(openAPI.getComponents().getCallbacks());
    assertNotNull(openAPI.getComponents().getCallbacks().get("TestCallback"));
    openAPI = Yaml.mapper().readValue(jsonString, OpenAPI.class);
    assertNotNull(openAPI);
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

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