Search in sources :

Example 6 with Explode

use of io.swagger.v3.oas.annotations.enums.Explode in project vertx-web by vert-x3.

the class OpenAPI3MultipleFilesValidationTest method testQueryOptionalFormExplodeObjectFailure.

/**
 * Test: query_optional_form_explode_object Expected parameters sent: color:
 * R=100&G=200&B=150&alpha=50 Expected response: Validation failure
 */
@Test
public void testQueryOptionalFormExplodeObjectFailure() throws Exception {
    Operation op = testSpec.getPaths().get("/query/form/explode/object").getGet();
    OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec, refsCache);
    loadHandlers("/query/form/explode/object", HttpMethod.GET, true, validationHandler, routingContext -> {
        RequestParameters params = routingContext.get("parsedParameters");
        RequestParameter colorQueryParam = params.queryParameter("color");
        assertNotNull(colorQueryParam);
        assertTrue(colorQueryParam.isObject());
        routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json").end(((JsonObject) colorQueryParam.toJson()).encode());
    });
    String requestURI = "/query/form/explode/object?R=100&G=200&B=150&alpha=aaa";
    testRequest(HttpMethod.GET, requestURI, 400, errorMessage(ValidationException.ErrorType.NO_MATCH));
}
Also used : OpenAPI3RequestValidationHandlerImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl) RequestParameter(io.vertx.ext.web.api.RequestParameter) Operation(io.swagger.v3.oas.models.Operation) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 7 with Explode

use of io.swagger.v3.oas.annotations.enums.Explode in project vertx-web by vert-x3.

the class OpenAPI3ValidationTest method testQueryOptionalFormExplodeObject.

/**
 * Test: query_optional_form_explode_object
 * Expected parameters sent:
 * color: R=100&G=200&B=150&alpha=50
 * Expected response: {"color":{"R":"100","G":"200","B":"150","alpha":"50"}}
 */
@Test
public void testQueryOptionalFormExplodeObject() throws Exception {
    Operation op = testSpec.getPaths().get("/query/form/explode/object").getGet();
    OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec, refsCache);
    loadHandlers("/query/form/explode/object", HttpMethod.GET, false, validationHandler, routingContext -> {
        RequestParameters params = routingContext.get("parsedParameters");
        RequestParameter colorQueryParam = params.queryParameter("color");
        assertNotNull(colorQueryParam);
        assertTrue(colorQueryParam.isObject());
        routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json").end(((JsonObject) colorQueryParam.toJson()).encode());
    });
    String requestURI = "/query/form/explode/object?R=100&G=200&B=150&alpha=50";
    testEmptyRequestWithJSONObjectResponse(HttpMethod.GET, requestURI, 200, "OK", new JsonObject("{\"R\":\"100\",\"G\":\"200\",\"B\":\"150\",\"alpha\":50}"));
}
Also used : OpenAPI3RequestValidationHandlerImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl) RequestParameter(io.vertx.ext.web.api.RequestParameter) JsonObject(io.vertx.core.json.JsonObject) Operation(io.swagger.v3.oas.models.Operation) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 8 with Explode

use of io.swagger.v3.oas.annotations.enums.Explode in project vertx-web by vert-x3.

the class OpenAPI3ValidationTest method testQueryOptionalFormExplodeObjectFailure.

/**
 * Test: query_optional_form_explode_object
 * Expected parameters sent:
 * color: R=100&G=200&B=150&alpha=50
 * Expected response: Validation failure
 */
@Test
public void testQueryOptionalFormExplodeObjectFailure() throws Exception {
    Operation op = testSpec.getPaths().get("/query/form/explode/object").getGet();
    OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec, refsCache);
    loadHandlers("/query/form/explode/object", HttpMethod.GET, true, validationHandler, routingContext -> {
        RequestParameters params = routingContext.get("parsedParameters");
        RequestParameter colorQueryParam = params.queryParameter("color");
        assertNotNull(colorQueryParam);
        assertTrue(colorQueryParam.isObject());
        routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json").end(((JsonObject) colorQueryParam.toJson()).encode());
    });
    String requestURI = "/query/form/explode/object?R=100&G=200&B=150&alpha=aaa";
    testRequest(HttpMethod.GET, requestURI, 400, errorMessage(ValidationException.ErrorType.NO_MATCH));
}
Also used : OpenAPI3RequestValidationHandlerImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl) RequestParameter(io.vertx.ext.web.api.RequestParameter) Operation(io.swagger.v3.oas.models.Operation) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 9 with Explode

use of io.swagger.v3.oas.annotations.enums.Explode in project swagger-parser by swagger-api.

the class OpenAPIV3ParserTest method testIssue292WithCSVCollectionFormat.

@Test
public void testIssue292WithCSVCollectionFormat() {
    String yaml = "openapi: '3.0.0'\n" + "info:\n" + "  version: '0.0.0'\n" + "  title: nada\n" + "paths:\n" + "  /persons:\n" + "    get:\n" + "      parameters:\n" + "      - name: testParam\n" + "        in: query\n" + "        type: array\n" + "        items:\n" + "          type: string\n" + "        explode: false\n" + "      responses:\n" + "        200:\n" + "          description: Successful response";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    OpenAPI openAPI = result.getOpenAPI();
    Parameter param = openAPI.getPaths().get("/persons").getGet().getParameters().get(0);
    QueryParameter qp = (QueryParameter) param;
    assertTrue(qp.getStyle().toString().equals("form"));
    Assert.assertFalse(qp.getExplode());
}
Also used : QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) HeaderParameter(io.swagger.v3.oas.models.parameters.HeaderParameter) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) 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 10 with Explode

use of io.swagger.v3.oas.annotations.enums.Explode in project swagger-parser by swagger-api.

the class OpenAPIDeserializer method getParameter.

public Parameter getParameter(ObjectNode obj, String location, ParseResult result) {
    if (obj == null) {
        return null;
    }
    Parameter parameter = null;
    JsonNode ref = obj.get("$ref");
    if (ref != null) {
        if (ref.getNodeType().equals(JsonNodeType.STRING)) {
            parameter = new Parameter();
            String mungedRef = mungedRef(ref.textValue());
            if (mungedRef != null) {
                parameter.set$ref(mungedRef);
            } else {
                parameter.set$ref(ref.textValue());
            }
            if (result.isOpenapi31()) {
                String desc = getString("description", obj, false, location, result);
                if (StringUtils.isNotBlank(desc)) {
                    parameter.setDescription(desc);
                }
            }
            return parameter;
        } else {
            result.invalidType(location, "$ref", "string", obj);
            return null;
        }
    }
    String l = null;
    JsonNode ln = obj.get("name");
    if (ln != null) {
        l = ln.asText();
    } else {
        l = "['unknown']";
    }
    location += ".[" + l + "]";
    String value = getString("in", obj, true, location, result);
    if (!result.isAllowEmptyStrings() && StringUtils.isBlank(value) || result.isAllowEmptyStrings() && value == null) {
        return null;
    }
    if (QUERY_PARAMETER.equals(value)) {
        parameter = new QueryParameter();
    } else if (HEADER_PARAMETER.equals(value)) {
        parameter = new HeaderParameter();
    } else if (PATH_PARAMETER.equals(value)) {
        parameter = new PathParameter();
    } else if (COOKIE_PARAMETER.equals(value)) {
        parameter = new CookieParameter();
    }
    if (parameter == null) {
        result.invalidType(location, "in", "[query|header|path|cookie]", obj);
        return null;
    }
    parameter.setIn(value);
    value = getString("name", obj, true, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        parameter.setName(value);
    }
    value = getString("description", obj, false, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        parameter.setDescription(value);
    }
    Boolean required = getBoolean("required", obj, false, location, result);
    if (required != null) {
        parameter.setRequired(required);
    } else {
        parameter.setRequired(false);
    }
    Boolean deprecated = getBoolean("deprecated", obj, false, location, result);
    if (deprecated != null) {
        parameter.setDeprecated(deprecated);
    }
    if (parameter instanceof QueryParameter) {
        Boolean allowEmptyValue = getBoolean("allowEmptyValue", obj, false, location, result);
        if (allowEmptyValue != null) {
            parameter.setAllowEmptyValue(allowEmptyValue);
        }
    }
    ObjectNode parameterObject = getObject("schema", obj, false, location, result);
    if (parameterObject != null) {
        parameter.setSchema(getSchema(parameterObject, String.format("%s.%s", location, "schemas"), result));
    }
    ObjectNode examplesObject = getObject("examples", obj, false, location, result);
    if (examplesObject != null) {
        parameter.setExamples(getExamples(examplesObject, String.format("%s.%s", location, "examples"), result, false));
    }
    Object example = getAnyType("example", obj, location, result);
    if (example != null) {
        if (examplesObject != null) {
            result.warning(location, "examples already defined -- ignoring \"example\" field");
        } else {
            parameter.setExample(example instanceof NullNode ? null : example);
        }
    }
    Boolean allowReserved = getBoolean("allowReserved", obj, false, location, result);
    if (allowReserved != null) {
        parameter.setAllowReserved(allowReserved);
    }
    ObjectNode contentNode = getObject("content", obj, false, location, result);
    if (contentNode != null) {
        Content content = getContent(contentNode, String.format("%s.%s", location, "content"), result);
        if (content.size() == 0) {
            result.unsupported(location, "content with no media type", contentNode);
            result.invalid();
        } else if (content.size() > 1) {
            result.unsupported(location, "content with multiple media types", contentNode);
            result.invalid();
        } else if (parameter.getSchema() != null) {
            result.unsupported(location, "content when schema defined", contentNode);
            result.invalid();
        } else {
            parameter.setContent(content);
        }
    } else if (parameter.getSchema() == null) {
        result.missing(location, "content");
    }
    value = getString("style", obj, false, location, result);
    if (parameter.getContent() == null) {
        setStyle(value, parameter, location, obj, result);
        Boolean explode = getBoolean("explode", obj, false, location, result);
        if (explode != null) {
            parameter.setExplode(explode);
        } else if (StyleEnum.FORM.equals(parameter.getStyle())) {
            parameter.setExplode(Boolean.TRUE);
        } else {
            parameter.setExplode(Boolean.FALSE);
        }
    }
    Map<String, Object> extensions = getExtensions(obj);
    if (extensions != null && extensions.size() > 0) {
        parameter.setExtensions(extensions);
    }
    Set<String> keys = getKeys(obj);
    Map<String, Set<String>> specKeys = KEYS.get(result.isOpenapi31() ? "openapi31" : "openapi30");
    for (String key : keys) {
        if (!specKeys.get("PARAMETER_KEYS").contains(key) && !key.startsWith("x-")) {
            result.extra(location, key, obj.get(key));
        }
        validateReservedKeywords(specKeys, key, location, result);
    }
    return parameter;
}
Also used : QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) Content(io.swagger.v3.oas.models.media.Content) 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) CookieParameter(io.swagger.v3.oas.models.parameters.CookieParameter) HeaderParameter(io.swagger.v3.oas.models.parameters.HeaderParameter) NullNode(com.fasterxml.jackson.databind.node.NullNode)

Aggregations

Parameter (io.swagger.v3.oas.models.parameters.Parameter)7 Operation (io.swagger.v3.oas.models.Operation)5 Schema (io.swagger.v3.oas.models.media.Schema)5 RequestParameter (io.vertx.ext.web.api.RequestParameter)4 RequestParameters (io.vertx.ext.web.api.RequestParameters)4 OpenAPI3RequestValidationHandlerImpl (io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)3 Map (java.util.Map)3 Optional (java.util.Optional)3 Collectors (java.util.stream.Collectors)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 NullNode (com.fasterxml.jackson.databind.node.NullNode)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Content (io.swagger.v3.oas.models.media.Content)2 Encoding (io.swagger.v3.oas.models.media.Encoding)2 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)2