use of io.swagger.v3.core.converter.ModelConverter in project swagger-core by swagger-api.
the class SamplePropertyExtendedConverter method resolve.
@Override
public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) {
if (type.isSchemaProperty()) {
JavaType _type = Json.mapper().constructType(type.getType());
if (_type != null) {
Class<?> cls = _type.getRawClass();
if (MyCustomClass.class.isAssignableFrom(cls)) {
Schema schema = new DateTimeSchema();
super.resolveSchemaMembers(schema, type);
return schema;
}
}
}
if (chain.hasNext()) {
return chain.next().resolve(type, context, chain);
} else {
return null;
}
}
use of io.swagger.v3.core.converter.ModelConverter 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));
}
}
}
use of io.swagger.v3.core.converter.ModelConverter in project swagger-core by swagger-api.
the class XMLInfoTest method testSimple.
@Test
public void testSimple() throws Exception {
final ModelConverter mr = modelResolver();
ModelConverterContextImpl ctx = new ModelConverterContextImpl(mr);
final Schema model = mr.resolve(new AnnotatedType(XmlDecoratedBean.class), ctx, null);
final XML xml = model.getXml();
assertNotNull(xml);
assertEquals(xml.getName(), "xmlDecoratedBean");
// Cast it to an array property
final ArraySchema property = (ArraySchema) model.getProperties().get("elements");
assertNotNull(property);
final XML propertyXml = property.getXml();
assertNotNull(propertyXml);
assertNull(propertyXml.getName());
assertTrue(propertyXml.getWrapped());
// Get the xml for items for the array property
final XML itemsXml = property.getItems().getXml();
assertNotNull(itemsXml);
// Check the name of item name
assertEquals(itemsXml.getName(), "element");
assertNotNull(model.getProperties().get("elementC"));
}
use of io.swagger.v3.core.converter.ModelConverter in project swagger-core by swagger-api.
the class XMLInfoTest method testReadingXmlAccessorTypePublic.
@Test
public void testReadingXmlAccessorTypePublic() throws Exception {
final ModelConverter mr = modelResolver();
final Schema model = mr.resolve(new AnnotatedType(XmlDecoratedBeanXmlAccessorPublic.class), new ModelConverterContextImpl(mr), null);
final XML xml = model.getXml();
assertNotNull(xml);
assertEquals(xml.getName(), "xmlDecoratedBean");
final Schema propertyA = (Schema) model.getProperties().get("a");
assertNotNull(propertyA);
final Schema propertyB = (Schema) model.getProperties().get("b");
assertNotNull(propertyB);
final Schema propertyC = (Schema) model.getProperties().get("c");
assertNull(propertyC);
}
use of io.swagger.v3.core.converter.ModelConverter in project swagger-core by swagger-api.
the class GenericOpenApiContext method init.
@Override
public T init() throws OpenApiConfigurationException {
if (openApiConfiguration == null) {
openApiConfiguration = loadConfiguration();
}
if (openApiConfiguration == null) {
openApiConfiguration = new SwaggerConfiguration().resourcePackages(resourcePackages).resourceClasses(resourceClasses);
((SwaggerConfiguration) openApiConfiguration).setId(id);
((SwaggerConfiguration) openApiConfiguration).setOpenAPI31(openAPI31);
}
openApiConfiguration = mergeParentConfiguration(openApiConfiguration, parent);
try {
if (openApiReader == null) {
openApiReader = buildReader(ContextUtils.deepCopy(openApiConfiguration));
}
if (openApiScanner == null) {
openApiScanner = buildScanner(ContextUtils.deepCopy(openApiConfiguration));
}
if (objectMapperProcessor == null) {
objectMapperProcessor = buildObjectMapperProcessor(ContextUtils.deepCopy(openApiConfiguration));
}
if (modelConverters == null || modelConverters.isEmpty()) {
modelConverters = buildModelConverters(ContextUtils.deepCopy(openApiConfiguration));
}
if (outputJsonMapper == null) {
if (Boolean.TRUE.equals(openApiConfiguration.isOpenAPI31())) {
outputJsonMapper = Json31.mapper().copy();
} else {
outputJsonMapper = Json.mapper().copy();
}
}
if (outputYamlMapper == null) {
if (Boolean.TRUE.equals(openApiConfiguration.isOpenAPI31())) {
outputYamlMapper = Yaml31.mapper().copy();
} else {
outputYamlMapper = Yaml.mapper().copy();
}
}
if (openApiConfiguration.isSortOutput() != null && openApiConfiguration.isSortOutput()) {
outputJsonMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
outputJsonMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
outputYamlMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
outputYamlMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
if (Boolean.TRUE.equals(openApiConfiguration.isOpenAPI31())) {
outputJsonMapper.addMixIn(OpenAPI.class, SortedOpenAPIMixin31.class);
outputJsonMapper.addMixIn(Schema.class, SortedSchemaMixin31.class);
outputYamlMapper.addMixIn(OpenAPI.class, SortedOpenAPIMixin31.class);
outputYamlMapper.addMixIn(Schema.class, SortedSchemaMixin31.class);
} else {
outputJsonMapper.addMixIn(OpenAPI.class, SortedOpenAPIMixin.class);
outputJsonMapper.addMixIn(Schema.class, SortedSchemaMixin.class);
outputYamlMapper.addMixIn(OpenAPI.class, SortedOpenAPIMixin.class);
outputYamlMapper.addMixIn(Schema.class, SortedSchemaMixin.class);
}
}
} catch (Exception e) {
LOGGER.error("error initializing context: " + e.getMessage(), e);
throw new OpenApiConfigurationException("error initializing context: " + e.getMessage(), e);
}
try {
if (objectMapperProcessor != null) {
ObjectMapper mapper = IntegrationObjectMapperFactory.createJson();
objectMapperProcessor.processJsonObjectMapper(mapper);
ModelConverters.getInstance().addConverter(new ModelResolver(mapper));
objectMapperProcessor.processOutputJsonObjectMapper(outputJsonMapper);
objectMapperProcessor.processOutputYamlObjectMapper(outputYamlMapper);
}
} catch (Exception e) {
LOGGER.error("error configuring objectMapper: " + e.getMessage(), e);
throw new OpenApiConfigurationException("error configuring objectMapper: " + e.getMessage(), e);
}
try {
if (modelConverters != null && !modelConverters.isEmpty()) {
for (ModelConverter converter : modelConverters) {
ModelConverters.getInstance().addConverter(converter);
}
}
} catch (Exception e) {
LOGGER.error("error configuring model converters: " + e.getMessage(), e);
throw new OpenApiConfigurationException("error configuring model converters: " + e.getMessage(), e);
}
// set cache TTL if present in configuration
if (openApiConfiguration.getCacheTTL() != null) {
this.cacheTTL = openApiConfiguration.getCacheTTL();
}
register();
return (T) this;
}
Aggregations