Search in sources :

Example 56 with Header

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

the class ParameterDeSerializationTest method deserializeStringArrayHeaderParameter.

@Test(description = "it should deserialize a string array HeaderParameter")
public void deserializeStringArrayHeaderParameter() throws IOException {
    final String json = "{\"in\":\"header\",\"required\":true,\"schema\":{\"type\":\"string\"}}";
    final Parameter p = m.readValue(json, Parameter.class);
    SerializationMatchers.assertEqualsToJson(p, json);
}
Also used : Parameter(io.swagger.v3.oas.models.parameters.Parameter) Test(org.testng.annotations.Test)

Example 57 with Header

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

the class AnnotationsUtilsHeadersTest method extensionsTest.

@Test(dataProvider = "expectedData")
public void extensionsTest(String methodName, Optional<Map<String, io.swagger.v3.oas.models.headers.Header>> expected) throws NoSuchMethodException {
    final Method method = getClass().getDeclaredMethod(methodName);
    final Header[] headers = Arrays.stream(method.getAnnotation(Operation.class).responses()).flatMap(response -> Arrays.stream(response.headers())).toArray(Header[]::new);
    final Optional<Map<String, io.swagger.v3.oas.models.headers.Header>> optionalMap = AnnotationsUtils.getHeaders(headers, null);
    Assert.assertEquals(optionalMap, expected);
}
Also used : Arrays(java.util.Arrays) DataProvider(org.testng.annotations.DataProvider) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.testng.annotations.Test) AnnotationsUtils(io.swagger.v3.core.util.AnnotationsUtils) Operation(io.swagger.v3.oas.annotations.Operation) StyleEnum(io.swagger.v3.oas.models.headers.Header.StyleEnum) Assert(org.testng.Assert) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse) Map(java.util.Map) Optional(java.util.Optional) Method(java.lang.reflect.Method) Header(io.swagger.v3.oas.annotations.headers.Header) Header(io.swagger.v3.oas.annotations.headers.Header) Method(java.lang.reflect.Method) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 58 with Header

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

the class OpenAPI3_1SerializationTest method testComponentPathItemsSerialization.

@Test
public void testComponentPathItemsSerialization() {
    Schema schema = new StringSchema();
    schema.addType(schema.getType());
    OpenAPI openAPI = new OpenAPI().openapi("3.1.0").components(new Components().addSchemas("stringTest", schema).addPathItem("/pathTest", new PathItem().description("test path item").get(new Operation().operationId("testPathItem").responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("response description"))))).addResponses("201", new ApiResponse().description("api response description")).addParameters("param", new Parameter().in("query").description("parameter description").schema(schema)).addExamples("example", new Example().summary("example summary").value("This is an example/").description("example description")).addRequestBodies("body", new RequestBody().content(new Content().addMediaType("application/json", new MediaType().schema(new ObjectSchema())))).addHeaders("test-head", new Header().description("test header description")).addSecuritySchemes("basic", new SecurityScheme().in(SecurityScheme.In.HEADER).scheme("http").description("ref security description")).addLinks("Link", new Link().operationRef("#/paths/~12.0~1repositories~1{username}/get")).addCallbacks("TestCallback", new Callback().addPathItem("{$request.query.queryUrl}", new PathItem().description("test path item").post(new Operation().operationId("testPathItem")))));
    SerializationMatchers.assertEqualsToYaml31(openAPI, "openapi: 3.1.0\n" + "components:\n" + "  schemas:\n" + "    stringTest:\n" + "      type: string\n" + "  responses:\n" + "    \"201\":\n" + "      description: api response description\n" + "  parameters:\n" + "    param:\n" + "      in: query\n" + "      description: parameter description\n" + "      schema:\n" + "        type: string\n" + "  examples:\n" + "    example:\n" + "      summary: example summary\n" + "      description: example description\n" + "      value: This is an example/\n" + "  requestBodies:\n" + "    body:\n" + "      content:\n" + "        application/json:\n" + "          schema: {}\n" + "  headers:\n" + "    test-head:\n" + "      description: test header description\n" + "  securitySchemes:\n" + "    basic:\n" + "      description: ref security description\n" + "      in: header\n" + "      scheme: http\n" + "  links:\n" + "    Link:\n" + "      operationRef: \"#/paths/~12.0~1repositories~1{username}/get\"\n" + "  callbacks:\n" + "    TestCallback:\n" + "      '{$request.query.queryUrl}':\n" + "        description: test path item\n" + "        post:\n" + "          operationId: testPathItem\n" + "  pathItems:\n" + "    /pathTest:\n" + "      description: test path item\n" + "      get:\n" + "        operationId: testPathItem\n" + "        responses:\n" + "          \"200\":\n" + "            description: response description");
    SerializationMatchers.assertEqualsToJson31(openAPI, "{\n" + "  \"openapi\" : \"3.1.0\",\n" + "  \"components\" : {\n" + "    \"schemas\" : {\n" + "      \"stringTest\" : {\n" + "        \"type\" : \"string\"\n" + "      }\n" + "    },\n" + "    \"responses\" : {\n" + "      \"201\" : {\n" + "        \"description\" : \"api response description\"\n" + "      }\n" + "    },\n" + "    \"parameters\" : {\n" + "      \"param\" : {\n" + "        \"in\" : \"query\",\n" + "        \"description\" : \"parameter description\",\n" + "        \"schema\" : {\n" + "          \"type\" : \"string\"\n" + "        }\n" + "      }\n" + "    },\n" + "    \"examples\" : {\n" + "      \"example\" : {\n" + "        \"summary\" : \"example summary\",\n" + "        \"description\" : \"example description\",\n" + "        \"value\" : \"This is an example/\"\n" + "      }\n" + "    },\n" + "    \"requestBodies\" : {\n" + "      \"body\" : {\n" + "        \"content\" : {\n" + "          \"application/json\" : {\n" + "            \"schema\" : { }\n" + "          }\n" + "        }\n" + "      }\n" + "    },\n" + "    \"headers\" : {\n" + "      \"test-head\" : {\n" + "        \"description\" : \"test header description\"\n" + "      }\n" + "    },\n" + "    \"securitySchemes\" : {\n" + "      \"basic\" : {\n" + "        \"description\" : \"ref security description\",\n" + "        \"in\" : \"header\",\n" + "        \"scheme\" : \"http\"\n" + "      }\n" + "    },\n" + "    \"links\" : {\n" + "      \"Link\" : {\n" + "        \"operationRef\" : \"#/paths/~12.0~1repositories~1{username}/get\"\n" + "      }\n" + "    },\n" + "    \"callbacks\" : {\n" + "      \"TestCallback\" : {\n" + "        \"{$request.query.queryUrl}\" : {\n" + "          \"description\" : \"test path item\",\n" + "          \"post\" : {\n" + "            \"operationId\" : \"testPathItem\"\n" + "          }\n" + "        }\n" + "      }\n" + "    },\n" + "    \"pathItems\" : {\n" + "      \"/pathTest\" : {\n" + "        \"description\" : \"test path item\",\n" + "        \"get\" : {\n" + "          \"operationId\" : \"testPathItem\",\n" + "          \"responses\" : {\n" + "            \"200\" : {\n" + "              \"description\" : \"response description\"\n" + "            }\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  }\n" + "}");
    openAPI.openapi("3.0.3");
    SerializationMatchers.assertEqualsToYaml(openAPI, "openapi: 3.0.3\n" + "components:\n" + "  schemas:\n" + "    stringTest:\n" + "      type: string\n" + "  responses:\n" + "    \"201\":\n" + "      description: api response description\n" + "  parameters:\n" + "    param:\n" + "      in: query\n" + "      description: parameter description\n" + "      schema:\n" + "        type: string\n" + "  examples:\n" + "    example:\n" + "      summary: example summary\n" + "      description: example description\n" + "      value: This is an example/\n" + "  requestBodies:\n" + "    body:\n" + "      content:\n" + "        application/json:\n" + "          schema:\n" + "            type: object\n" + "  headers:\n" + "    test-head:\n" + "      description: test header description\n" + "  securitySchemes:\n" + "    basic:\n" + "      description: ref security description\n" + "      in: header\n" + "      scheme: http\n" + "  links:\n" + "    Link:\n" + "      operationRef: \"#/paths/~12.0~1repositories~1{username}/get\"\n" + "  callbacks:\n" + "    TestCallback:\n" + "      '{$request.query.queryUrl}':\n" + "        description: test path item\n" + "        post:\n" + "          operationId: testPathItem");
    SerializationMatchers.assertEqualsToJson(openAPI, "{\n" + "  \"openapi\" : \"3.0.3\",\n" + "  \"components\" : {\n" + "    \"schemas\" : {\n" + "      \"stringTest\" : {\n" + "        \"type\" : \"string\"\n" + "      }\n" + "    },\n" + "    \"responses\" : {\n" + "      \"201\" : {\n" + "        \"description\" : \"api response description\"\n" + "      }\n" + "    },\n" + "    \"parameters\" : {\n" + "      \"param\" : {\n" + "        \"in\" : \"query\",\n" + "        \"description\" : \"parameter description\",\n" + "        \"schema\" : {\n" + "          \"type\" : \"string\"\n" + "        }\n" + "      }\n" + "    },\n" + "    \"examples\" : {\n" + "      \"example\" : {\n" + "        \"summary\" : \"example summary\",\n" + "        \"description\" : \"example description\",\n" + "        \"value\" : \"This is an example/\"\n" + "      }\n" + "    },\n" + "    \"requestBodies\" : {\n" + "      \"body\" : {\n" + "        \"content\" : {\n" + "          \"application/json\" : {\n" + "            \"schema\" : {\n" + "              \"type\" : \"object\"\n" + "            }\n" + "          }\n" + "        }\n" + "      }\n" + "    },\n" + "    \"headers\" : {\n" + "      \"test-head\" : {\n" + "        \"description\" : \"test header description\"\n" + "      }\n" + "    },\n" + "    \"securitySchemes\" : {\n" + "      \"basic\" : {\n" + "        \"description\" : \"ref security description\",\n" + "        \"in\" : \"header\",\n" + "        \"scheme\" : \"http\"\n" + "      }\n" + "    },\n" + "    \"links\" : {\n" + "      \"Link\" : {\n" + "        \"operationRef\" : \"#/paths/~12.0~1repositories~1{username}/get\"\n" + "      }\n" + "    },\n" + "    \"callbacks\" : {\n" + "      \"TestCallback\" : {\n" + "        \"{$request.query.queryUrl}\" : {\n" + "          \"description\" : \"test path item\",\n" + "          \"post\" : {\n" + "            \"operationId\" : \"testPathItem\"\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  }\n" + "}");
}
Also used : Schema(io.swagger.v3.oas.models.media.Schema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Operation(io.swagger.v3.oas.models.Operation) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Components(io.swagger.v3.oas.models.Components) PathItem(io.swagger.v3.oas.models.PathItem) Callback(io.swagger.v3.oas.models.callbacks.Callback) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Header(io.swagger.v3.oas.models.headers.Header) Content(io.swagger.v3.oas.models.media.Content) Example(io.swagger.v3.oas.models.examples.Example) Parameter(io.swagger.v3.oas.models.parameters.Parameter) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) Link(io.swagger.v3.oas.models.links.Link) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Example 59 with Header

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

the class OpenAPI3_1SerializationTest method testHeaderRefSerialization.

@Test
public void testHeaderRefSerialization() {
    OpenAPI openAPI = new OpenAPI().openapi("3.1.0").path("/test", new PathItem().description("test path item").post(new Operation().operationId("testPathItem").responses(new ApiResponses().addApiResponse("default", new ApiResponse().description("default response").addHeaderObject("header", new Header().$ref("#/components/responses/okResponse").description("ref header description")))))).components(new Components().addHeaders("test-head", new Header().description("test header description")));
    SerializationMatchers.assertEqualsToYaml31(openAPI, "openapi: 3.1.0\n" + "paths:\n" + "  /test:\n" + "    description: test path item\n" + "    post:\n" + "      operationId: testPathItem\n" + "      responses:\n" + "        default:\n" + "          description: default response\n" + "          headers:\n" + "            header:\n" + "              description: ref header description\n" + "              $ref: '#/components/responses/okResponse'\n" + "components:\n" + "  headers:\n" + "    test-head:\n" + "      description: test header description");
    SerializationMatchers.assertEqualsToJson31(openAPI, "{\n" + "  \"openapi\" : \"3.1.0\",\n" + "  \"paths\" : {\n" + "    \"/test\" : {\n" + "      \"description\" : \"test path item\",\n" + "      \"post\" : {\n" + "        \"operationId\" : \"testPathItem\",\n" + "        \"responses\" : {\n" + "          \"default\" : {\n" + "            \"description\" : \"default response\",\n" + "            \"headers\" : {\n" + "              \"header\" : {\n" + "                \"description\" : \"ref header description\",\n" + "                \"$ref\" : \"#/components/responses/okResponse\"\n" + "              }\n" + "            }\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  },\n" + "  \"components\" : {\n" + "    \"headers\" : {\n" + "      \"test-head\" : {\n" + "        \"description\" : \"test header description\"\n" + "      }\n" + "    }\n" + "  }\n" + "}");
}
Also used : Components(io.swagger.v3.oas.models.Components) PathItem(io.swagger.v3.oas.models.PathItem) Header(io.swagger.v3.oas.models.headers.Header) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Test(org.testng.annotations.Test)

Example 60 with Header

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

the class ParameterDeserializer method deserialize.

@Override
public Parameter deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    Parameter result = null;
    JsonNode node = jp.getCodec().readTree(jp);
    JsonNode sub = node.get("$ref");
    JsonNode inNode = node.get("in");
    JsonNode desc = node.get("description");
    if (sub != null) {
        result = new Parameter().$ref(sub.asText());
        if (desc != null && openapi31) {
            result.description(desc.asText());
        }
    } else if (inNode != null) {
        String in = inNode.asText();
        ObjectReader reader = null;
        ObjectMapper mapper = null;
        if (openapi31) {
            mapper = Json31.mapper();
        } else {
            mapper = Json.mapper();
        }
        if ("query".equals(in)) {
            reader = mapper.readerFor(QueryParameter.class);
        } else if ("header".equals(in)) {
            reader = mapper.readerFor(HeaderParameter.class);
        } else if ("path".equals(in)) {
            reader = mapper.readerFor(PathParameter.class);
        } else if ("cookie".equals(in)) {
            reader = mapper.readerFor(CookieParameter.class);
        }
        if (reader != null) {
            result = reader.with(DeserializationFeature.READ_ENUMS_USING_TO_STRING).readValue(node);
        }
    }
    return result;
}
Also used : Parameter(io.swagger.v3.oas.models.parameters.Parameter) CookieParameter(io.swagger.v3.oas.models.parameters.CookieParameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) HeaderParameter(io.swagger.v3.oas.models.parameters.HeaderParameter) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) CookieParameter(io.swagger.v3.oas.models.parameters.CookieParameter) HeaderParameter(io.swagger.v3.oas.models.parameters.HeaderParameter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

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