Search in sources :

Example 1 with SerializableParameter

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

the class SimpleReaderTest method scanResourceWithParamEnums.

@Test(description = "scan a resource with param enums")
public void scanResourceWithParamEnums() {
    Swagger swagger = getSwagger(ResourceWithEnums.class);
    SerializableParameter param = (SerializableParameter) getGetParameters(swagger, "/{id}").get(2);
    List<String> _enum = param.getEnum();
    assertEquals(_enum, Arrays.asList("a", "b", "c", "d", "e"));
    List<Parameter> checkEnumHandling = getGetParameters(swagger, "/checkEnumHandling/{v0}");
    List<String> allEnumValues = Lists.newArrayList(Collections2.transform(Arrays.asList(TestEnum.values()), Functions.toStringFunction()));
    SerializableParameter v0 = (SerializableParameter) checkEnumHandling.get(0);
    assertEquals(v0.getEnum(), allEnumValues);
    SerializableParameter v1 = (SerializableParameter) checkEnumHandling.get(1);
    assertEquals(((StringProperty) v1.getItems()).getEnum(), allEnumValues);
    SerializableParameter v2 = (SerializableParameter) checkEnumHandling.get(2);
    assertEquals(((StringProperty) v2.getItems()).getEnum(), allEnumValues);
    SerializableParameter v3 = (SerializableParameter) checkEnumHandling.get(3);
    assertEquals(v3.getEnum(), Arrays.asList("A", "B", "C"));
}
Also used : SerializableParameter(io.swagger.models.parameters.SerializableParameter) Swagger(io.swagger.models.Swagger) SerializableParameter(io.swagger.models.parameters.SerializableParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) Test(org.testng.annotations.Test)

Example 2 with SerializableParameter

use of io.swagger.models.parameters.SerializableParameter 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 3 with SerializableParameter

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

the class SwaggerParserTest method testIssue358.

@Test
public void testIssue358() {
    SwaggerParser parser = new SwaggerParser();
    final Swagger swagger = parser.read("src/test/resources/issue_358.yaml");
    assertNotNull(swagger);
    List<Parameter> parms = swagger.getPath("/testApi").getGet().getParameters();
    assertEquals(1, parms.size());
    assertEquals("pathParam", parms.get(0).getName());
    assertEquals("string", ((SerializableParameter) parms.get(0)).getType());
}
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) Test(org.testng.annotations.Test)

Example 4 with SerializableParameter

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

the class SwaggerParserTest method testIssue357.

@Test
public void testIssue357() {
    SwaggerParser parser = new SwaggerParser();
    final Swagger swagger = parser.read("src/test/resources/issue_357.yaml");
    assertNotNull(swagger);
    List<Parameter> getParams = swagger.getPath("/testApi").getGet().getParameters();
    assertEquals(2, getParams.size());
    for (Parameter param : getParams) {
        SerializableParameter sp = (SerializableParameter) param;
        switch(param.getName()) {
            case "pathParam1":
                assertEquals(sp.getType(), "integer");
                break;
            case "pathParam2":
                assertEquals(sp.getType(), "string");
                break;
            default:
                fail("Unexpected parameter named " + sp.getName());
                break;
        }
    }
}
Also used : SerializableParameter(io.swagger.models.parameters.SerializableParameter) 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) Test(org.testng.annotations.Test)

Example 5 with SerializableParameter

use of io.swagger.models.parameters.SerializableParameter in project light-rest-4j by networknt.

the class ValidatorTestUtil method intParam.

public static SerializableParameter intParam(final boolean required, final BigDecimal min, final BigDecimal max) {
    final SerializableParameter result = mock(SerializableParameter.class);
    when(result.getName()).thenReturn("Test Parameter");
    when(result.getType()).thenReturn("integer");
    when(result.getFormat()).thenReturn("int32");
    when(result.getRequired()).thenReturn(required);
    when(result.getMinimum()).thenReturn(min);
    when(result.getMaximum()).thenReturn(max);
    return result;
}
Also used : SerializableParameter(io.swagger.models.parameters.SerializableParameter)

Aggregations

SerializableParameter (io.swagger.models.parameters.SerializableParameter)13 BodyParameter (io.swagger.models.parameters.BodyParameter)7 PathParameter (io.swagger.models.parameters.PathParameter)6 QueryParameter (io.swagger.models.parameters.QueryParameter)6 Parameter (io.swagger.models.parameters.Parameter)5 FormParameter (io.swagger.models.parameters.FormParameter)4 HeaderParameter (io.swagger.models.parameters.HeaderParameter)4 Swagger (io.swagger.models.Swagger)3 AbstractSerializableParameter (io.swagger.models.parameters.AbstractSerializableParameter)3 ArrayProperty (io.swagger.models.properties.ArrayProperty)3 Property (io.swagger.models.properties.Property)3 RefProperty (io.swagger.models.properties.RefProperty)3 StringProperty (io.swagger.models.properties.StringProperty)3 ArrayList (java.util.ArrayList)3 Test (org.testng.annotations.Test)3 Operation (io.swagger.models.Operation)2 Path (io.swagger.models.Path)2 RefModel (io.swagger.models.RefModel)2 Response (io.swagger.models.Response)2 RefParameter (io.swagger.models.parameters.RefParameter)2