Search in sources :

Example 1 with LongProperty

use of io.swagger.models.properties.LongProperty 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 2 with LongProperty

use of io.swagger.models.properties.LongProperty in project camel by apache.

the class RestSwaggerReader method doParseResponseMessages.

private void doParseResponseMessages(Swagger swagger, VerbDefinition verb, Operation op) {
    for (RestOperationResponseMsgDefinition msg : verb.getResponseMsgs()) {
        Response response = null;
        if (op.getResponses() != null) {
            response = op.getResponses().get(msg.getCode());
        }
        if (response == null) {
            response = new Response();
        }
        if (ObjectHelper.isNotEmpty(msg.getResponseModel())) {
            Property prop = modelTypeAsProperty(msg.getResponseModel(), swagger);
            response.setSchema(prop);
        }
        response.setDescription(msg.getMessage());
        // add headers
        if (msg.getHeaders() != null) {
            for (RestOperationResponseHeaderDefinition header : msg.getHeaders()) {
                String name = header.getName();
                String type = header.getDataType();
                if ("string".equals(type)) {
                    StringProperty sp = new StringProperty();
                    sp.setName(name);
                    sp.setDescription(header.getDescription());
                    if (header.getAllowableValues() != null) {
                        sp.setEnum(header.getAllowableValues());
                    }
                    response.addHeader(name, sp);
                } else if ("int".equals(type) || "integer".equals(type)) {
                    IntegerProperty ip = new IntegerProperty();
                    ip.setName(name);
                    ip.setDescription(header.getDescription());
                    List<Integer> values;
                    if (!header.getAllowableValues().isEmpty()) {
                        values = new ArrayList<Integer>();
                        for (String text : header.getAllowableValues()) {
                            values.add(Integer.valueOf(text));
                        }
                        ip.setEnum(values);
                    }
                    response.addHeader(name, ip);
                } else if ("long".equals(type)) {
                    LongProperty lp = new LongProperty();
                    lp.setName(name);
                    lp.setDescription(header.getDescription());
                    List<Long> values;
                    if (!header.getAllowableValues().isEmpty()) {
                        values = new ArrayList<Long>();
                        for (String text : header.getAllowableValues()) {
                            values.add(Long.valueOf(text));
                        }
                        lp.setEnum(values);
                    }
                    response.addHeader(name, lp);
                } else if ("float".equals(type)) {
                    FloatProperty lp = new FloatProperty();
                    lp.setName(name);
                    lp.setDescription(header.getDescription());
                    List<Float> values;
                    if (!header.getAllowableValues().isEmpty()) {
                        values = new ArrayList<Float>();
                        for (String text : header.getAllowableValues()) {
                            values.add(Float.valueOf(text));
                        }
                        lp.setEnum(values);
                    }
                    response.addHeader(name, lp);
                } else if ("double".equals(type)) {
                    DoubleProperty dp = new DoubleProperty();
                    dp.setName(name);
                    dp.setDescription(header.getDescription());
                    List<Double> values;
                    if (!header.getAllowableValues().isEmpty()) {
                        values = new ArrayList<Double>();
                        for (String text : header.getAllowableValues()) {
                            values.add(Double.valueOf(text));
                        }
                        dp.setEnum(values);
                    }
                    response.addHeader(name, dp);
                } else if ("boolean".equals(type)) {
                    BooleanProperty bp = new BooleanProperty();
                    bp.setName(name);
                    bp.setDescription(header.getDescription());
                    response.addHeader(name, bp);
                } else if ("array".equals(type)) {
                    ArrayProperty ap = new ArrayProperty();
                    ap.setName(name);
                    ap.setDescription(header.getDescription());
                    if (header.getArrayType() != null) {
                        if (header.getArrayType().equalsIgnoreCase("string")) {
                            ap.setItems(new StringProperty());
                        }
                        if (header.getArrayType().equalsIgnoreCase("int") || header.getArrayType().equalsIgnoreCase("integer")) {
                            ap.setItems(new IntegerProperty());
                        }
                        if (header.getArrayType().equalsIgnoreCase("long")) {
                            ap.setItems(new LongProperty());
                        }
                        if (header.getArrayType().equalsIgnoreCase("float")) {
                            ap.setItems(new FloatProperty());
                        }
                        if (header.getArrayType().equalsIgnoreCase("double")) {
                            ap.setItems(new DoubleProperty());
                        }
                        if (header.getArrayType().equalsIgnoreCase("boolean")) {
                            ap.setItems(new BooleanProperty());
                        }
                    }
                    response.addHeader(name, ap);
                }
            }
        }
        op.addResponse(msg.getCode(), response);
    }
}
Also used : IntegerProperty(io.swagger.models.properties.IntegerProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) RestOperationResponseHeaderDefinition(org.apache.camel.model.rest.RestOperationResponseHeaderDefinition) RestOperationResponseMsgDefinition(org.apache.camel.model.rest.RestOperationResponseMsgDefinition) BooleanProperty(io.swagger.models.properties.BooleanProperty) ArrayList(java.util.ArrayList) StringProperty(io.swagger.models.properties.StringProperty) Response(io.swagger.models.Response) LongProperty(io.swagger.models.properties.LongProperty) ArrayList(java.util.ArrayList) List(java.util.List) FloatProperty(io.swagger.models.properties.FloatProperty) DoubleProperty(io.swagger.models.properties.DoubleProperty) 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)

Example 3 with LongProperty

use of io.swagger.models.properties.LongProperty in project swagger-core by swagger-api.

the class HiddenFieldTest method testHiddenField.

@Test(description = "it should ignore a hidden field")
public void testHiddenField() {
    final Map<String, Model> models = ModelConverters.getInstance().read(ModelWithHiddenFields.class);
    final Model model = models.get("ModelWithHiddenFields");
    assertNotNull(model);
    assertEquals(model.getProperties().size(), 2);
    final Property idValue = model.getProperties().get("id");
    assertTrue(idValue instanceof LongProperty);
    assertTrue(idValue.getRequired());
    final Property nameValue = model.getProperties().get("name");
    assertTrue(nameValue instanceof StringProperty);
}
Also used : LongProperty(io.swagger.models.properties.LongProperty) Model(io.swagger.models.Model) StringProperty(io.swagger.models.properties.StringProperty) StringProperty(io.swagger.models.properties.StringProperty) LongProperty(io.swagger.models.properties.LongProperty) ApiModelProperty(io.swagger.annotations.ApiModelProperty) Property(io.swagger.models.properties.Property) Test(org.testng.annotations.Test)

Example 4 with LongProperty

use of io.swagger.models.properties.LongProperty in project swagger-core by swagger-api.

the class PropertySerializationTest method serializeLongProperty.

@Test(description = "it should serialize a LongProperty")
public void serializeLongProperty() throws IOException {
    final LongProperty p = new LongProperty()._default(8675309L);
    final String json = "{\"type\":\"integer\",\"format\":\"int64\",\"default\":8675309}";
    assertEquals(m.writeValueAsString(p), json);
}
Also used : LongProperty(io.swagger.models.properties.LongProperty) Test(org.testng.annotations.Test)

Example 5 with LongProperty

use of io.swagger.models.properties.LongProperty in project swagger-core by swagger-api.

the class LongPropertyTest method testEquals.

@Test
public void testEquals() {
    final LongProperty prop1 = new LongProperty();
    prop1.setName(PROP_1);
    prop1.setRequired(true);
    final LongProperty prop2 = new LongProperty();
    prop2.setName(PROP_2);
    assertNotEquals(prop1, prop2);
    prop2.setName(PROP_1);
    prop2.setRequired(true);
    assertEquals(prop1, prop2);
}
Also used : LongProperty(io.swagger.models.properties.LongProperty) Test(org.testng.annotations.Test)

Aggregations

LongProperty (io.swagger.models.properties.LongProperty)17 StringProperty (io.swagger.models.properties.StringProperty)13 Test (org.testng.annotations.Test)13 Property (io.swagger.models.properties.Property)8 ArrayProperty (io.swagger.models.properties.ArrayProperty)7 IntegerProperty (io.swagger.models.properties.IntegerProperty)7 RefProperty (io.swagger.models.properties.RefProperty)7 Model (io.swagger.models.Model)6 DoubleProperty (io.swagger.models.properties.DoubleProperty)5 Operation (io.swagger.models.Operation)4 Path (io.swagger.models.Path)4 Response (io.swagger.models.Response)4 QueryParameter (io.swagger.models.parameters.QueryParameter)4 BooleanProperty (io.swagger.models.properties.BooleanProperty)4 FloatProperty (io.swagger.models.properties.FloatProperty)4 Contact (io.swagger.models.Contact)3 Info (io.swagger.models.Info)3 ModelImpl (io.swagger.models.ModelImpl)3 Person (io.swagger.models.Person)3 RefModel (io.swagger.models.RefModel)3