Search in sources :

Example 1 with VerbDefinition

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

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

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

the class RestContextRefDefinitionHelper method cloneRestDefinition.

private static RestDefinition cloneRestDefinition(JAXBContext jaxbContext, RestDefinition def) throws JAXBException {
    Marshaller marshal = jaxbContext.createMarshaller();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    marshal.marshal(def, bos);
    ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    Object clone = unmarshaller.unmarshal(bis);
    if (clone != null && clone instanceof RestDefinition) {
        RestDefinition def2 = (RestDefinition) clone;
        Iterator<VerbDefinition> verbit1 = def.getVerbs().iterator();
        Iterator<VerbDefinition> verbit2 = def2.getVerbs().iterator();
        while (verbit1.hasNext() && verbit2.hasNext()) {
            VerbDefinition verb1 = verbit1.next();
            VerbDefinition verb2 = verbit2.next();
            if (verb1.getToOrRoute() instanceof RouteDefinition && verb2.getToOrRoute() instanceof RouteDefinition) {
                RouteDefinition route1 = (RouteDefinition) verb1.getToOrRoute();
                RouteDefinition route2 = (RouteDefinition) verb2.getToOrRoute();
                // need to clone the namespaces also as they are not JAXB marshalled (as they are transient)
                Iterator<ExpressionNode> it = ProcessorDefinitionHelper.filterTypeInOutputs(route1.getOutputs(), ExpressionNode.class);
                Iterator<ExpressionNode> it2 = ProcessorDefinitionHelper.filterTypeInOutputs(route2.getOutputs(), ExpressionNode.class);
                while (it.hasNext() && it2.hasNext()) {
                    ExpressionNode node = it.next();
                    ExpressionNode node2 = it2.next();
                    NamespaceAwareExpression name = null;
                    NamespaceAwareExpression name2 = null;
                    if (node.getExpression() instanceof NamespaceAwareExpression) {
                        name = (NamespaceAwareExpression) node.getExpression();
                    }
                    if (node2.getExpression() instanceof NamespaceAwareExpression) {
                        name2 = (NamespaceAwareExpression) node2.getExpression();
                    }
                    if (name != null && name2 != null && name.getNamespaces() != null && !name.getNamespaces().isEmpty()) {
                        Map<String, String> map = new HashMap<String, String>();
                        map.putAll(name.getNamespaces());
                        name2.setNamespaces(map);
                    }
                }
            }
        }
        return def2;
    }
    return null;
}
Also used : Marshaller(javax.xml.bind.Marshaller) RestDefinition(org.apache.camel.model.rest.RestDefinition) VerbDefinition(org.apache.camel.model.rest.VerbDefinition) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Unmarshaller(javax.xml.bind.Unmarshaller) NamespaceAwareExpression(org.apache.camel.model.language.NamespaceAwareExpression)

Example 4 with VerbDefinition

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

the class RestUndertowHttpPojoTypeTest method testUndertowPojoTypeValidateModel.

@Test
public void testUndertowPojoTypeValidateModel() throws Exception {
    // Wasn't clear if there's a way to put this test into camel-core just to test the model
    // perhaps without starting the Camel Context?
    List<RestDefinition> restDefinitions = context().getRestDefinitions();
    assertNotNull(restDefinitions);
    assertTrue(restDefinitions.size() > 0);
    RestDefinition restDefinition = restDefinitions.get(0);
    List<VerbDefinition> verbs = restDefinition.getVerbs();
    assertNotNull(verbs);
    Map<String, VerbDefinition> mapVerb = new TreeMap<>();
    verbs.forEach(verb -> mapVerb.put(verb.getId(), verb));
    assertEquals(UserPojo[].class.getCanonicalName(), mapVerb.get("getUsers").getOutType());
    assertEquals(UserPojo[].class.getCanonicalName(), mapVerb.get("getUsersList").getOutType());
    assertEquals(UserPojo.class.getCanonicalName(), mapVerb.get("getUser").getOutType());
    assertEquals(UserPojo.class.getCanonicalName(), mapVerb.get("putUser").getType());
    assertEquals(UserPojo[].class.getCanonicalName(), mapVerb.get("putUsers").getType());
    assertEquals(UserPojo[].class.getCanonicalName(), mapVerb.get("putUsersList").getType());
}
Also used : RestDefinition(org.apache.camel.model.rest.RestDefinition) VerbDefinition(org.apache.camel.model.rest.VerbDefinition) TreeMap(java.util.TreeMap) BaseUndertowTest(org.apache.camel.component.undertow.BaseUndertowTest) Test(org.junit.Test)

Aggregations

VerbDefinition (org.apache.camel.model.rest.VerbDefinition)4 ArrayList (java.util.ArrayList)2 RestDefinition (org.apache.camel.model.rest.RestDefinition)2 ArrayModel (io.swagger.models.ArrayModel)1 Operation (io.swagger.models.Operation)1 Path (io.swagger.models.Path)1 RefModel (io.swagger.models.RefModel)1 Response (io.swagger.models.Response)1 Tag (io.swagger.models.Tag)1 AbstractSerializableParameter (io.swagger.models.parameters.AbstractSerializableParameter)1 BodyParameter (io.swagger.models.parameters.BodyParameter)1 FormParameter (io.swagger.models.parameters.FormParameter)1 HeaderParameter (io.swagger.models.parameters.HeaderParameter)1 Parameter (io.swagger.models.parameters.Parameter)1 PathParameter (io.swagger.models.parameters.PathParameter)1 QueryParameter (io.swagger.models.parameters.QueryParameter)1 SerializableParameter (io.swagger.models.parameters.SerializableParameter)1 ArrayProperty (io.swagger.models.properties.ArrayProperty)1 BooleanProperty (io.swagger.models.properties.BooleanProperty)1 DoubleProperty (io.swagger.models.properties.DoubleProperty)1