Search in sources :

Example 6 with Header

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

the class OpenAPIResolverTest method testIssue1352.

@Test
public void testIssue1352(@Injectable final List<AuthorizationValue> auths) {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setResolveFully(true);
    OpenAPI openAPI = new OpenAPIV3Parser().readLocation("issue-1352.json", auths, options).getOpenAPI();
    assertNull(openAPI.getPaths().get("/responses").getPatch().getResponses().get("200").getHeaders().get("x-my-secret-header").getSchema().get$ref());
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 7 with Header

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

the class OpenAPIV3ParserTest method testValidateExternalRefsTrue.

@Test(description = "option true, adds Original Location to messages when ref is relative/local")
public void testValidateExternalRefsTrue() {
    ParseOptions options = new ParseOptions();
    options.setValidateExternalRefs(true);
    options.setResolve(true);
    SwaggerParseResult result = new OpenAPIV3Parser().readLocation("./swos-443/root.yaml", null, options);
    OpenAPI openAPI = result.getOpenAPI();
    assertNotNull(openAPI);
    assertNotNull(result.getMessages());
    assertEquals(result.getMessages().size(), 19);
    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"));
    assertTrue(result.getMessages().contains("attribute components.schemas.ErrorModel.properties is not of type `object` (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.schemas.Examples.properties is not of type `object` (./ref.yaml)"));
}
Also used : 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.models.headers.Header in project swagger-parser by swagger-api.

the class OpenAPIDeserializer method getHeaders.

public Map<String, Header> getHeaders(ObjectNode obj, String location, ParseResult result, boolean underComponents) {
    if (obj == null) {
        return null;
    }
    Map<String, Header> headers = new LinkedHashMap<>();
    Set<String> headerKeys = getKeys(obj);
    for (String headerName : headerKeys) {
        if (underComponents) {
            if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$", headerName)) {
                result.warning(location, "Header name " + headerName + " doesn't adhere to regular expression " + "^[a-zA-Z0-9\\.\\-_]+$");
            }
        }
        JsonNode headerValue = obj.get(headerName);
        if (!headerValue.getNodeType().equals(JsonNodeType.OBJECT)) {
            result.invalidType(location, headerName, "object", headerValue);
        } else {
            ObjectNode header = (ObjectNode) headerValue;
            Header headerObj = getHeader(header, String.format("%s.%s", location, headerName), result);
            if (headerObj != null) {
                headers.put(headerName, headerObj);
            }
        }
    }
    return headers;
}
Also used : Header(io.swagger.v3.oas.models.headers.Header) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 9 with Header

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

the class OpenAPIDeserializer method getResponse.

public ApiResponse getResponse(ObjectNode node, String location, ParseResult result) {
    if (node == null) {
        return null;
    }
    ApiResponse apiResponse = new ApiResponse();
    JsonNode ref = node.get("$ref");
    if (ref != null) {
        if (ref.getNodeType().equals(JsonNodeType.STRING)) {
            String mungedRef = mungedRef(ref.textValue());
            if (mungedRef != null) {
                apiResponse.set$ref(mungedRef);
            } else {
                apiResponse.set$ref(ref.textValue());
            }
            return apiResponse;
        } else {
            result.invalidType(location, "$ref", "string", node);
            return null;
        }
    }
    String value = getString("description", node, true, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        apiResponse.description(value);
    }
    ObjectNode headerObject = getObject("headers", node, false, location, result);
    if (headerObject != null) {
        Map<String, Header> headers = getHeaders(headerObject, location, result, false);
        if (headers != null && headers.size() > 0) {
            apiResponse.setHeaders(headers);
        }
    }
    ObjectNode linksObj = getObject("links", node, false, location, result);
    if (linksObj != null) {
        Map<String, Link> links = getLinks(linksObj, location, result, false);
        if (links != null && links.size() > 0) {
            apiResponse.setLinks(links);
        }
    }
    ObjectNode contentObject = getObject("content", node, false, location, result);
    if (contentObject != null) {
        apiResponse.setContent(getContent(contentObject, String.format("%s.%s", location, "content"), result));
    }
    Map<String, Object> extensions = getExtensions(node);
    if (extensions != null && extensions.size() > 0) {
        apiResponse.setExtensions(extensions);
    }
    Set<String> keys = getKeys(node);
    for (String key : keys) {
        if (!RESPONSE_KEYS.contains(key) && !key.startsWith("x-")) {
            result.extra(location, key, node.get(key));
        }
    }
    return apiResponse;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Header(io.swagger.v3.oas.models.headers.Header) JsonNode(com.fasterxml.jackson.databind.JsonNode) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Link(io.swagger.v3.oas.models.links.Link)

Example 10 with Header

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

the class RemoteUrl method urlToString.

public static String urlToString(String url, List<AuthorizationValue> auths) throws Exception {
    InputStream is = null;
    BufferedReader br = null;
    try {
        URLConnection conn;
        do {
            final URL inUrl = new URL(cleanUrl(url));
            final List<AuthorizationValue> query = new ArrayList<>();
            final List<AuthorizationValue> header = new ArrayList<>();
            if (auths != null && auths.size() > 0) {
                for (AuthorizationValue auth : auths) {
                    if (auth.getUrlMatcher() != null) {
                        if (auth.getUrlMatcher().test(inUrl)) {
                            if ("query".equals(auth.getType())) {
                                appendValue(inUrl, auth, query);
                            } else if ("header".equals(auth.getType())) {
                                appendValue(inUrl, auth, header);
                            }
                        }
                    }
                }
            }
            if (!query.isEmpty()) {
                final URI inUri = inUrl.toURI();
                final StringBuilder newQuery = new StringBuilder(inUri.getQuery() == null ? "" : inUri.getQuery());
                for (AuthorizationValue item : query) {
                    if (newQuery.length() > 0) {
                        newQuery.append("&");
                    }
                    newQuery.append(URLEncoder.encode(item.getKeyName(), UTF_8.name())).append("=").append(URLEncoder.encode(item.getValue(), UTF_8.name()));
                }
                conn = new URI(inUri.getScheme(), inUri.getAuthority(), inUri.getPath(), newQuery.toString(), inUri.getFragment()).toURL().openConnection();
            } else {
                conn = inUrl.openConnection();
            }
            CONNECTION_CONFIGURATOR.process(conn);
            for (AuthorizationValue item : header) {
                conn.setRequestProperty(item.getKeyName(), item.getValue());
            }
            conn.setRequestProperty("Accept", ACCEPT_HEADER_VALUE);
            conn.setRequestProperty("User-Agent", USER_AGENT_HEADER_VALUE);
            conn.connect();
            url = ((HttpURLConnection) conn).getHeaderField("Location");
        } while ((301 == ((HttpURLConnection) conn).getResponseCode()) || (302 == ((HttpURLConnection) conn).getResponseCode()) || (307 == ((HttpURLConnection) conn).getResponseCode()) || (308 == ((HttpURLConnection) conn).getResponseCode()));
        InputStream in = conn.getInputStream();
        StringBuilder contents = new StringBuilder();
        BufferedReader input = new BufferedReader(new InputStreamReader(in, UTF_8));
        for (int i = 0; i != -1; i = input.read()) {
            char c = (char) i;
            if (!Character.isISOControl(c)) {
                contents.append((char) i);
            }
            if (c == '\n') {
                contents.append('\n');
            }
        }
        in.close();
        return contents.toString();
    } catch (javax.net.ssl.SSLProtocolException e) {
        LOGGER.warn("there is a problem with the target SSL certificate");
        LOGGER.warn("**** you may want to run with -Djsse.enableSNIExtension=false\n\n");
        LOGGER.error("unable to read", e);
        throw e;
    } catch (Exception e) {
        LOGGER.error("unable to read", e);
        throw e;
    } finally {
        if (is != null) {
            is.close();
        }
        if (br != null) {
            br.close();
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) KeyManagementException(java.security.KeyManagementException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AuthorizationValue(io.swagger.v3.parser.core.models.AuthorizationValue) BufferedReader(java.io.BufferedReader)

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