Search in sources :

Example 1 with RestOperationResponseMsgDefinition

use of org.apache.camel.model.rest.RestOperationResponseMsgDefinition in project camel by apache.

the class RestSwaggerReader method parse.

private void parse(Swagger swagger, RestDefinition rest, String camelContextId, ClassResolver classResolver) {
    List<VerbDefinition> verbs = new ArrayList<>(rest.getVerbs());
    // must sort the verbs by uri so we group them together when an uri has multiple operations
    Collections.sort(verbs, new VerbOrdering());
    // we need to group the operations within the same tag, so use the path as default if not configured
    String pathAsTag = rest.getTag() != null ? rest.getTag() : FileUtil.stripLeadingSeparator(rest.getPath());
    String summary = rest.getDescriptionText();
    if (ObjectHelper.isNotEmpty(pathAsTag)) {
        // add rest as tag
        Tag tag = new Tag();
        tag.description(summary);
        tag.name(pathAsTag);
        swagger.addTag(tag);
    }
    // gather all types in use
    Set<String> types = new LinkedHashSet<>();
    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;
        }
        String type = verb.getType();
        if (ObjectHelper.isNotEmpty(type)) {
            if (type.endsWith("[]")) {
                type = type.substring(0, type.length() - 2);
            }
            types.add(type);
        }
        type = verb.getOutType();
        if (ObjectHelper.isNotEmpty(type)) {
            if (type.endsWith("[]")) {
                type = type.substring(0, type.length() - 2);
            }
            types.add(type);
        }
        // there can also be types in response messages
        if (verb.getResponseMsgs() != null) {
            for (RestOperationResponseMsgDefinition def : verb.getResponseMsgs()) {
                type = def.getResponseModel();
                if (ObjectHelper.isNotEmpty(type)) {
                    if (type.endsWith("[]")) {
                        type = type.substring(0, type.length() - 2);
                    }
                    types.add(type);
                }
            }
        }
    }
    // use annotation scanner to find models (annotated classes)
    for (String type : types) {
        Class<?> clazz = classResolver.resolveClass(type);
        appendModels(clazz, swagger);
    }
    doParseVerbs(swagger, rest, camelContextId, verbs, pathAsTag);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) VerbDefinition(org.apache.camel.model.rest.VerbDefinition) RestOperationResponseMsgDefinition(org.apache.camel.model.rest.RestOperationResponseMsgDefinition) ArrayList(java.util.ArrayList) Tag(io.swagger.models.Tag)

Example 2 with RestOperationResponseMsgDefinition

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

ArrayList (java.util.ArrayList)2 RestOperationResponseMsgDefinition (org.apache.camel.model.rest.RestOperationResponseMsgDefinition)2 Response (io.swagger.models.Response)1 Tag (io.swagger.models.Tag)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 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 RestOperationResponseHeaderDefinition (org.apache.camel.model.rest.RestOperationResponseHeaderDefinition)1 VerbDefinition (org.apache.camel.model.rest.VerbDefinition)1