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);
}
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);
}
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;
}
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");
}
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);
}
Aggregations