Search in sources :

Example 46 with Header

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

the class OpenAPIDeserializerTest method testExampleVsExamples.

@Test
public void testExampleVsExamples() {
    String json = "{" + "\"openapi\": \"3.0.0\"," + "\"info\": {\"title\": \"Examples\", \"version\": \"0.0.0\"}," + "\"paths\": {}," + "\"components\": {" + "  \"parameters\": {" + "    \"withExample\": {" + "      \"name\": \"withExample\"," + "      \"in\": \"query\"," + "      \"schema\": {\"type\": \"string\"}," + "      \"example\": \"Hello\"}," + "    \"withExamples\": {" + "      \"name\": \"withExamples\"," + "      \"in\": \"query\"," + "      \"schema\": {\"type\": \"string\"}," + "      \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}}," + "    \"withBoth\": {" + "      \"name\": \"withBoth\"," + "      \"in\": \"query\"," + "      \"schema\": {\"type\": \"string\"}," + "      \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}," + "      \"example\": \"Hello\"}," + "    \"withContentExample\": {" + "      \"name\": \"withContentExample\"," + "      \"in\": \"query\"," + "      \"content\": {" + "        \"application/json\": {" + "          \"schema\": {\"type\": \"string\"}," + "          \"example\": \"Hello\"}}}," + "    \"withContentExamples\": {" + "      \"name\": \"withContentExamples\"," + "      \"in\": \"query\"," + "      \"content\": {" + "        \"application/json\": {" + "          \"schema\": {\"type\": \"string\"}," + "          \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}}}}," + "    \"withContentBoth\": {" + "      \"name\": \"withContentBoth\"," + "      \"in\": \"query\"," + "      \"content\": {" + "        \"application/json\": {" + "          \"schema\": {\"type\": \"string\"}," + "          \"example\": \"Hello\"," + "          \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}}}}}," + "  \"headers\": {" + "    \"withExample\": {" + "      \"schema\": {\"type\": \"string\"}," + "      \"example\": \"Hello\"}," + "    \"withExamples\": {" + "      \"schema\": {\"type\": \"string\"}," + "      \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}}," + "    \"withBoth\": {" + "      \"schema\": {\"type\": \"string\"}," + "      \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}," + "      \"example\": \"Hello\"}}," + "  \"requestBodies\": {" + "    \"withBodyExample\": {" + "      \"content\": {" + "        \"application/json\": {" + "          \"schema\": {\"type\": \"string\"}," + "          \"example\": \"Hello\"}}}," + "    \"withBodyExamples\": {" + "      \"content\": {" + "        \"application/json\": {" + "          \"schema\": {\"type\": \"string\"}," + "          \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}}}}," + "    \"withBodyBoth\": {" + "      \"content\": {" + "        \"application/json\": {" + "          \"schema\": {\"type\": \"string\"}," + "          \"example\": \"Hello\"," + "          \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}}}}}}}";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(json, null, null);
    assertEqualsNoOrder(result.getMessages().toArray(), new Object[] { "attribute components.parameters.withBoth.[withBoth].examples already defined -- ignoring \"example\" field", "attribute components.parameters.withContentBoth.[withContentBoth].content.'application/json'.examples already defined -- ignoring \"example\" field", "attribute components.requestBodies.withBodyBoth.content.'application/json'.examples already defined -- ignoring \"example\" field", "attribute components.headers.withBoth.examples already defined -- ignoring \"example\" field" }, "Expected warnings not found");
    OpenAPI openAPI = result.getOpenAPI();
    Parameter param;
    param = openAPI.getComponents().getParameters().get("withExample");
    assertNull(param.getExamples(), "Examples,");
    assertNotNull(param.getExample(), "Example,");
    param = openAPI.getComponents().getParameters().get("withExamples");
    assertNotNull(param.getExamples(), "Examples,");
    assertNull(param.getExample(), "Example,");
    param = openAPI.getComponents().getParameters().get("withBoth");
    assertNotNull(param.getExamples(), "Examples,");
    assertNull(param.getExample(), "Example,");
    Header header;
    header = openAPI.getComponents().getHeaders().get("withExample");
    assertNull(header.getExamples(), "Examples,");
    assertNotNull(header.getExample(), "Example,");
    header = openAPI.getComponents().getHeaders().get("withExamples");
    assertNotNull(header.getExamples(), "Examples,");
    assertNull(header.getExample(), "Example,");
    header = openAPI.getComponents().getHeaders().get("withBoth");
    assertNotNull(header.getExamples(), "Examples,");
    assertNull(header.getExample(), "Example,");
    MediaType mediaType;
    mediaType = openAPI.getComponents().getParameters().get("withContentExample").getContent().get("application/json");
    assertNull(mediaType.getExamples(), "Examples,");
    assertNotNull(mediaType.getExample(), "Example,");
    mediaType = openAPI.getComponents().getParameters().get("withContentExamples").getContent().get("application/json");
    assertNotNull(mediaType.getExamples(), "Examples,");
    assertNull(mediaType.getExample(), "Example,");
    mediaType = openAPI.getComponents().getParameters().get("withContentBoth").getContent().get("application/json");
    assertNotNull(mediaType.getExamples(), "Examples,");
    assertNull(mediaType.getExample(), "Example,");
    mediaType = openAPI.getComponents().getRequestBodies().get("withBodyExample").getContent().get("application/json");
    assertNull(mediaType.getExamples(), "Examples,");
    assertNotNull(mediaType.getExample(), "Example,");
    mediaType = openAPI.getComponents().getRequestBodies().get("withBodyExamples").getContent().get("application/json");
    assertNotNull(mediaType.getExamples(), "Examples,");
    assertNull(mediaType.getExample(), "Example,");
    mediaType = openAPI.getComponents().getRequestBodies().get("withBodyBoth").getContent().get("application/json");
    assertNotNull(mediaType.getExamples(), "Examples,");
    assertNull(mediaType.getExample(), "Example,");
}
Also used : Header(io.swagger.v3.oas.models.headers.Header) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) MediaType(io.swagger.v3.oas.models.media.MediaType) 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 47 with Header

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

the class OpenAPIDeserializerTest method testExamples.

@Test
public void testExamples(@Injectable List<AuthorizationValue> auths) {
    String yaml = "openapi: 3.0.1\n" + "info:\n" + "  title: httpbin\n" + "  version: 0.0.0\n" + "servers:\n" + "  - url: http://httpbin.org\n" + "paths:\n" + "  /post:\n" + "    post:\n" + "      summary: Returns the POSTed data\n" + "      requestBody:\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              $ref: '#/components/schemas/AnyValue'\n" + "            examples:\n" + "              AnObject:\n" + "                $ref: '#/components/examples/AnObject'\n" + "              ANull:\n" + "                $ref: '#/components/examples/ANull'\n" + "          application/yaml:\n" + "            schema:\n" + "              $ref: '#/components/schemas/AnyValue'\n" + "            examples:\n" + "              AString:\n" + "                $ref: '#/components/examples/AString'\n" + "              AnArray:\n" + "                $ref: '#/components/examples/AnArray'\n" + "          text/plain:\n" + "            schema:\n" + "              type: string\n" + "              example: Hi there\n" + "          application/x-www-form-urlencoded:\n" + "            schema:\n" + "              type: object\n" + "              properties:\n" + "                id:\n" + "                  type: integer\n" + "                name:\n" + "                  type: string\n" + "            example:\n" + "              id: 42\n" + "              name: Arthur Dent\n" + "      responses:\n" + "        '200':\n" + "          description: OK\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                type: object\n" + "\n" + "  #/response-headers:\n" + "  /:\n" + "    get:\n" + "      summary: Returns a response with the specified headers\n" + "      parameters:\n" + "        - in: header\n" + "          name: Server\n" + "          required: true\n" + "          schema:\n" + "            type: string\n" + "          examples:\n" + "            httpbin:\n" + "              value: httpbin\n" + "            unicorn:\n" + "              value: unicorn\n" + "        - in: header\n" + "          name: X-Request-Id\n" + "          required: true\n" + "          schema:\n" + "            type: integer\n" + "          example: 37\n" + "      responses:\n" + "        '200':\n" + "          description: A response with the specified headers\n" + "          headers:\n" + "            Server:\n" + "              schema:\n" + "                type: string\n" + "              examples:\n" + "                httpbin:\n" + "                  value: httpbin\n" + "                unicorn:\n" + "                  value: unicorn\n" + "            X-Request-Id:\n" + "              schema:\n" + "                type: integer\n" + "              example: 37\n" + "\n" + "components:\n" + "  schemas:\n" + "    AnyValue:\n" + "      nullable: true\n" + "      description: Can be anything - string, object, array, null, etc.\n" + "\n" + "  examples:\n" + "    AString:\n" + "      value: Hi there\n" + "    ANumber:\n" + "      value: 42\n" + "    ANull:\n" + "      value: null\n" + "    AnArray:\n" + "      value: [1, 2, 3]\n" + "    AnObject:\n" + "      value:\n" + "        id:  42\n" + "        name: Arthur Dent";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult result = parser.readContents(yaml, auths, options);
    OpenAPI openAPI = result.getOpenAPI();
    MediaType mediaTypeJson = openAPI.getPaths().get("/post").getPost().getRequestBody().getContent().get("application/json");
    Header header1 = openAPI.getPaths().get("/").getGet().getResponses().get("200").getHeaders().get("Server");
    Header header2 = openAPI.getPaths().get("/").getGet().getResponses().get("200").getHeaders().get("X-Request-Id");
    Parameter parameter1 = openAPI.getPaths().get("/").getGet().getParameters().get(0);
    Parameter parameter2 = openAPI.getPaths().get("/").getGet().getParameters().get(1);
    Assert.assertNotNull(mediaTypeJson.getExamples());
    Assert.assertEquals(mediaTypeJson.getExamples().get("AnObject").get$ref(), "#/components/examples/AnObject");
    Assert.assertNotNull(header1.getExamples());
    Assert.assertEquals(header1.getExamples().get("httpbin").getValue(), "httpbin");
    Assert.assertNotNull(header2.getExample());
    Assert.assertEquals(header2.getExample(), 37);
    Assert.assertNotNull(parameter1.getExamples());
    Assert.assertEquals(parameter1.getExamples().get("unicorn").getValue(), "unicorn");
    Assert.assertNotNull(parameter2.getExample());
    Assert.assertEquals(parameter2.getExample(), 37);
}
Also used : Header(io.swagger.v3.oas.models.headers.Header) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) MediaType(io.swagger.v3.oas.models.media.MediaType) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) 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 48 with Header

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

the class RemoteUrlTest method testAuthorizationHeaderWithMatchingUrl.

@Test
public void testAuthorizationHeaderWithMatchingUrl() throws Exception {
    final String expectedBody = setupStub();
    final String headerName = "Authorization";
    final String headerValue = "foobar";
    final AuthorizationValue authorizationValue = new AuthorizationValue(headerName, headerValue, "header", url -> url.toString().startsWith("http://localhost"));
    final String actualBody = RemoteUrl.urlToString(getUrl(), Arrays.asList(authorizationValue));
    assertEquals(actualBody, expectedBody);
    verify(getRequestedFor(urlEqualTo("/v2/pet/1")).withHeader("Accept", equalTo(EXPECTED_ACCEPTS_HEADER)).withHeader(headerName, equalTo(headerValue)));
}
Also used : AuthorizationValue(io.swagger.v3.parser.core.models.AuthorizationValue) Test(org.testng.annotations.Test)

Example 49 with Header

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

the class OpenAPIParserTest method testIssue844.

@Test
public void testIssue844() {
    OpenAPIParser openApiParser = new OpenAPIParser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    OpenAPI openAPI = openApiParser.readLocation("reusableParametersWithExternalRef.json", null, options).getOpenAPI();
    assertNotNull(openAPI);
    assertEquals(openAPI.getPaths().get("/pets/{id}").getGet().getParameters().get(0).getIn(), "header");
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.junit.Test)

Example 50 with Header

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

the class OpenAPIDeserializer method getHeader.

public Header getHeader(ObjectNode headerNode, String location, ParseResult result) {
    if (headerNode == null) {
        return null;
    }
    Header header = new Header();
    JsonNode ref = headerNode.get("$ref");
    if (ref != null) {
        if (ref.getNodeType().equals(JsonNodeType.STRING)) {
            String mungedRef = mungedRef(ref.textValue());
            if (mungedRef != null) {
                header.set$ref(mungedRef);
            } else {
                header.set$ref(ref.textValue());
            }
            return header;
        } else {
            result.invalidType(location, "$ref", "string", headerNode);
            return null;
        }
    }
    String value = getString("description", headerNode, false, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        header.setDescription(value);
    }
    Boolean required = getBoolean("required", headerNode, false, location, result);
    if (required != null) {
        header.setRequired(required);
    }
    Boolean deprecated = getBoolean("deprecated", headerNode, false, location, result);
    if (deprecated != null) {
        header.setDeprecated(deprecated);
    }
    Boolean explode = getBoolean("explode", headerNode, false, location, result);
    if (explode != null) {
        header.setExplode(explode);
    } else {
        header.setExplode(Boolean.FALSE);
    }
    header.setStyle(Header.StyleEnum.SIMPLE);
    ObjectNode headerObject = getObject("schema", headerNode, false, location, result);
    if (headerObject != null) {
        header.setSchema(getSchema(headerObject, location, result));
    }
    ObjectNode examplesObject = getObject("examples", headerNode, false, location, result);
    if (examplesObject != null) {
        header.setExamples(getExamples(examplesObject, location, result, false));
    }
    Object example = getAnyExample("example", headerNode, location, result);
    if (example != null) {
        if (examplesObject != null) {
            result.warning(location, "examples already defined -- ignoring \"example\" field");
        } else {
            header.setExample(example instanceof NullNode ? null : example);
        }
    }
    ObjectNode contentNode = getObject("content", headerNode, false, location, result);
    if (contentNode != null) {
        header.setContent(getContent(contentNode, String.format("%s.%s", location, "content"), result));
    }
    Map<String, Object> extensions = getExtensions(headerNode);
    if (extensions != null && extensions.size() > 0) {
        header.setExtensions(extensions);
    }
    Set<String> oAuthFlowKeys = getKeys(headerNode);
    for (String key : oAuthFlowKeys) {
        if (!HEADER_KEYS.contains(key) && !key.startsWith("x-")) {
            result.extra(location, key, headerNode.get(key));
        }
    }
    return header;
}
Also used : Header(io.swagger.v3.oas.models.headers.Header) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) NullNode(com.fasterxml.jackson.databind.node.NullNode)

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