Search in sources :

Example 31 with Parameter

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

the class ParameterSerializationTest method deserializeQueryParameter.

@Test(description = "it should deserialize a QueryParameter")
public void deserializeQueryParameter() throws IOException {
    final String json = "{\"in\":\"query\",\"required\":false,\"type\":\"string\"}";
    final Parameter p = m.readValue(json, Parameter.class);
    SerializationMatchers.assertEqualsToJson(p, json);
}
Also used : BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) Test(org.testng.annotations.Test)

Example 32 with Parameter

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

the class ParameterProcessorTest method parameterProcessorTest.

@Test(description = "parse parameters from method")
public void parameterProcessorTest() throws NoSuchMethodException {
    final Method method = getClass().getDeclaredMethod("parametrizedMethod", String.class, List.class, String.class, String.class, Integer.class);
    final Type[] genericParameterTypes = method.getGenericParameterTypes();
    final Annotation[][] paramAnnotations = method.getParameterAnnotations();
    final PathParameter p1 = (PathParameter) ParameterProcessor.applyAnnotations(null, new PathParameter(), genericParameterTypes[0], Arrays.asList(paramAnnotations[0]));
    assertNotNull(p1);
    assertEquals(p1.getIn(), "path");
    assertEquals(p1.getName(), "paramName1");
    assertEquals(p1.getDescription(), "paramValue1");
    assertEquals(p1.getDefaultValue(), "value1");
    assertTrue(p1.getRequired());
    assertEquals(p1.getEnum(), Arrays.asList("one", "two", "three"));
    assertNull(p1.getAccess());
    final QueryParameter p2 = (QueryParameter) ParameterProcessor.applyAnnotations(null, new QueryParameter().items(new IntegerProperty()), genericParameterTypes[1], Arrays.asList(paramAnnotations[1]));
    assertNotNull(p2);
    final IntegerProperty items = (IntegerProperty) p2.getItems();
    assertNotNull(items);
    assertEquals(p2.getIn(), "query");
    assertEquals(p2.getName(), "paramName2");
    assertNull(p2.getDescription());
    assertEquals((int) items.getDefault(), 10);
    assertFalse(p2.getRequired());
    assertEquals(p2.getAccess(), "test");
    final Parameter p3 = ParameterProcessor.applyAnnotations(null, null, genericParameterTypes[2], Arrays.asList(paramAnnotations[2]));
    assertNull(p3);
    final Parameter p4 = ParameterProcessor.applyAnnotations(null, null, genericParameterTypes[3], Arrays.asList(paramAnnotations[3]));
    assertNull(p4);
    final BodyParameter p5 = (BodyParameter) ParameterProcessor.applyAnnotations(null, null, genericParameterTypes[4], Arrays.asList(paramAnnotations[4]));
    assertNotNull(p5);
    assertEquals(p5.getIn(), "body");
}
Also used : IntegerProperty(io.swagger.models.properties.IntegerProperty) Type(java.lang.reflect.Type) QueryParameter(io.swagger.models.parameters.QueryParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) Method(java.lang.reflect.Method) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) Test(org.testng.annotations.Test)

Example 33 with Parameter

use of io.swagger.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, JsonProcessingException {
    Parameter result = null;
    JsonNode node = jp.getCodec().readTree(jp);
    JsonNode sub = node.get("$ref");
    JsonNode inNode = node.get("in");
    if (sub != null) {
        result = Json.mapper().convertValue(sub, RefParameter.class);
    } else if (inNode != null) {
        String in = inNode.asText();
        if ("query".equals(in)) {
            result = Json.mapper().convertValue(node, QueryParameter.class);
        } else if ("header".equals(in)) {
            result = Json.mapper().convertValue(node, HeaderParameter.class);
        } else if ("path".equals(in)) {
            result = Json.mapper().convertValue(node, PathParameter.class);
        } else if ("formData".equals(in)) {
            result = Json.mapper().convertValue(node, FormParameter.class);
        } else if ("body".equals(in)) {
            result = Json.mapper().convertValue(node, BodyParameter.class);
        } else if ("cookie".equals(in)) {
            result = Json.mapper().convertValue(node, CookieParameter.class);
        }
    }
    return result;
}
Also used : RefParameter(io.swagger.models.parameters.RefParameter) FormParameter(io.swagger.models.parameters.FormParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) RefParameter(io.swagger.models.parameters.RefParameter) CookieParameter(io.swagger.models.parameters.CookieParameter) JsonNode(com.fasterxml.jackson.databind.JsonNode) CookieParameter(io.swagger.models.parameters.CookieParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) FormParameter(io.swagger.models.parameters.FormParameter)

Example 34 with Parameter

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

the class SpecFilter method removeBrokenReferenceDefinitions.

private Swagger removeBrokenReferenceDefinitions(Swagger swagger) {
    if (swagger.getDefinitions() == null || swagger.getDefinitions().isEmpty())
        return swagger;
    Set<String> referencedDefinitions = new TreeSet<String>();
    if (swagger.getResponses() != null) {
        for (Response response : swagger.getResponses().values()) {
            String propertyRef = getPropertyRef(response.getSchema());
            if (propertyRef != null) {
                referencedDefinitions.add(propertyRef);
            }
        }
    }
    if (swagger.getParameters() != null) {
        for (Parameter p : swagger.getParameters().values()) {
            if (p instanceof BodyParameter) {
                BodyParameter bp = (BodyParameter) p;
                Set<String> modelRef = getModelRef(bp.getSchema());
                if (modelRef != null) {
                    referencedDefinitions.addAll(modelRef);
                }
            }
        }
    }
    if (swagger.getPaths() != null) {
        for (Path path : swagger.getPaths().values()) {
            if (path.getParameters() != null) {
                for (Parameter p : path.getParameters()) {
                    if (p instanceof BodyParameter) {
                        BodyParameter bp = (BodyParameter) p;
                        Set<String> modelRef = getModelRef(bp.getSchema());
                        if (modelRef != null) {
                            referencedDefinitions.addAll(modelRef);
                        }
                    }
                }
            }
            if (path.getOperations() != null) {
                for (Operation op : path.getOperations()) {
                    if (op.getResponses() != null) {
                        for (Response response : op.getResponses().values()) {
                            String propertyRef = getPropertyRef(response.getSchema());
                            if (propertyRef != null) {
                                referencedDefinitions.add(propertyRef);
                            }
                        }
                    }
                    if (op.getParameters() != null) {
                        for (Parameter p : op.getParameters()) {
                            if (p instanceof BodyParameter) {
                                BodyParameter bp = (BodyParameter) p;
                                Set<String> modelRef = getModelRef(bp.getSchema());
                                if (modelRef != null) {
                                    referencedDefinitions.addAll(modelRef);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if (swagger.getDefinitions() != null) {
        Set<String> nestedReferencedDefinitions = new TreeSet<String>();
        for (String ref : referencedDefinitions) {
            locateReferencedDefinitions(ref, nestedReferencedDefinitions, swagger);
        }
        referencedDefinitions.addAll(nestedReferencedDefinitions);
        swagger.getDefinitions().keySet().retainAll(referencedDefinitions);
    }
    return swagger;
}
Also used : Response(io.swagger.models.Response) Path(io.swagger.models.Path) TreeSet(java.util.TreeSet) BodyParameter(io.swagger.models.parameters.BodyParameter) Parameter(io.swagger.models.parameters.Parameter) Operation(io.swagger.models.Operation) BodyParameter(io.swagger.models.parameters.BodyParameter)

Example 35 with Parameter

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

the class SpecFilter method filterOperation.

public Operation filterOperation(SwaggerSpecFilter filter, Operation op, ApiDescription api, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
    Operation clonedOperation = new Operation().summary(op.getSummary()).description(op.getDescription()).operationId(op.getOperationId()).schemes(op.getSchemes()).consumes(op.getConsumes()).produces(op.getProduces()).tags(op.getTags()).externalDocs(op.getExternalDocs()).vendorExtensions(op.getVendorExtensions()).deprecated(op.isDeprecated());
    List<Parameter> clonedParams = new ArrayList<Parameter>();
    if (op.getParameters() != null) {
        for (Parameter param : op.getParameters()) {
            if (filter.isParamAllowed(param, op, api, params, cookies, headers)) {
                clonedParams.add(param);
            }
        }
    }
    clonedOperation.setParameters(clonedParams);
    clonedOperation.setSecurity(op.getSecurity());
    clonedOperation.setResponses(op.getResponses());
    return clonedOperation;
}
Also used : ArrayList(java.util.ArrayList) BodyParameter(io.swagger.models.parameters.BodyParameter) Parameter(io.swagger.models.parameters.Parameter) Operation(io.swagger.models.Operation)

Aggregations

Parameter (io.swagger.models.parameters.Parameter)186 BodyParameter (io.swagger.models.parameters.BodyParameter)102 QueryParameter (io.swagger.models.parameters.QueryParameter)101 PathParameter (io.swagger.models.parameters.PathParameter)89 Test (org.testng.annotations.Test)80 HeaderParameter (io.swagger.models.parameters.HeaderParameter)66 Swagger (io.swagger.models.Swagger)63 FormParameter (io.swagger.models.parameters.FormParameter)52 Operation (io.swagger.models.Operation)31 SerializableParameter (io.swagger.models.parameters.SerializableParameter)29 Test (org.junit.Test)25 Type (java.lang.reflect.Type)23 ArrayList (java.util.ArrayList)23 Path (io.swagger.models.Path)19 Annotation (java.lang.annotation.Annotation)17 JavaType (com.fasterxml.jackson.databind.JavaType)14 Response (io.swagger.models.Response)14 Method (java.lang.reflect.Method)14 Property (io.swagger.models.properties.Property)13 ModelImpl (io.swagger.models.ModelImpl)12