Search in sources :

Example 21 with Parameter

use of io.swagger.v3.oas.models.parameters.Parameter in project swagger-core by swagger-api.

the class SpecFilterTest method filterAwayQueryParameters.

@Test(description = "it should filter any query parameter")
public void filterAwayQueryParameters() throws IOException {
    final OpenAPI openAPI = getOpenAPI(RESOURCE_PATH);
    final OpenAPI filtered = new SpecFilter().filter(openAPI, new NoParametersWithoutQueryInFilter(), null, null, null);
    if (filtered.getPaths() != null) {
        for (Map.Entry<String, PathItem> entry : filtered.getPaths().entrySet()) {
            validateParameters(entry.getValue().getGet());
            validateParameters(entry.getValue().getPost());
            validateParameters(entry.getValue().getPut());
            validateParameters(entry.getValue().getPatch());
            validateParameters(entry.getValue().getHead());
            validateParameters(entry.getValue().getDelete());
            validateParameters(entry.getValue().getOptions());
        }
    }
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) NoParametersWithoutQueryInFilter(io.swagger.v3.core.filter.resources.NoParametersWithoutQueryInFilter) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.testng.annotations.Test)

Example 22 with Parameter

use of io.swagger.v3.oas.models.parameters.Parameter in project swagger-core by swagger-api.

the class OpenAPI3_1DeserializationTest method testRefDeserializationOnOAS31.

@Test
public void testRefDeserializationOnOAS31() throws IOException {
    final String jsonString = ResourceUtils.loadClassResource(getClass(), "specFiles/3.1.0/petstore-3.1_refs_siblings.yaml");
    OpenAPI openAPI = Yaml31.mapper().readValue(jsonString, OpenAPI.class);
    assertEquals(openAPI.getPaths().get("/ref_pet").get$ref(), "#/components/pathItems/pet");
    assertEquals(openAPI.getPaths().get("/ref_pet").getDescription(), "ref pathItem description");
    assertEquals(openAPI.getPaths().get("/ref_pet").getSummary(), "ref pathItem summary");
    assertEquals(openAPI.getPaths().get("/pets").getPost().getParameters().get(0).get$ref(), "#/components/parameters/testParameter");
    assertEquals(openAPI.getPaths().get("/pets").getPost().getParameters().get(0).getDescription(), "ref parameter description");
    assertEquals(openAPI.getPaths().get("/pets").getPost().getParameters().get(1).getName(), "randomParam");
    assertEquals(openAPI.getPaths().get("/pets").getPost().getParameters().get(1).getIn(), "query");
    assertEquals(openAPI.getPaths().get("/pets").getPost().getParameters().get(1).getExamples().get("refExample").get$ref(), "#/components/examples/testExample");
    assertEquals(openAPI.getPaths().get("/pets").getPost().getParameters().get(1).getExamples().get("refExample").getDescription(), "ref example description");
    assertEquals(openAPI.getPaths().get("/pets").getPost().getParameters().get(1).getExamples().get("refExample").getSummary(), "ref example summary");
    assertEquals(openAPI.getPaths().get("/pets").getPost().getCallbacks().get("callIt").get$ref(), "#/components/callbacks/TestCallback");
    assertEquals(openAPI.getPaths().get("/pets").getPost().getRequestBody().get$ref(), "#/components/requestBodies/body");
    assertEquals(openAPI.getPaths().get("/pets").getPost().getRequestBody().getDescription(), "ref request body description");
    assertEquals(openAPI.getPaths().get("/pets").getPost().getResponses().get("201").get$ref(), "#/components/responses/okResponse");
    assertEquals(openAPI.getPaths().get("/pets").getPost().getResponses().get("201").getDescription(), "ref response description");
    assertEquals(openAPI.getPaths().get("/pets").getPost().getResponses().get("default").getHeaders().get("head").get$ref(), "#/components/headers/head");
    assertEquals(openAPI.getPaths().get("/pets").getPost().getResponses().get("default").getHeaders().get("head").getDescription(), "ref header description");
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 23 with Parameter

use of io.swagger.v3.oas.models.parameters.Parameter in project swagger-core by swagger-api.

the class SpecFilter method filterParameter.

protected Parameter filterParameter(OpenAPISpecFilter filter, Operation operation, Parameter parameter, String resourcePath, String key, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
    if (parameter != null) {
        ApiDescription description = new ApiDescription(resourcePath, key);
        Optional<Parameter> filteredParameter = filter.filterParameter(parameter, operation, description, params, cookies, headers);
        if (filteredParameter.isPresent()) {
            return filteredParameter.get();
        }
    }
    return null;
}
Also used : Parameter(io.swagger.v3.oas.models.parameters.Parameter) ApiDescription(io.swagger.v3.core.model.ApiDescription)

Example 24 with Parameter

use of io.swagger.v3.oas.models.parameters.Parameter in project swagger-core by swagger-api.

the class SecurityDefinitionTest method createModelWithSecurityRequirements.

@Test(description = "it should create a model with security requirements")
public void createModelWithSecurityRequirements() throws IOException {
    final Schema personModel = ModelConverters.getInstance().read(Person.class).get("Person");
    final Schema errorModel = ModelConverters.getInstance().read(Error.class).get("Error");
    final Info info = new Info().version("1.0.0").title("Swagger Petstore");
    final Contact contact = new Contact().name("Swagger API Team").email("foo@bar.baz").url("http://swagger.io");
    info.setContact(contact);
    final OpenAPI oas = new OpenAPI().info(info).addServersItem(new Server().url("http://petstore.swagger.io")).schema("Person", personModel).schema("Error", errorModel);
    oas.schemaRequirement("githubAccessCode", new SecurityScheme().flows(new OAuthFlows().authorizationCode(new OAuthFlow().scopes(new Scopes().addString("user:email", "Grants read access to a user’s email addresses.")))));
    final Operation get = new Operation().summary("finds pets in the system").description("a longer description").addTagsItem("Pet Operations").operationId("get pet by id");
    get.addParametersItem(new Parameter().in("query").name("tags").description("tags to filter by").required(false).schema(new StringSchema()));
    get.addParametersItem(new Parameter().in("path").name("petId").description("pet to fetch").schema(new IntegerSchema().format("int64")));
    final ApiResponse response = new ApiResponse().description("pets returned").content(new Content().addMediaType("*/*", new MediaType().schema(new Schema().$ref("Person"))));
    final ApiResponse errorResponse = new ApiResponse().description("error response").content(new Content().addMediaType("*/*", new MediaType().schema(new Schema().$ref("Error"))));
    get.responses(new ApiResponses().addApiResponse("200", response).addApiResponse("default", errorResponse)).addSecurityItem(new SecurityRequirement().addList("internal_oauth2", "user:email")).addSecurityItem(new SecurityRequirement().addList("api_key"));
    oas.path("/pets", new PathItem().get(get));
    final String json = ResourceUtils.loadClassResource(getClass(), "ModelWithSecurityRequirements.json");
    SerializationMatchers.assertEqualsToJson(oas, json);
}
Also used : Server(io.swagger.v3.oas.models.servers.Server) OAuthFlows(io.swagger.v3.oas.models.security.OAuthFlows) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) Operation(io.swagger.v3.oas.models.Operation) Info(io.swagger.v3.oas.models.info.Info) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Contact(io.swagger.v3.oas.models.info.Contact) PathItem(io.swagger.v3.oas.models.PathItem) OAuthFlow(io.swagger.v3.oas.models.security.OAuthFlow) Scopes(io.swagger.v3.oas.models.security.Scopes) Content(io.swagger.v3.oas.models.media.Content) Parameter(io.swagger.v3.oas.models.parameters.Parameter) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Person(io.swagger.v3.core.oas.models.Person) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement) Test(org.testng.annotations.Test)

Example 25 with Parameter

use of io.swagger.v3.oas.models.parameters.Parameter in project swagger-core by swagger-api.

the class ParameterSerializationTest method serializePathParameter.

@Test(description = "it should serialize a PathParameter")
public void serializePathParameter() {
    final Parameter p = new PathParameter().schema(new StringSchema());
    final String json = "{\"in\":\"path\",\"required\":true,\"schema\":{\"type\":\"string\"}}";
    SerializationMatchers.assertEqualsToJson(p, json);
}
Also used : 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) StringSchema(io.swagger.v3.oas.models.media.StringSchema) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) Test(org.testng.annotations.Test)

Aggregations

Operation (io.swagger.v3.oas.annotations.Operation)59 Parameter (io.swagger.v3.oas.models.parameters.Parameter)48 Test (org.testng.annotations.Test)39 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)33 Operation (io.swagger.v3.oas.models.Operation)28 lombok.val (lombok.val)26 OpenAPI (io.swagger.v3.oas.models.OpenAPI)20 Map (java.util.Map)18 ArrayList (java.util.ArrayList)16 ReadOperation (org.springframework.boot.actuate.endpoint.annotation.ReadOperation)15 OpenAPI3RequestValidationHandlerImpl (io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl)14 Test (org.junit.Test)14 PathItem (io.swagger.v3.oas.models.PathItem)13 QueryParameter (io.swagger.v3.oas.models.parameters.QueryParameter)13 StringUtils (org.apache.commons.lang3.StringUtils)13 Parameter (io.swagger.v3.oas.annotations.Parameter)12 StringSchema (io.swagger.v3.oas.models.media.StringSchema)12 LinkedHashMap (java.util.LinkedHashMap)12 RequestParameters (io.vertx.ext.web.api.RequestParameters)11 HashMap (java.util.HashMap)11