Search in sources :

Example 1 with ModelConverter

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

the class GericModelConverter method resolve.

@Override
public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> next) {
    if (type.getType() instanceof Class<?>) {
        Class<?> cls = (Class<?>) type.getType();
        if (GenericModel.class.isAssignableFrom(cls)) {
            Schema impl = new Schema();
            impl.title(cls.getSimpleName());
            for (Entry<String, Class<?>> entry : GenericModel.getDeclaredProperties().entrySet()) {
                impl.addProperties(entry.getKey(), context.resolve(new AnnotatedType(entry.getValue())));
            }
            context.defineModel(impl.getTitle(), impl);
            return impl;
        }
    }
    if (next.hasNext()) {
        return next.next().resolve(type, context, next);
    } else {
        return null;
    }
}
Also used : AnnotatedType(io.swagger.v3.core.converter.AnnotatedType) Schema(io.swagger.v3.oas.models.media.Schema)

Example 2 with ModelConverter

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

the class XMLInfoTest method testReadingXmlAccessorTypeNone.

@Test
public void testReadingXmlAccessorTypeNone() throws Exception {
    final ModelConverter mr = modelResolver();
    final Schema model = mr.resolve(new AnnotatedType(XmlDecoratedBeanXmlAccessorNone.class), new ModelConverterContextImpl(mr), null);
    final XML xml = model.getXml();
    assertNotNull(xml);
    assertEquals(xml.getName(), "xmlDecoratedBean");
    final Schema property = (Schema) model.getProperties().get("a");
    assertNotNull(property);
    assertNull(model.getProperties().get("b"));
    assertNotNull(model.getProperties().get("c"));
    assertNotNull(model.getProperties().get("d"));
    assertNotNull(model.getProperties().get("e"));
    assertNotNull(model.getProperties().get("f"));
}
Also used : AnnotatedType(io.swagger.v3.core.converter.AnnotatedType) XML(io.swagger.v3.oas.models.media.XML) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) ModelConverter(io.swagger.v3.core.converter.ModelConverter) ModelConverterContextImpl(io.swagger.v3.core.converter.ModelConverterContextImpl) Test(org.testng.annotations.Test)

Example 3 with ModelConverter

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

the class ModelConverterContextImpl method resolve.

@Override
public Schema resolve(AnnotatedType type) {
    AnnotatedType aType = OptionalUtils.unwrapOptional(type);
    if (aType != null) {
        return resolve(aType);
    }
    if (processedTypes.contains(type)) {
        return modelByType.get(type);
    } else {
        processedTypes.add(type);
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(String.format("resolve %s", type.getType()));
    }
    Iterator<ModelConverter> converters = this.getConverters();
    Schema resolved = null;
    if (converters.hasNext()) {
        ModelConverter converter = converters.next();
        LOGGER.trace("trying extension {}", converter);
        resolved = converter.resolve(type, this, converters);
    }
    if (resolved != null) {
        modelByType.put(type, resolved);
        Schema resolvedImpl = resolved;
        if (resolvedImpl.getName() != null) {
            modelByName.put(resolvedImpl.getName(), resolved);
        }
    } else {
        processedTypes.remove(type);
    }
    return resolved;
}
Also used : Schema(io.swagger.v3.oas.models.media.Schema)

Example 4 with ModelConverter

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

the class ReaderTest method resolveJaxb.

private Map<String, Schema> resolveJaxb(Type type) {
    List<ModelConverter> converters = new CopyOnWriteArrayList<ModelConverter>();
    ObjectMapper mapper = JaxbObjectMapperFactory.getMapper();
    converters.add(new ModelResolver(mapper));
    ModelConverterContextImpl context = new ModelConverterContextImpl(converters);
    Schema resolve = context.resolve(new AnnotatedType().type(type));
    Map<String, Schema> schemas = new HashMap<String, Schema>();
    for (Map.Entry<String, Schema> entry : context.getDefinedModels().entrySet()) {
        if (entry.getValue().equals(resolve)) {
            schemas.put(entry.getKey(), entry.getValue());
        }
    }
    return schemas;
}
Also used : HashMap(java.util.HashMap) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) ModelConverter(io.swagger.v3.core.converter.ModelConverter) ModelResolver(io.swagger.v3.core.jackson.ModelResolver) AnnotatedType(io.swagger.v3.core.converter.AnnotatedType) Map(java.util.Map) HashMap(java.util.HashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ModelConverterContextImpl(io.swagger.v3.core.converter.ModelConverterContextImpl) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 5 with ModelConverter

use of io.swagger.v3.core.converter.ModelConverter 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)

Aggregations

Schema (io.swagger.v3.oas.models.media.Schema)9 ModelConverter (io.swagger.v3.core.converter.ModelConverter)8 AnnotatedType (io.swagger.v3.core.converter.AnnotatedType)7 ModelConverterContextImpl (io.swagger.v3.core.converter.ModelConverterContextImpl)5 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)5 XML (io.swagger.v3.oas.models.media.XML)4 Test (org.testng.annotations.Test)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Map (java.util.Map)3 JavaType (com.fasterxml.jackson.databind.JavaType)2 ModelResolver (io.swagger.v3.core.jackson.ModelResolver)2 IOException (java.io.IOException)2 JsonIdentityInfo (com.fasterxml.jackson.annotation.JsonIdentityInfo)1 JsonIdentityReference (com.fasterxml.jackson.annotation.JsonIdentityReference)1 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1 JsonIgnoreProperties (com.fasterxml.jackson.annotation.JsonIgnoreProperties)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)1 JsonUnwrapped (com.fasterxml.jackson.annotation.JsonUnwrapped)1 JsonValue (com.fasterxml.jackson.annotation.JsonValue)1