Search in sources :

Example 21 with ModelResolver

use of io.swagger.v3.core.jackson.ModelResolver in project swagger-core by swagger-api.

the class EnumTest method testEnum.

@Test
public void testEnum() {
    final ModelResolver modelResolver = new ModelResolver(mapper());
    final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
    final Schema model = context.resolve((new AnnotatedType().type(Currency.class)));
    assertNotNull(model);
    assertTrue(model instanceof StringSchema);
    final StringSchema strModel = (StringSchema) model;
    assertNotNull(strModel.getEnum());
    final Collection<String> modelValues = new ArrayList<String>(Collections2.transform(Arrays.asList(Currency.values()), Functions.toStringFunction()));
    assertEquals(strModel.getEnum(), modelValues);
    final Schema property = context.resolve(new AnnotatedType().type(Currency.class).schemaProperty(true));
    assertNotNull(property);
    assertTrue(property instanceof StringSchema);
    final StringSchema strProperty = (StringSchema) property;
    assertNotNull(strProperty.getEnum());
    final Collection<String> values = new ArrayList<>(Collections2.transform(Arrays.asList(Currency.values()), Functions.toStringFunction()));
    assertEquals(strProperty.getEnum(), values);
}
Also used : AnnotatedType(io.swagger.v3.core.converter.AnnotatedType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Schema(io.swagger.v3.oas.models.media.Schema) ArrayList(java.util.ArrayList) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ModelConverterContextImpl(io.swagger.v3.core.converter.ModelConverterContextImpl) ModelResolver(io.swagger.v3.core.jackson.ModelResolver) Test(org.testng.annotations.Test)

Example 22 with ModelResolver

use of io.swagger.v3.core.jackson.ModelResolver in project swagger-core by swagger-api.

the class EnumTest method testEnumGenerics.

@Test
public void testEnumGenerics() {
    final ModelResolver modelResolver = new ModelResolver(mapper());
    final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
    final Schema model = context.resolve((new AnnotatedType().type(Contract.class)));
    assertNotNull(model);
    assertEquals(model.getName(), "Contract");
    assertTrue(model.getProperties().containsKey("type"));
    assertNotNull(model.getProperties().get("type"));
}
Also used : AnnotatedType(io.swagger.v3.core.converter.AnnotatedType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Schema(io.swagger.v3.oas.models.media.Schema) ModelConverterContextImpl(io.swagger.v3.core.converter.ModelConverterContextImpl) ModelResolver(io.swagger.v3.core.jackson.ModelResolver) Test(org.testng.annotations.Test)

Example 23 with ModelResolver

use of io.swagger.v3.core.jackson.ModelResolver in project swagger-core by swagger-api.

the class JodaTest method testSimple.

@Test
public void testSimple() throws Exception {
    final ModelConverter mr = modelResolver();
    final Schema model = mr.resolve(new AnnotatedType(ModelWithJodaDateTime.class), new ModelConverterContextImpl(mr), null);
    assertNotNull(model);
    final Map<String, Schema> props = model.getProperties();
    assertEquals(props.size(), 2);
    for (Map.Entry<String, Schema> entry : props.entrySet()) {
        final String name = entry.getKey();
        final Schema prop = entry.getValue();
        if ("name".equals(name)) {
            assertEquals(prop.getType(), "string");
        } else if ("createdAt".equals(name)) {
            assertEquals(prop.getType(), "string");
            assertEquals(prop.getFormat(), "date-time");
        } else {
            fail(String.format("Unknown property '%s'", name));
        }
    }
}
Also used : AnnotatedType(io.swagger.v3.core.converter.AnnotatedType) Schema(io.swagger.v3.oas.models.media.Schema) Map(java.util.Map) ModelConverter(io.swagger.v3.core.converter.ModelConverter) ModelConverterContextImpl(io.swagger.v3.core.converter.ModelConverterContextImpl) Test(org.testng.annotations.Test)

Example 24 with ModelResolver

use of io.swagger.v3.core.jackson.ModelResolver in project swagger-core by swagger-api.

the class Ticket2862SubtypeTest method testSubType.

@Test
public void testSubType() throws Exception {
    final ModelResolver modelResolver = new ModelResolver(mapper());
    final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
    final Schema model = context.resolve(new AnnotatedType(Ticket2862Model.class));
    SerializationMatchers.assertEqualsToYaml(context.getDefinedModels(), "Ticket2862Model:\n" + "  type: object\n" + "  properties:\n" + "    Ticket2862Model:\n" + "      type: object\n" + "Ticket2862ModelImpl:\n" + "  type: string\n" + "  allOf:\n" + "  - $ref: '#/components/schemas/Ticket2862Model'\n" + "  enum:\n" + "  - VALUE1\n" + "  - VALUE2\n");
}
Also used : AnnotatedType(io.swagger.v3.core.converter.AnnotatedType) Schema(io.swagger.v3.oas.models.media.Schema) Ticket2862Model(io.swagger.v3.core.resolving.resources.Ticket2862Model) ModelConverterContextImpl(io.swagger.v3.core.converter.ModelConverterContextImpl) ModelResolver(io.swagger.v3.core.jackson.ModelResolver) Test(org.testng.annotations.Test)

Example 25 with ModelResolver

use of io.swagger.v3.core.jackson.ModelResolver in project swagger-core by swagger-api.

the class Ticket2992Test method testLocalTime.

@Test
public void testLocalTime() throws Exception {
    final ModelResolver modelResolver = new ModelResolver(mapper());
    ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
    Schema model = context.resolve(new AnnotatedType(TestObject2992.class));
    SerializationMatchers.assertEqualsToYaml(context.getDefinedModels(), "LocalTime:\n" + "  type: object\n" + "  properties:\n" + "    hour:\n" + "      type: integer\n" + "      format: int32\n" + "    minute:\n" + "      type: integer\n" + "      format: int32\n" + "    second:\n" + "      type: integer\n" + "      format: int32\n" + "    nano:\n" + "      type: integer\n" + "      format: int32\n" + "TestObject2992:\n" + "  type: object\n" + "  properties:\n" + "    name:\n" + "      type: string\n" + "    a:\n" + "      $ref: '#/components/schemas/LocalTime'\n" + "    b:\n" + "      $ref: '#/components/schemas/LocalTime'\n" + "    c:\n" + "      $ref: '#/components/schemas/LocalTime'\n" + "    d:\n" + "      type: string\n" + "      format: date-time\n" + "    e:\n" + "      type: string\n" + "      format: date-time\n" + "    f:\n" + "      type: string\n" + "      format: date-time");
    PrimitiveType.enablePartialTime();
    context = new ModelConverterContextImpl(modelResolver);
    context.resolve(new AnnotatedType(TestObject2992.class));
    SerializationMatchers.assertEqualsToYaml(context.getDefinedModels(), "TestObject2992:\n" + "  type: object\n" + "  properties:\n" + "    name:\n" + "      type: string\n" + "    a:\n" + "      type: string\n" + "      format: partial-time\n" + "    b:\n" + "      type: string\n" + "      format: partial-time\n" + "    c:\n" + "      type: string\n" + "      format: partial-time\n" + "    d:\n" + "      type: string\n" + "      format: date-time\n" + "    e:\n" + "      type: string\n" + "      format: date-time\n" + "    f:\n" + "      type: string\n" + "      format: date-time");
}
Also used : AnnotatedType(io.swagger.v3.core.converter.AnnotatedType) Schema(io.swagger.v3.oas.models.media.Schema) ModelConverterContextImpl(io.swagger.v3.core.converter.ModelConverterContextImpl) ModelResolver(io.swagger.v3.core.jackson.ModelResolver) TestObject2992(io.swagger.v3.core.resolving.resources.TestObject2992) Test(org.testng.annotations.Test)

Aggregations

ModelConverterContextImpl (io.swagger.v3.core.converter.ModelConverterContextImpl)33 ModelResolver (io.swagger.v3.core.jackson.ModelResolver)30 AnnotatedType (io.swagger.v3.core.converter.AnnotatedType)25 Schema (io.swagger.v3.oas.models.media.Schema)25 Test (org.testng.annotations.Test)24 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)11 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)7 ModelConverter (io.swagger.v3.core.converter.ModelConverter)6 BeforeMethod (org.testng.annotations.BeforeMethod)5 XML (io.swagger.v3.oas.models.media.XML)3 BeforeTest (org.testng.annotations.BeforeTest)3 OpenAPISchema2JsonSchema (io.swagger.v3.core.util.OpenAPISchema2JsonSchema)2 StringSchema (io.swagger.v3.oas.models.media.StringSchema)2 Map (java.util.Map)2 MyThing (io.swagger.v3.core.resolving.resources.MyThing)1 TestObject2915 (io.swagger.v3.core.resolving.resources.TestObject2915)1 TestObject2972 (io.swagger.v3.core.resolving.resources.TestObject2972)1 TestObject2992 (io.swagger.v3.core.resolving.resources.TestObject2992)1 TestObject3697 (io.swagger.v3.core.resolving.resources.TestObject3697)1 Ticket2862Model (io.swagger.v3.core.resolving.resources.Ticket2862Model)1