Search in sources :

Example 31 with ModelResolver

use of io.swagger.v3.core.jackson.ModelResolver 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"));
}
Also used : AnnotatedType(io.swagger.v3.core.converter.AnnotatedType) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) 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 32 with ModelResolver

use of io.swagger.v3.core.jackson.ModelResolver 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);
}
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 33 with ModelResolver

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

the class PatternAndSchemaPropertiesTest method testPatternAndSchemaProperties.

@Test
public void testPatternAndSchemaProperties() throws Exception {
    final ModelResolver modelResolver = new ModelResolver(mapper());
    ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
    Schema model = context.resolve(new AnnotatedType(AnnotatedPet.class));
    assertEquals(((Schema) model.getPatternProperties().get("what.*ever")).getFormat(), "int32");
    assertEquals(((Schema) model.getPatternProperties().get("it.*takes")).get$ref(), "#/components/schemas/Category");
    assertEquals(((Schema) model.getProperties().get("anotherCategory")).get$ref(), "#/components/schemas/Category");
    assertEquals(((Schema) model.getProperties().get("anotherInteger")).getFormat(), "int32");
    SerializationMatchers.assertEqualsToYaml(context.getDefinedModels(), "AnnotatedPet:\n" + "  type: object\n" + "  properties:\n" + "    id:\n" + "      type: integer\n" + "      format: int64\n" + "    category:\n" + "      $ref: '#/components/schemas/Category'\n" + "    name:\n" + "      type: string\n" + "    photoUrls:\n" + "      type: array\n" + "      xml:\n" + "        wrapped: true\n" + "      items:\n" + "        type: string\n" + "        xml:\n" + "          name: photoUrl\n" + "    tags:\n" + "      type: array\n" + "      xml:\n" + "        wrapped: true\n" + "      items:\n" + "        $ref: '#/components/schemas/Tag'\n" + "    status:\n" + "      type: string\n" + "      description: pet status in the store\n" + "      enum:\n" + "      - available\n" + "      - pending\n" + "      - sold\n" + "    anotherCategory:\n" + "      $ref: '#/components/schemas/Category'\n" + "    anotherInteger:\n" + "      maximum: 10\n" + "      type: integer\n" + "      description: prop schema 1\n" + "      format: int32\n" + "  description: Annotated Pet\n" + "  nullable: true\n" + "Category:\n" + "  type: object\n" + "  properties:\n" + "    id:\n" + "      type: integer\n" + "      format: int64\n" + "    name:\n" + "      type: string\n" + "  description: prop schema 2\n" + "  xml:\n" + "    name: Category\n" + "Tag:\n" + "  type: object\n" + "  properties:\n" + "    id:\n" + "      type: integer\n" + "      format: int64\n" + "    name:\n" + "      type: string\n" + "  xml:\n" + "    name: Tag");
    context.getDefinedModels().values().forEach(s -> new OpenAPISchema2JsonSchema().process(s));
    SerializationMatchers.assertEqualsToYaml31(context.getDefinedModels(), "AnnotatedPet:\n" + "  type:\n" + "  - object\n" + "  - \"null\"\n" + "  properties:\n" + "    id:\n" + "      type: integer\n" + "      format: int64\n" + "    category:\n" + "      $ref: '#/components/schemas/Category'\n" + "    name:\n" + "      type: string\n" + "    photoUrls:\n" + "      type: array\n" + "      xml:\n" + "        wrapped: true\n" + "      items:\n" + "        type: string\n" + "        xml:\n" + "          name: photoUrl\n" + "    tags:\n" + "      type: array\n" + "      xml:\n" + "        wrapped: true\n" + "      items:\n" + "        $ref: '#/components/schemas/Tag'\n" + "    status:\n" + "      type: string\n" + "      description: pet status in the store\n" + "      enum:\n" + "      - available\n" + "      - pending\n" + "      - sold\n" + "    anotherCategory:\n" + "      $ref: '#/components/schemas/Category'\n" + "    anotherInteger:\n" + "      maximum: 10\n" + "      type: integer\n" + "      description: prop schema 1\n" + "      format: int32\n" + "  patternProperties:\n" + "    what.*ever:\n" + "      maximum: 10\n" + "      type: integer\n" + "      description: prop schema 1\n" + "      format: int32\n" + "    it.*takes:\n" + "      $ref: '#/components/schemas/Category'\n" + "  description: Annotated Pet\n" + "Category:\n" + "  type: object\n" + "  properties:\n" + "    id:\n" + "      type: integer\n" + "      format: int64\n" + "    name:\n" + "      type: string\n" + "  description: prop schema 2\n" + "  xml:\n" + "    name: Category\n" + "Tag:\n" + "  type: object\n" + "  properties:\n" + "    id:\n" + "      type: integer\n" + "      format: int64\n" + "    name:\n" + "      type: string\n" + "  xml:\n" + "    name: Tag\n");
}
Also used : AnnotatedType(io.swagger.v3.core.converter.AnnotatedType) OpenAPISchema2JsonSchema(io.swagger.v3.core.util.OpenAPISchema2JsonSchema) OpenAPISchema2JsonSchema(io.swagger.v3.core.util.OpenAPISchema2JsonSchema) Schema(io.swagger.v3.oas.models.media.Schema) AnnotatedPet(io.swagger.v3.core.resolving.v31.model.AnnotatedPet) ModelConverterContextImpl(io.swagger.v3.core.converter.ModelConverterContextImpl) ModelResolver(io.swagger.v3.core.jackson.ModelResolver) Test(org.testng.annotations.Test)

Example 34 with ModelResolver

use of io.swagger.v3.core.jackson.ModelResolver 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;
}
Also used : IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ModelConverter(io.swagger.v3.core.converter.ModelConverter) ModelResolver(io.swagger.v3.core.jackson.ModelResolver)

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