Search in sources :

Example 51 with Header

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

the class SwaggerConverter method convert.

public Parameter convert(io.swagger.models.parameters.Parameter v2Parameter) {
    Parameter v3Parameter = new Parameter();
    if (StringUtils.isNotBlank(v2Parameter.getDescription())) {
        v3Parameter.setDescription(v2Parameter.getDescription());
    }
    if (v2Parameter instanceof SerializableParameter) {
        v3Parameter.setAllowEmptyValue(((SerializableParameter) v2Parameter).getAllowEmptyValue());
    }
    v3Parameter.setIn(v2Parameter.getIn());
    v3Parameter.setName(v2Parameter.getName());
    Object exampleExtension = v2Parameter.getVendorExtensions().get("x-example");
    if (exampleExtension != null) {
        v3Parameter.setExample(exampleExtension);
    }
    Schema schema = null;
    if (v2Parameter instanceof RefParameter) {
        RefParameter ref = (RefParameter) v2Parameter;
        if (ref.get$ref().indexOf("#/parameters") == 0) {
            String updatedRef = "#/components/";
            if (components.getRequestBodies() != null && components.getRequestBodies().get(ref.getSimpleRef()) != null) {
                updatedRef += "requestBodies";
            } else if (components.getSchemas() != null && components.getSchemas().get("formData_" + ref.getSimpleRef()) != null) {
                updatedRef += "schemas";
            } else {
                updatedRef += "parameters";
            }
            updatedRef += ref.get$ref().substring("#/parameters".length());
            ref.set$ref(updatedRef);
        }
        v3Parameter.set$ref(ref.get$ref());
    } else if (v2Parameter instanceof SerializableParameter) {
        SerializableParameter sp = (SerializableParameter) v2Parameter;
        if ("array".equals(sp.getType())) {
            ArraySchema a = new ArraySchema();
            // TODO: convert arrays to proper template format
            String cf = sp.getCollectionFormat();
            if (StringUtils.isEmpty(cf)) {
                cf = "csv";
            }
            switch(cf) {
                case "ssv":
                    if ("query".equals(v2Parameter.getIn())) {
                        v3Parameter.setStyle(Parameter.StyleEnum.SPACEDELIMITED);
                    }
                    break;
                case "pipes":
                    if ("query".equals(v2Parameter.getIn())) {
                        v3Parameter.setStyle((Parameter.StyleEnum.PIPEDELIMITED));
                    }
                    break;
                case "tsv":
                    break;
                case "multi":
                    if ("query".equals(v2Parameter.getIn())) {
                        v3Parameter.setStyle(Parameter.StyleEnum.FORM);
                        v3Parameter.setExplode(true);
                    }
                    break;
                case "csv":
                default:
                    if ("query".equals(v2Parameter.getIn())) {
                        v3Parameter.setStyle(Parameter.StyleEnum.FORM);
                        v3Parameter.setExplode(false);
                    } else if ("header".equals(v2Parameter.getIn()) || "path".equals(v2Parameter.getIn())) {
                        v3Parameter.setStyle(Parameter.StyleEnum.SIMPLE);
                        v3Parameter.setExplode(false);
                    }
            }
            Property items = sp.getItems();
            Schema itemsSchema = convert(items);
            a.setItems(itemsSchema);
            if (sp.getMaxItems() != null) {
                a.setMaxItems(sp.getMaxItems());
            }
            if (sp.getMinItems() != null) {
                a.setMinItems(sp.getMinItems());
            }
            if (sp.isUniqueItems() != null) {
                a.setUniqueItems(sp.isUniqueItems());
            }
            schema = a;
        } else {
            schema = SchemaTypeUtil.createSchema(sp.getType(), sp.getFormat());
            schema.setType(sp.getType());
            schema.setFormat(sp.getFormat());
            schema.setMaximum(sp.getMaximum());
            schema.setExclusiveMaximum(sp.isExclusiveMaximum());
            schema.setMinimum(sp.getMinimum());
            schema.setExclusiveMinimum(sp.isExclusiveMinimum());
            schema.setMinLength(sp.getMinLength());
            schema.setMaxLength(sp.getMaxLength());
            if (sp.getMultipleOf() != null) {
                schema.setMultipleOf(new BigDecimal(sp.getMultipleOf().toString()));
            }
            schema.setPattern(sp.getPattern());
        }
        if (sp.getEnum() != null) {
            for (String e : sp.getEnum()) {
                switch(sp.getType() == null ? SchemaTypeUtil.OBJECT_TYPE : sp.getType()) {
                    case SchemaTypeUtil.INTEGER_TYPE:
                        schema.addEnumItemObject(Integer.parseInt(e));
                        break;
                    case SchemaTypeUtil.NUMBER_TYPE:
                        schema.addEnumItemObject(new BigDecimal(e));
                        break;
                    case SchemaTypeUtil.BOOLEAN_TYPE:
                        schema.addEnumItemObject(Boolean.valueOf(e));
                        break;
                    default:
                        schema.addEnumItemObject(e);
                        break;
                }
            }
        }
        if (sp.getVendorExtensions() != null) {
            Object nullableExtension = sp.getVendorExtensions().get("x-nullable");
            if (nullableExtension != null) {
                schema.setNullable((Boolean) nullableExtension);
            }
        }
        schema.setExtensions(convert(sp.getVendorExtensions()));
        if (sp instanceof AbstractSerializableParameter) {
            AbstractSerializableParameter ap = (AbstractSerializableParameter) sp;
            schema.setDefault(ap.getDefault());
        }
    }
    if (v2Parameter.getRequired()) {
        v3Parameter.setRequired(v2Parameter.getRequired());
    }
    v3Parameter.setSchema(schema);
    v3Parameter.setExtensions(convert(v2Parameter.getVendorExtensions()));
    return v3Parameter;
}
Also used : SerializableParameter(io.swagger.models.parameters.SerializableParameter) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) RefParameter(io.swagger.models.parameters.RefParameter) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) FileSchema(io.swagger.v3.oas.models.media.FileSchema) Parameter(io.swagger.v3.oas.models.parameters.Parameter) SerializableParameter(io.swagger.models.parameters.SerializableParameter) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) RefParameter(io.swagger.models.parameters.RefParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) BigDecimal(java.math.BigDecimal)

Example 52 with Header

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

the class V2ConverterTest method testIssue785.

@Test(description = "OpenAPIParser.readLocation fails when fetching valid Swagger 2.0 resource with AuthorizationValues provided")
public void testIssue785() {
    AuthorizationValue apiKey = new AuthorizationValue("api_key", "special-key", "header");
    List<AuthorizationValue> authorizationValues = Arrays.asList(apiKey);
    SwaggerConverter converter = new SwaggerConverter();
    List<io.swagger.models.auth.AuthorizationValue> convertedAuthList = converter.convert(authorizationValues);
    assertEquals(convertedAuthList.size(), authorizationValues.size());
}
Also used : AuthorizationValue(io.swagger.v3.parser.core.models.AuthorizationValue) SwaggerConverter(io.swagger.v3.parser.converter.SwaggerConverter) Test(org.testng.annotations.Test)

Example 53 with Header

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