Search in sources :

Example 1 with RestOperationResponseHeaderDefinition

use of org.apache.camel.model.rest.RestOperationResponseHeaderDefinition 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)

Aggregations

Response (io.swagger.models.Response)1 ArrayProperty (io.swagger.models.properties.ArrayProperty)1 BooleanProperty (io.swagger.models.properties.BooleanProperty)1 DoubleProperty (io.swagger.models.properties.DoubleProperty)1 FloatProperty (io.swagger.models.properties.FloatProperty)1 IntegerProperty (io.swagger.models.properties.IntegerProperty)1 LongProperty (io.swagger.models.properties.LongProperty)1 Property (io.swagger.models.properties.Property)1 RefProperty (io.swagger.models.properties.RefProperty)1 StringProperty (io.swagger.models.properties.StringProperty)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 RestOperationResponseHeaderDefinition (org.apache.camel.model.rest.RestOperationResponseHeaderDefinition)1 RestOperationResponseMsgDefinition (org.apache.camel.model.rest.RestOperationResponseMsgDefinition)1