Search in sources :

Example 1 with ModelConverters

use of io.swagger.v3.core.converter.ModelConverters in project swagger-core by swagger-api.

the class CustomResolverTest method testCustomConverter.

@Test(description = "it should ignore properties with type Bar")
public void testCustomConverter() {
    // add the custom converter
    final ModelConverters converters = new ModelConverters();
    converters.addConverter(new CustomConverter(Json.mapper()));
    Map<String, Schema> models = converters.readAll(Foo.class);
    Schema model = models.get("io.swagger.v3.core.converting.override.CustomResolverTest$Foo");
    assertNotNull(model);
    assertEquals(model.getProperties().size(), 2);
    final Schema barProperty = (Schema) model.getProperties().get("bar");
    assertEquals(barProperty.get$ref(), "#/components/schemas/io.swagger.v3.core.converting.override.CustomResolverTest$Bar");
    final Schema titleProperty = (Schema) model.getProperties().get("title");
    assertNotNull(titleProperty);
    model = models.get("io.swagger.v3.core.converting.override.CustomResolverTest$Bar");
    assertNotNull(model);
}
Also used : Schema(io.swagger.v3.oas.models.media.Schema) ModelConverters(io.swagger.v3.core.converter.ModelConverters) Test(org.testng.annotations.Test)

Example 2 with ModelConverters

use of io.swagger.v3.core.converter.ModelConverters in project swagger-core by swagger-api.

the class CustomConverterTest method testCustomConverter.

@Test(description = "it should ignore properties with type Bar")
public void testCustomConverter() {
    // add the custom converter
    final ModelConverters converters = new ModelConverters();
    converters.addConverter(new CustomConverter());
    final Schema model = converters.read(Foo.class).get("Foo");
    assertNotNull(model);
    assertEquals(model.getProperties().size(), 1);
    final Schema barProperty = (Schema) model.getProperties().get("bar");
    assertNull(barProperty);
    final Schema titleProperty = (Schema) model.getProperties().get("title");
    assertNotNull(titleProperty);
}
Also used : Schema(io.swagger.v3.oas.models.media.Schema) ModelConverters(io.swagger.v3.core.converter.ModelConverters) Test(org.testng.annotations.Test)

Example 3 with ModelConverters

use of io.swagger.v3.core.converter.ModelConverters in project swagger-core by swagger-api.

the class GenericOpenApiContext method buildModelConverters.

protected Set<ModelConverter> buildModelConverters(final OpenAPIConfiguration openApiConfiguration) throws Exception {
    if (openApiConfiguration.getModelConverterClasses() != null && !openApiConfiguration.getModelConverterClasses().isEmpty()) {
        LinkedHashSet<ModelConverter> modelConverters = new LinkedHashSet<>();
        for (String converterClass : openApiConfiguration.getModelConverterClasses()) {
            Class cls = getClass().getClassLoader().loadClass(converterClass);
            ModelConverter converter = (ModelConverter) cls.newInstance();
            modelConverters.add(converter);
        }
        return modelConverters;
    }
    return null;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ModelConverter(io.swagger.v3.core.converter.ModelConverter)

Example 4 with ModelConverters

use of io.swagger.v3.core.converter.ModelConverters in project swagger-core by swagger-api.

the class OverrideTest method test.

@Test
public void test() {
    GenericModel.declareProperty(NAME, String.class);
    GenericModel.declareProperty(COUNT, int.class);
    // create new instead of use singleton
    final ModelConverters converters = new ModelConverters();
    converters.addConverter(new GericModelConverter());
    final Map<String, Schema> read = converters.read(GenericModel.class);
    assertTrue(read.containsKey(GenericModel.class.getSimpleName()));
    final Schema model = read.get(GenericModel.class.getSimpleName());
    assertTrue(model.getProperties().containsKey(NAME));
    assertEquals(((Schema) model.getProperties().get(NAME)).getType(), "string");
    assertTrue(model.getProperties().containsKey(COUNT));
    assertEquals(((Schema) model.getProperties().get(COUNT)).getType(), "integer");
}
Also used : GenericModel(io.swagger.v3.core.converting.override.resources.GenericModel) Schema(io.swagger.v3.oas.models.media.Schema) ModelConverters(io.swagger.v3.core.converter.ModelConverters) Test(org.testng.annotations.Test)

Example 5 with ModelConverters

use of io.swagger.v3.core.converter.ModelConverters in project swagger-core by swagger-api.

the class SnakeCaseConverterTest method testConvert.

@Test(description = "it should change naming style")
public void testConvert() {
    // add the custom converter
    final SnakeCaseConverter snakeCaseConverter = new SnakeCaseConverter();
    final ModelConverters converters = new ModelConverters();
    converters.addConverter(snakeCaseConverter);
    final Map<String, Schema> models = converters.readAll(SnakeCaseModel.class);
    final String json = "{" + "   \"bar\":{" + "      \"type\":\"object\"," + "      \"properties\":{" + "         \"foo\":{" + "            \"type\":\"string\"" + "         }" + "      }" + "   }," + "   \"snake_case_model\":{" + "      \"type\":\"object\"," + "      \"properties\":{" + "         \"bar\":{" + "            \"$ref\":\"#/components/schemas/bar\"" + "         }," + "         \"title\":{" + "            \"type\":\"string\"" + "         }" + "      }," + "      \"xml\":{" + "        \"name\":\"snakeCaseModel\"" + "      }" + "   }" + "}";
    // TODO test xml if/when added to annotations and resolver
    Json.prettyPrint(models);
    SerializationMatchers.assertEqualsToJson(models, json);
}
Also used : Schema(io.swagger.v3.oas.models.media.Schema) ModelConverters(io.swagger.v3.core.converter.ModelConverters) Test(org.testng.annotations.Test)

Aggregations

ModelConverters (io.swagger.v3.core.converter.ModelConverters)4 Schema (io.swagger.v3.oas.models.media.Schema)4 Test (org.testng.annotations.Test)4 ModelConverter (io.swagger.v3.core.converter.ModelConverter)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 GenericModel (io.swagger.v3.core.converting.override.resources.GenericModel)1 ModelResolver (io.swagger.v3.core.jackson.ModelResolver)1 IOException (java.io.IOException)1 LinkedHashSet (java.util.LinkedHashSet)1