Search in sources :

Example 1 with RefModel

use of io.swagger.models.RefModel 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 RefModel

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

the class ModelDeserializer method deserialize.

@Override
public Model deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    JsonNode node = jp.getCodec().readTree(jp);
    JsonNode sub = node.get("$ref");
    JsonNode allOf = node.get("allOf");
    if (sub != null) {
        return Json.mapper().convertValue(sub, RefModel.class);
    } else if (allOf != null) {
        ComposedModel model = null;
        // we only support one parent, no multiple inheritance or composition
        model = Json.mapper().convertValue(node, ComposedModel.class);
        List<Model> allComponents = model.getAllOf();
        if (allComponents.size() >= 1) {
            model.setParent(allComponents.get(0));
            if (allComponents.size() >= 2) {
                model.setChild(allComponents.get(allComponents.size() - 1));
                List<RefModel> interfaces = new ArrayList<RefModel>();
                int size = allComponents.size();
                for (Model m : allComponents.subList(1, size - 1)) {
                    if (m instanceof RefModel) {
                        RefModel ref = (RefModel) m;
                        interfaces.add(ref);
                    }
                }
                model.setInterfaces(interfaces);
            } else {
                model.setChild(new ModelImpl());
            }
        }
        return model;
    } else {
        sub = node.get("type");
        Model model = null;
        if (sub != null && "array".equals(((TextNode) sub).textValue())) {
            model = Json.mapper().convertValue(node, ArrayModel.class);
        } else {
            model = Json.mapper().convertValue(node, ModelImpl.class);
        }
        return model;
    }
}
Also used : RefModel(io.swagger.models.RefModel) ComposedModel(io.swagger.models.ComposedModel) RefModel(io.swagger.models.RefModel) ComposedModel(io.swagger.models.ComposedModel) Model(io.swagger.models.Model) ArrayModel(io.swagger.models.ArrayModel) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayList(java.util.ArrayList) List(java.util.List) ModelImpl(io.swagger.models.ModelImpl)

Example 3 with RefModel

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

the class ChildTypeTest method testChildTypeParameter.

@Test(description = "Tests schema ref is correctly set up for child type parameter")
public void testChildTypeParameter() {
    Operation op = swagger.getPath("/childType/testChildTypeParameter").getPost();
    BodyParameter parameter = getBodyParameter(op, 0);
    Model schema = parameter.getSchema();
    assertEquals(schema.getClass().getName(), RefModel.class.getName());
    assertEquals(((RefModel) schema).getSimpleRef(), "Sub1Bean");
}
Also used : RefModel(io.swagger.models.RefModel) RefModel(io.swagger.models.RefModel) Model(io.swagger.models.Model) Operation(io.swagger.models.Operation) BodyParameter(io.swagger.models.parameters.BodyParameter) Test(org.testng.annotations.Test)

Example 4 with RefModel

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

the class SwaggerSerializerTest method convertSpec.

@Test(description = "it should convert a spec")
public void convertSpec() throws IOException {
    final Model personModel = ModelConverters.getInstance().read(Person.class).get("Person");
    final Model errorModel = ModelConverters.getInstance().read(Error.class).get("Error");
    final Info info = new Info().version("1.0.0").title("Swagger Petstore");
    final Contact contact = new Contact().name("Swagger API Team").email("foo@bar.baz").url("http://swagger.io");
    info.setContact(contact);
    final Map<String, Object> map = new HashMap<String, Object>();
    map.put("name", "value");
    info.setVendorExtension("x-test2", map);
    info.setVendorExtension("x-test", "value");
    final Swagger swagger = new Swagger().info(info).host("petstore.swagger.io").securityDefinition("api-key", new ApiKeyAuthDefinition("key", In.HEADER)).scheme(Scheme.HTTP).consumes("application/json").produces("application/json").model("Person", personModel).model("Error", errorModel);
    final Operation get = new Operation().produces("application/json").summary("finds pets in the system").description("a longer description").tag("Pet Operations").operationId("get pet by id").deprecated(true);
    get.parameter(new QueryParameter().name("tags").description("tags to filter by").required(false).property(new StringProperty()));
    get.parameter(new PathParameter().name("petId").description("pet to fetch").property(new LongProperty()));
    final Response response = new Response().description("pets returned").schema(new RefProperty().asDefault("Person")).example("application/json", "fun!");
    final Response errorResponse = new Response().description("error response").schema(new RefProperty().asDefault("Error"));
    get.response(200, response).defaultResponse(errorResponse);
    final Operation post = new Operation().summary("adds a new pet").description("you can add a new pet this way").tag("Pet Operations").operationId("add pet").defaultResponse(errorResponse).parameter(new BodyParameter().description("the pet to add").schema(new RefModel().asDefault("Person")));
    swagger.path("/pets", new Path().get(get).post(post));
    final String swaggerJson = Json.mapper().writeValueAsString(swagger);
    final Swagger rebuilt = Json.mapper().readValue(swaggerJson, Swagger.class);
    SerializationMatchers.assertEqualsToJson(rebuilt, swaggerJson);
}
Also used : Path(io.swagger.models.Path) QueryParameter(io.swagger.models.parameters.QueryParameter) RefModel(io.swagger.models.RefModel) HashMap(java.util.HashMap) Error(io.swagger.models.Error) StringProperty(io.swagger.models.properties.StringProperty) Operation(io.swagger.models.Operation) Info(io.swagger.models.Info) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) Contact(io.swagger.models.Contact) RefProperty(io.swagger.models.properties.RefProperty) Response(io.swagger.models.Response) ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) Swagger(io.swagger.models.Swagger) LongProperty(io.swagger.models.properties.LongProperty) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) Person(io.swagger.models.Person) Test(org.testng.annotations.Test)

Example 5 with RefModel

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

the class ModelSerializerTest method shouldNotCreateXmlObjectForRef.

@Test(description = "it should not create an xml object for ref")
public void shouldNotCreateXmlObjectForRef() throws IOException {
    final RefModel model = new RefModel("Monster");
    model.setDescription("oops");
    model.setExternalDocs(new ExternalDocs("external docs", "http://swagger.io"));
    assertEquals(Json.mapper().writeValueAsString(model), "{\"$ref\":\"#/definitions/Monster\"}");
}
Also used : RefModel(io.swagger.models.RefModel) ExternalDocs(io.swagger.models.ExternalDocs) Test(org.testng.annotations.Test)

Aggregations

RefModel (io.swagger.models.RefModel)13 Model (io.swagger.models.Model)7 BodyParameter (io.swagger.models.parameters.BodyParameter)7 Test (org.testng.annotations.Test)6 ModelImpl (io.swagger.models.ModelImpl)4 Operation (io.swagger.models.Operation)4 RefProperty (io.swagger.models.properties.RefProperty)4 StringProperty (io.swagger.models.properties.StringProperty)4 ArrayModel (io.swagger.models.ArrayModel)3 Path (io.swagger.models.Path)3 Response (io.swagger.models.Response)3 Swagger (io.swagger.models.Swagger)3 ArrayProperty (io.swagger.models.properties.ArrayProperty)3 IntegerProperty (io.swagger.models.properties.IntegerProperty)3 Property (io.swagger.models.properties.Property)3 HashMap (java.util.HashMap)3 ComposedModel (io.swagger.models.ComposedModel)2 PathParameter (io.swagger.models.parameters.PathParameter)2 QueryParameter (io.swagger.models.parameters.QueryParameter)2 LongProperty (io.swagger.models.properties.LongProperty)2