Search in sources :

Example 11 with PathParameter

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

the class ParameterProcessorTest method resourceWithParamRangeTest.

@Test
public void resourceWithParamRangeTest() throws NoSuchMethodException {
    final Method method = getClass().getDeclaredMethod("rangedParametrizedMethod", Integer.class, Double.class, Integer.class, Integer.class);
    final Type[] genericParameterTypes = method.getGenericParameterTypes();
    final Annotation[][] paramAnnotations = method.getParameterAnnotations();
    final PathParameter param0 = (PathParameter) ParameterProcessor.applyAnnotations(null, new PathParameter(), genericParameterTypes[0], Arrays.asList(paramAnnotations[0]));
    assertNotNull(param0);
    assertEquals(param0.getDefaultValue(), "5");
    assertEquals(param0.getMinimum(), new BigDecimal(0.0));
    assertEquals(param0.getMaximum(), new BigDecimal(10.0));
    assertEquals(param0.getCollectionFormat(), "multi");
    final PathParameter param1 = (PathParameter) ParameterProcessor.applyAnnotations(null, new PathParameter(), genericParameterTypes[1], Arrays.asList(paramAnnotations[1]));
    assertNotNull(param1);
    assertEquals(param1.getMinimum(), new BigDecimal(0.0));
    assertNull(param1.getMaximum(), null);
    assertTrue(param1.isExclusiveMinimum());
    assertTrue(param1.isExclusiveMaximum());
    final PathParameter param2 = (PathParameter) ParameterProcessor.applyAnnotations(null, new PathParameter(), genericParameterTypes[2], Arrays.asList(paramAnnotations[2]));
    assertNotNull(param2);
    assertNull(param2.getMinimum());
    assertEquals(param2.getMaximum(), new BigDecimal(100.0));
    final PathParameter param3 = (PathParameter) ParameterProcessor.applyAnnotations(null, new PathParameter().items(new IntegerProperty()), genericParameterTypes[3], Arrays.asList(paramAnnotations[3]));
    assertNotNull(param3);
    final IntegerProperty items = (IntegerProperty) param3.getItems();
    assertNotNull(items);
    assertEquals(items.getMinimum(), new BigDecimal(0.0));
    assertEquals(items.getMaximum(), new BigDecimal(5.0));
    assertTrue(items.getExclusiveMinimum());
    assertTrue(items.getExclusiveMaximum());
}
Also used : IntegerProperty(io.swagger.models.properties.IntegerProperty) Type(java.lang.reflect.Type) Method(java.lang.reflect.Method) PathParameter(io.swagger.models.parameters.PathParameter) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 12 with PathParameter

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

the class ParameterSerializationTest method serializeEnumPathParameter.

@Test(description = "it should serialize a path parameter with enum")
public void serializeEnumPathParameter() {
    PathParameter p = new PathParameter().items(new StringProperty())._enum(Arrays.asList("a", "b", "c"));
    final String json = "{" + "   \"in\":\"path\"," + "   \"required\":true," + "   \"items\":{" + "      \"type\":\"string\"" + "   }," + "   \"enum\":[\"a\",\"b\",\"c\"]" + "}";
    SerializationMatchers.assertEqualsToJson(p, json);
}
Also used : StringProperty(io.swagger.models.properties.StringProperty) PathParameter(io.swagger.models.parameters.PathParameter) Test(org.testng.annotations.Test)

Example 13 with PathParameter

use of io.swagger.models.parameters.PathParameter in project camel by apache.

the class RestSwaggerReader method doParseVerbs.

private void doParseVerbs(Swagger swagger, RestDefinition rest, String camelContextId, List<VerbDefinition> verbs, String pathAsTag) {
    // used during gathering of apis
    List<Path> paths = new ArrayList<>();
    String basePath = rest.getPath();
    for (VerbDefinition verb : verbs) {
        // check if the Verb Definition must be excluded from documentation
        Boolean apiDocs;
        if (verb.getApiDocs() != null) {
            apiDocs = verb.getApiDocs();
        } else {
            // fallback to option on rest
            apiDocs = rest.getApiDocs();
        }
        if (apiDocs != null && !apiDocs) {
            continue;
        }
        // the method must be in lower case
        String method = verb.asVerb().toLowerCase(Locale.US);
        // operation path is a key
        String opPath = SwaggerHelper.buildUrl(basePath, verb.getUri());
        Operation op = new Operation();
        if (ObjectHelper.isNotEmpty(pathAsTag)) {
            // group in the same tag
            op.addTag(pathAsTag);
        }
        final String routeId = verb.getRouteId();
        final String operationId = Optional.ofNullable(rest.getId()).orElse(routeId);
        op.operationId(operationId);
        // add id as vendor extensions
        op.getVendorExtensions().put("x-camelContextId", camelContextId);
        op.getVendorExtensions().put("x-routeId", routeId);
        Path path = swagger.getPath(opPath);
        if (path == null) {
            path = new Path();
            paths.add(path);
        }
        path = path.set(method, op);
        String consumes = verb.getConsumes() != null ? verb.getConsumes() : rest.getConsumes();
        if (consumes != null) {
            String[] parts = consumes.split(",");
            for (String part : parts) {
                op.addConsumes(part);
            }
        }
        String produces = verb.getProduces() != null ? verb.getProduces() : rest.getProduces();
        if (produces != null) {
            String[] parts = produces.split(",");
            for (String part : parts) {
                op.addProduces(part);
            }
        }
        if (verb.getDescriptionText() != null) {
            op.summary(verb.getDescriptionText());
        }
        for (RestOperationParamDefinition param : verb.getParams()) {
            Parameter parameter = null;
            if (param.getType().equals(RestParamType.body)) {
                parameter = new BodyParameter();
            } else if (param.getType().equals(RestParamType.formData)) {
                parameter = new FormParameter();
            } else if (param.getType().equals(RestParamType.header)) {
                parameter = new HeaderParameter();
            } else if (param.getType().equals(RestParamType.path)) {
                parameter = new PathParameter();
            } else if (param.getType().equals(RestParamType.query)) {
                parameter = new QueryParameter();
            }
            if (parameter != null) {
                parameter.setName(param.getName());
                parameter.setDescription(param.getDescription());
                parameter.setRequired(param.getRequired());
                // set type on parameter
                if (parameter instanceof SerializableParameter) {
                    SerializableParameter serializableParameter = (SerializableParameter) parameter;
                    if (param.getDataType() != null) {
                        serializableParameter.setType(param.getDataType());
                        if (param.getDataType().equalsIgnoreCase("array")) {
                            if (param.getArrayType() != null) {
                                if (param.getArrayType().equalsIgnoreCase("string")) {
                                    serializableParameter.setItems(new StringProperty());
                                }
                                if (param.getArrayType().equalsIgnoreCase("int") || param.getArrayType().equalsIgnoreCase("integer")) {
                                    serializableParameter.setItems(new IntegerProperty());
                                }
                                if (param.getArrayType().equalsIgnoreCase("long")) {
                                    serializableParameter.setItems(new LongProperty());
                                }
                                if (param.getArrayType().equalsIgnoreCase("float")) {
                                    serializableParameter.setItems(new FloatProperty());
                                }
                                if (param.getArrayType().equalsIgnoreCase("double")) {
                                    serializableParameter.setItems(new DoubleProperty());
                                }
                                if (param.getArrayType().equalsIgnoreCase("boolean")) {
                                    serializableParameter.setItems(new BooleanProperty());
                                }
                            }
                        }
                    }
                    if (param.getCollectionFormat() != null) {
                        serializableParameter.setCollectionFormat(param.getCollectionFormat().name());
                    }
                    if (param.getAllowableValues() != null && !param.getAllowableValues().isEmpty()) {
                        serializableParameter.setEnum(param.getAllowableValues());
                    }
                }
                // set default value on parameter
                if (parameter instanceof AbstractSerializableParameter) {
                    AbstractSerializableParameter qp = (AbstractSerializableParameter) parameter;
                    if (param.getDefaultValue() != null) {
                        qp.setDefaultValue(param.getDefaultValue());
                    }
                }
                // set schema on body parameter
                if (parameter instanceof BodyParameter) {
                    BodyParameter bp = (BodyParameter) parameter;
                    if (verb.getType() != null) {
                        if (verb.getType().endsWith("[]")) {
                            String typeName = verb.getType();
                            typeName = typeName.substring(0, typeName.length() - 2);
                            Property prop = modelTypeAsProperty(typeName, swagger);
                            if (prop != null) {
                                ArrayModel arrayModel = new ArrayModel();
                                arrayModel.setItems(prop);
                                bp.setSchema(arrayModel);
                            }
                        } else {
                            String ref = modelTypeAsRef(verb.getType(), swagger);
                            if (ref != null) {
                                bp.setSchema(new RefModel(ref));
                            }
                        }
                    }
                }
                op.addParameter(parameter);
            }
        }
        // if we have an out type then set that as response message
        if (verb.getOutType() != null) {
            Response response = new Response();
            Property prop = modelTypeAsProperty(verb.getOutType(), swagger);
            response.setSchema(prop);
            response.setDescription("Output type");
            op.addResponse("200", response);
        }
        // enrich with configured response messages from the rest-dsl
        doParseResponseMessages(swagger, verb, op);
        // add path
        swagger.path(opPath, path);
    }
}
Also used : AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) SerializableParameter(io.swagger.models.parameters.SerializableParameter) QueryParameter(io.swagger.models.parameters.QueryParameter) RefModel(io.swagger.models.RefModel) VerbDefinition(org.apache.camel.model.rest.VerbDefinition) ArrayList(java.util.ArrayList) StringProperty(io.swagger.models.properties.StringProperty) Operation(io.swagger.models.Operation) BodyParameter(io.swagger.models.parameters.BodyParameter) FormParameter(io.swagger.models.parameters.FormParameter) PathParameter(io.swagger.models.parameters.PathParameter) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) FloatProperty(io.swagger.models.properties.FloatProperty) RestOperationParamDefinition(org.apache.camel.model.rest.RestOperationParamDefinition) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) LongProperty(io.swagger.models.properties.LongProperty) RefProperty(io.swagger.models.properties.RefProperty) Property(io.swagger.models.properties.Property) DoubleProperty(io.swagger.models.properties.DoubleProperty) FloatProperty(io.swagger.models.properties.FloatProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) BooleanProperty(io.swagger.models.properties.BooleanProperty) Path(io.swagger.models.Path) IntegerProperty(io.swagger.models.properties.IntegerProperty) BooleanProperty(io.swagger.models.properties.BooleanProperty) Response(io.swagger.models.Response) LongProperty(io.swagger.models.properties.LongProperty) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) SerializableParameter(io.swagger.models.parameters.SerializableParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) 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) DoubleProperty(io.swagger.models.properties.DoubleProperty) ArrayModel(io.swagger.models.ArrayModel)

Example 14 with PathParameter

use of io.swagger.models.parameters.PathParameter in project swagger-parser by swagger-api.

the class LegacyConverterTest method testFixedProperties.

@Test
public void testFixedProperties() throws IOException {
    final Swagger swagger = converter.read("src/test/resources/specs/v1_2/singleFile.json");
    final Path path = swagger.getPath("/pet/{petId}");
    assertEquals(path.getPost().getResponses().size(), 1);
    for (Response item : path.getPost().getResponses().values()) {
        assertNull(item.getSchema());
    }
    assertEquals(path.getDelete().getResponses().size(), 1);
    assertEquals(path.getDelete().getResponses().containsKey("default"), true);
    assertEquals(path.getDelete().getResponses().get("default").getDescription(), "success");
    final PathParameter id = (PathParameter) Iterables.find(path.getPatch().getParameters(), new Predicate<Parameter>() {

        @Override
        public boolean apply(Parameter input) {
            return "petId".equals(input.getName());
        }
    });
    assertEquals(id.getType(), "string");
    assertNull(id.getFormat());
}
Also used : Path(io.swagger.models.Path) Response(io.swagger.models.Response) Swagger(io.swagger.models.Swagger) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) PathParameter(io.swagger.models.parameters.PathParameter) Predicate(com.google.common.base.Predicate) Test(org.testng.annotations.Test)

Example 15 with PathParameter

use of io.swagger.models.parameters.PathParameter in project swagger-parser by swagger-api.

the class SwaggerParserTest method testParameterRequired.

@Test(description = "Test (path & form) parameter's required attribute")
public void testParameterRequired() {
    SwaggerParser parser = new SwaggerParser();
    final Swagger swagger = parser.read("src/test/resources/petstore.json");
    final List<Parameter> operationParams = swagger.getPath("/pet/{petId}").getPost().getParameters();
    final PathParameter pathParameter = (PathParameter) operationParams.get(0);
    Assert.assertTrue(pathParameter.getRequired());
    final FormParameter formParameter = (FormParameter) operationParams.get(1);
    Assert.assertFalse(formParameter.getRequired());
}
Also used : Swagger(io.swagger.models.Swagger) SerializableParameter(io.swagger.models.parameters.SerializableParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) 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) PathParameter(io.swagger.models.parameters.PathParameter) FormParameter(io.swagger.models.parameters.FormParameter) Test(org.testng.annotations.Test)

Aggregations

PathParameter (io.swagger.models.parameters.PathParameter)51 QueryParameter (io.swagger.models.parameters.QueryParameter)35 Parameter (io.swagger.models.parameters.Parameter)32 Test (org.testng.annotations.Test)32 BodyParameter (io.swagger.models.parameters.BodyParameter)26 HeaderParameter (io.swagger.models.parameters.HeaderParameter)16 Swagger (io.swagger.models.Swagger)14 StringProperty (io.swagger.models.properties.StringProperty)12 FormParameter (io.swagger.models.parameters.FormParameter)11 Operation (io.swagger.models.Operation)10 SerializableParameter (io.swagger.models.parameters.SerializableParameter)10 Response (io.swagger.models.Response)8 IntegerProperty (io.swagger.models.properties.IntegerProperty)8 RefProperty (io.swagger.models.properties.RefProperty)8 Path (io.swagger.models.Path)6 ArrayProperty (io.swagger.models.properties.ArrayProperty)6 HashMap (java.util.HashMap)6 ModelImpl (io.swagger.models.ModelImpl)5 RefModel (io.swagger.models.RefModel)5 ArrayList (java.util.ArrayList)5