Search in sources :

Example 1 with Resource

use of com.yahoo.elide.swagger.model.Resource in project elide by yahoo.

the class JsonApiModelResolver method resolve.

@Override
public Model resolve(java.lang.reflect.Type type, ModelConverterContext context, Iterator<ModelConverter> next) {
    if (!(type instanceof Class || type instanceof SimpleType || type instanceof Type)) {
        return super.resolve(type, context, next);
    }
    Type<?> clazzType = null;
    /*
         * If an Elide entity is an attribute somewhere in a model, the ModelResolver will
         * end up wrapping this as a SimpleType (rather than trying to resolve the entity class directly).
         */
    if (type instanceof SimpleType) {
        type = ((SimpleType) type).getRawClass();
        clazzType = ClassType.of((Class<?>) type);
    } else if (type instanceof Type) {
        clazzType = (Type<?>) type;
    } else if (type instanceof Class) {
        clazzType = ClassType.of((Class<?>) type);
    }
    /* Not an entity managed by Elide, let Swagger convert it */
    String typeAlias;
    try {
        typeAlias = dictionary.getJsonAliasFor(clazzType);
    } catch (IllegalArgumentException e) {
        return super.resolve(type, context, next);
    }
    Resource entitySchema = new Resource();
    entitySchema.description(getModelDescription(clazzType));
    entitySchema.setSecurityDescription(getClassPermissions(clazzType));
    /* Populate the attributes */
    List<String> attributeNames = dictionary.getAttributes(clazzType);
    for (String attributeName : attributeNames) {
        Type<?> attributeType = dictionary.getType(clazzType, attributeName);
        Property attribute = processAttribute(clazzType, attributeName, attributeType, context, next);
        entitySchema.addAttribute(attributeName, attribute);
    }
    /* Populate the relationships */
    List<String> relationshipNames = dictionary.getRelationships(clazzType);
    for (String relationshipName : relationshipNames) {
        Type<?> relationshipType = dictionary.getParameterizedType(clazzType, relationshipName);
        Relationship relationship = processRelationship(clazzType, relationshipName, relationshipType);
        if (relationship != null) {
            entitySchema.addRelationship(relationshipName, relationship);
        }
    }
    entitySchema.name(typeAlias);
    return entitySchema;
}
Also used : SimpleType(com.fasterxml.jackson.databind.type.SimpleType) SimpleType(com.fasterxml.jackson.databind.type.SimpleType) ClassType(com.yahoo.elide.core.type.ClassType) Type(com.yahoo.elide.core.type.Type) Relationship(com.yahoo.elide.swagger.property.Relationship) Resource(com.yahoo.elide.swagger.model.Resource) ApiModelProperty(io.swagger.annotations.ApiModelProperty) Property(io.swagger.models.properties.Property)

Example 2 with Resource

use of com.yahoo.elide.swagger.model.Resource in project elide by yahoo.

the class SwaggerBuilderTest method testDefinitionGeneration.

@Test
public void testDefinitionGeneration() throws Exception {
    Map<String, Model> definitions = swagger.getDefinitions();
    assertEquals(4, definitions.size());
    assertTrue(definitions.containsKey("book"));
    assertTrue(definitions.containsKey("author"));
    assertTrue(definitions.containsKey("publisher"));
    assertTrue(definitions.containsKey("Address"));
    Model bookModel = definitions.get("book");
    assertTrue(bookModel instanceof Resource);
    assertEquals("A book", bookModel.getDescription());
    ObjectProperty attributeProps = (ObjectProperty) bookModel.getProperties().get("attributes");
    assertTrue(attributeProps.getProperties().containsKey("title"));
    ObjectProperty relationProps = (ObjectProperty) bookModel.getProperties().get("relationships");
    assertTrue(relationProps.getProperties().containsKey("publisher"));
    assertTrue(relationProps.getProperties().containsKey("authors"));
}
Also used : ObjectProperty(io.swagger.models.properties.ObjectProperty) Model(io.swagger.models.Model) Resource(com.yahoo.elide.swagger.model.Resource) Test(org.junit.jupiter.api.Test)

Example 3 with Resource

use of com.yahoo.elide.swagger.model.Resource in project elide by yahoo.

the class SwaggerBuilderTest method testOperationRequestBodies.

@Test
public void testOperationRequestBodies() throws Exception {
    /* These take a datum pointing to a resource */
    Operation[] resourceOps = { swagger.getPaths().get("/book").getPost(), swagger.getPaths().get("/book/{bookId}").getPatch() };
    for (Operation op : resourceOps) {
        BodyParameter bodyParam = (BodyParameter) op.getParameters().stream().filter((param) -> param.getIn().equals("body")).findFirst().get();
        assertNotNull(bodyParam);
        verifyDatum(bodyParam.getSchema(), "book");
    }
    /* These don't take any params */
    Operation[] noParamOps = { swagger.getPaths().get("/book").getGet(), swagger.getPaths().get("/book/{bookId}").getDelete(), swagger.getPaths().get("/book/{bookId}").getGet() };
    for (Operation op : noParamOps) {
        Optional<Parameter> bodyParam = op.getParameters().stream().filter((param) -> param.getIn().equals("body")).findFirst();
        assertFalse(bodyParam.isPresent());
    }
    /* These take a 'data' of relationships */
    Operation[] relationshipOps = { swagger.getPaths().get("/book/{bookId}/relationships/authors").getPatch(), swagger.getPaths().get("/book/{bookId}/relationships/authors").getDelete(), swagger.getPaths().get("/book/{bookId}/relationships/authors").getPost() };
    for (Operation op : relationshipOps) {
        BodyParameter bodyParam = (BodyParameter) op.getParameters().stream().filter((param) -> param.getIn().equals("body")).findFirst().get();
        assertNotNull(bodyParam);
        verifyDataRelationship(bodyParam.getSchema(), "author");
    }
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Resource(com.yahoo.elide.swagger.model.Resource) Arrays(java.util.Arrays) Data(com.yahoo.elide.swagger.property.Data) Swagger(io.swagger.models.Swagger) Tag(io.swagger.models.Tag) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Include(com.yahoo.elide.annotation.Include) StringProperty(io.swagger.models.properties.StringProperty) HashMap(java.util.HashMap) Publisher(example.models.Publisher) ArrayProperty(io.swagger.models.properties.ArrayProperty) Model(io.swagger.models.Model) TestInstance(org.junit.jupiter.api.TestInstance) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Author(example.models.Author) BeforeAll(org.junit.jupiter.api.BeforeAll) Path(io.swagger.models.Path) Map(java.util.Map) NO_VERSION(com.yahoo.elide.core.dictionary.EntityDictionary.NO_VERSION) Book(example.models.Book) RefProperty(io.swagger.models.properties.RefProperty) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Operation(io.swagger.models.Operation) Datum(com.yahoo.elide.swagger.property.Datum) Property(io.swagger.models.properties.Property) Id(javax.persistence.Id) Entity(javax.persistence.Entity) BodyParameter(io.swagger.models.parameters.BodyParameter) Set(java.util.Set) Parameter(io.swagger.models.parameters.Parameter) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Info(io.swagger.models.Info) QueryParameter(io.swagger.models.parameters.QueryParameter) Test(org.junit.jupiter.api.Test) Response(io.swagger.models.Response) ObjectProperty(io.swagger.models.properties.ObjectProperty) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Relationship(com.yahoo.elide.swagger.property.Relationship) Optional(java.util.Optional) BodyParameter(io.swagger.models.parameters.BodyParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) Operation(io.swagger.models.Operation) BodyParameter(io.swagger.models.parameters.BodyParameter) Test(org.junit.jupiter.api.Test)

Example 4 with Resource

use of com.yahoo.elide.swagger.model.Resource in project elide by yahoo.

the class JsonApiModelResolverTest method testModelDescriptions.

@Test
public void testModelDescriptions() {
    Resource model = getModel(KEY_BOOK);
    assertEquals("A book", model.getDescription());
    model = getModel(KEY_AUTHOR);
    assertEquals("The Author", model.getDescription());
    model = getModel(KEY_PUBLISHER);
    assertNull(model.getDescription());
}
Also used : Resource(com.yahoo.elide.swagger.model.Resource) Test(org.junit.jupiter.api.Test)

Aggregations

Resource (com.yahoo.elide.swagger.model.Resource)4 Test (org.junit.jupiter.api.Test)3 Relationship (com.yahoo.elide.swagger.property.Relationship)2 Model (io.swagger.models.Model)2 ObjectProperty (io.swagger.models.properties.ObjectProperty)2 Property (io.swagger.models.properties.Property)2 SimpleType (com.fasterxml.jackson.databind.type.SimpleType)1 Include (com.yahoo.elide.annotation.Include)1 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)1 NO_VERSION (com.yahoo.elide.core.dictionary.EntityDictionary.NO_VERSION)1 ClassType (com.yahoo.elide.core.type.ClassType)1 Type (com.yahoo.elide.core.type.Type)1 Data (com.yahoo.elide.swagger.property.Data)1 Datum (com.yahoo.elide.swagger.property.Datum)1 Author (example.models.Author)1 Book (example.models.Book)1 Publisher (example.models.Publisher)1 ApiModelProperty (io.swagger.annotations.ApiModelProperty)1 Info (io.swagger.models.Info)1 Operation (io.swagger.models.Operation)1