Search in sources :

Example 76 with Parameter

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

the class BeanParamTest method shouldSerializeTypeParameter.

// tests issue #2466
@Test(description = "check array type of serialized BeanParam containing QueryParams")
public void shouldSerializeTypeParameter() {
    OpenAPI openApi = new Reader(new OpenAPI()).read(MyBeanParamResource.class);
    List<Parameter> getOperationParams = openApi.getPaths().get("/").getGet().getParameters();
    Assert.assertEquals(getOperationParams.size(), 1);
    Parameter param = getOperationParams.get(0);
    Assert.assertEquals(param.getName(), "listOfStrings");
    Schema<?> schema = param.getSchema();
    // These are the important checks:
    Assert.assertEquals(schema.getClass(), ArraySchema.class);
    Assert.assertEquals(((ArraySchema) schema).getItems().getType(), "string");
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Parameter(io.swagger.v3.oas.models.parameters.Parameter) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 77 with Parameter

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

the class ReaderTest method test2818.

@Test(description = "test ticket #2818 @Parameter annotation")
public void test2818() {
    Reader reader = new Reader(new OpenAPI());
    OpenAPI openAPI = reader.read(Ticket2818Resource.class);
    Paths paths = openAPI.getPaths();
    assertEquals(paths.size(), 1);
    PathItem pathItem = paths.get("/bookstore/{id}");
    assertNotNull(pathItem);
    Operation operation = pathItem.getGet();
    assertNotNull(operation);
    assertEquals(operation.getParameters().get(0).getSchema().getType(), "integer");
    assertEquals(operation.getParameters().get(0).getSchema().getFormat(), "int32");
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) Paths(io.swagger.v3.oas.models.Paths) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 78 with Parameter

use of io.swagger.v3.oas.models.parameters.Parameter 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)

Example 79 with Parameter

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

the class SpecFilter method filterOperation.

protected Operation filterOperation(OpenAPISpecFilter filter, Operation operation, String resourcePath, String key, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
    if (operation != null) {
        ApiDescription description = new ApiDescription(resourcePath, key);
        Optional<Operation> filteredOperation = filter.filterOperation(operation, description, params, cookies, headers);
        if (filteredOperation.isPresent()) {
            List<Parameter> filteredParameters = new ArrayList<>();
            Operation filteredOperationGet = filteredOperation.get();
            Operation clone = new Operation();
            clone.setCallbacks(filteredOperationGet.getCallbacks());
            clone.setDeprecated(filteredOperationGet.getDeprecated());
            clone.setDescription(filteredOperationGet.getDescription());
            clone.setExtensions(filteredOperationGet.getExtensions());
            clone.setExternalDocs(filteredOperationGet.getExternalDocs());
            clone.setOperationId(filteredOperationGet.getOperationId());
            clone.setSecurity(filteredOperationGet.getSecurity());
            clone.setServers(filteredOperationGet.getServers());
            clone.setSummary(filteredOperationGet.getSummary());
            clone.setTags(filteredOperationGet.getTags());
            List<Parameter> parameters = filteredOperationGet.getParameters();
            if (parameters != null) {
                for (Parameter parameter : parameters) {
                    Parameter filteredParameter = filterParameter(filter, operation, parameter, resourcePath, key, params, cookies, headers);
                    if (filteredParameter != null) {
                        filteredParameters.add(filteredParameter);
                    }
                }
                clone.setParameters(filteredParameters);
            }
            RequestBody requestBody = filteredOperation.get().getRequestBody();
            if (requestBody != null) {
                RequestBody filteredRequestBody = filterRequestBody(filter, operation, requestBody, resourcePath, key, params, cookies, headers);
                clone.setRequestBody(filteredRequestBody);
            }
            ApiResponses responses = filteredOperation.get().getResponses();
            ApiResponses clonedResponses = responses;
            if (responses != null) {
                responses.forEach((responseKey, response) -> {
                    ApiResponse filteredResponse = filterResponse(filter, operation, response, resourcePath, key, params, cookies, headers);
                    if (filteredResponse != null) {
                        clonedResponses.addApiResponse(responseKey, filteredResponse);
                    }
                });
                clone.setResponses(clonedResponses);
            }
            return clone;
        }
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) Parameter(io.swagger.v3.oas.models.parameters.Parameter) Operation(io.swagger.v3.oas.models.Operation) ApiDescription(io.swagger.v3.core.model.ApiDescription) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody)

Example 80 with Parameter

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

the class SpecFilter method addPathItemSchemaRef.

private void addPathItemSchemaRef(PathItem pathItem, Set<String> referencedDefinitions) {
    if (pathItem.getParameters() != null) {
        for (Parameter parameter : pathItem.getParameters()) {
            addSchemaRef(parameter.getSchema(), referencedDefinitions);
            addContentSchemaRef(parameter.getContent(), referencedDefinitions);
        }
    }
    Map<PathItem.HttpMethod, Operation> ops = pathItem.readOperationsMap();
    for (Operation op : ops.values()) {
        if (op.getRequestBody() != null) {
            addContentSchemaRef(op.getRequestBody().getContent(), referencedDefinitions);
        }
        if (op.getResponses() != null) {
            for (String keyResponses : op.getResponses().keySet()) {
                ApiResponse response = op.getResponses().get(keyResponses);
                if (response.getHeaders() != null) {
                    for (String keyHeaders : response.getHeaders().keySet()) {
                        Header header = response.getHeaders().get(keyHeaders);
                        addSchemaRef(header.getSchema(), referencedDefinitions);
                        addContentSchemaRef(header.getContent(), referencedDefinitions);
                    }
                }
                addContentSchemaRef(response.getContent(), referencedDefinitions);
            }
        }
        if (op.getParameters() != null) {
            for (Parameter parameter : op.getParameters()) {
                addSchemaRef(parameter.getSchema(), referencedDefinitions);
                addContentSchemaRef(parameter.getContent(), referencedDefinitions);
            }
        }
        if (op.getCallbacks() != null) {
            for (String keyCallback : op.getCallbacks().keySet()) {
                Callback callback = op.getCallbacks().get(keyCallback);
                for (PathItem callbackPathItem : callback.values()) {
                    addPathItemSchemaRef(callbackPathItem, referencedDefinitions);
                }
            }
        }
    }
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) Callback(io.swagger.v3.oas.models.callbacks.Callback) Header(io.swagger.v3.oas.models.headers.Header) Parameter(io.swagger.v3.oas.models.parameters.Parameter) Operation(io.swagger.v3.oas.models.Operation) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse)

Aggregations

Parameter (io.swagger.v3.oas.models.parameters.Parameter)48 Test (org.testng.annotations.Test)39 Operation (io.swagger.v3.oas.models.Operation)28 Operation (io.swagger.v3.oas.annotations.Operation)27 lombok.val (lombok.val)26 OpenAPI (io.swagger.v3.oas.models.OpenAPI)20 Map (java.util.Map)17 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 StringSchema (io.swagger.v3.oas.models.media.StringSchema)12 LinkedHashMap (java.util.LinkedHashMap)12 Parameter (io.swagger.v3.oas.annotations.Parameter)11 RequestParameters (io.vertx.ext.web.api.RequestParameters)11 HashMap (java.util.HashMap)11 Schema (io.swagger.v3.oas.models.media.Schema)10