Skip to content

Commit

Permalink
feat(core): add SchemaTitleModelConverter
Browse files Browse the repository at this point in the history
extracted from springwolf#890

Co-authored-by: David Beck <[email protected]>
  • Loading branch information
timonback and David Beck committed Aug 16, 2024
1 parent 1746421 commit 9e896f8
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.springwolf.core.asyncapi.schemas.converters;

import com.fasterxml.jackson.databind.JavaType;
import io.swagger.v3.core.converter.AnnotatedType;
import io.swagger.v3.core.converter.ModelConverter;
import io.swagger.v3.core.converter.ModelConverterContext;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.PrimitiveType;
import io.swagger.v3.oas.models.media.Schema;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Iterator;

@NoArgsConstructor
@Slf4j
public class SchemaTitleModelConverter implements ModelConverter {
@Override
public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) {
JavaType javaType = Json.mapper().constructType(type.getType());
if (chain.hasNext()) {
Schema<?> schema = chain.next().resolve(type, context, chain);
boolean isPrimitiveType = PrimitiveType.createProperty(type.getType()) != null;
if (schema != null && !isPrimitiveType) {
if (schema.get$ref() != null) {
Schema<?> definedModel = context.resolve(type);
if (definedModel != null && definedModel.getTitle() == null) {
definedModel.setTitle(javaType.getRawClass().getSimpleName());
}
}
}
return schema;
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import io.github.springwolf.core.asyncapi.scanners.common.payload.internal.TypeToClassConverter;
import io.github.springwolf.core.asyncapi.schemas.SwaggerSchemaService;
import io.github.springwolf.core.asyncapi.schemas.SwaggerSchemaUtil;
import io.github.springwolf.core.asyncapi.schemas.converters.SchemaTitleModelConverter;
import io.github.springwolf.core.configuration.docket.AsyncApiDocketService;
import io.github.springwolf.core.configuration.docket.DefaultAsyncApiDocketService;
import io.github.springwolf.core.configuration.properties.SpringwolfConfigConstants;
Expand Down Expand Up @@ -138,6 +139,12 @@ public ExampleGeneratorPostProcessor exampleGeneratorPostProcessor(SchemaWalkerP
return new ExampleGeneratorPostProcessor(schemaWalkerProvider);
}

@Bean
@ConditionalOnMissingBean
public SchemaTitleModelConverter schemaTitleModelConverter() {
return new SchemaTitleModelConverter();
}

@Bean
@ConditionalOnMissingBean
public SchemaWalkerProvider schemaWalkerProvider(List<SchemaWalker> schemaWalkers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.github.springwolf.core.asyncapi.components.postprocessors.ExampleGeneratorPostProcessor;
import io.github.springwolf.core.asyncapi.schemas.SwaggerSchemaService;
import io.github.springwolf.core.asyncapi.schemas.SwaggerSchemaUtil;
import io.github.springwolf.core.asyncapi.schemas.converters.SchemaTitleModelConverter;
import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties;
import io.github.springwolf.core.fixtures.ClasspathUtil;
import io.swagger.v3.core.util.Json;
Expand Down Expand Up @@ -47,7 +48,7 @@ class DefaultJsonComponentsServiceIntegrationTest {
private static final String CONTENT_TYPE_APPLICATION_JSON = "application/json";

private final SwaggerSchemaService schemaService = new SwaggerSchemaService(
List.of(),
List.of(new SchemaTitleModelConverter()),
List.of(new ExampleGeneratorPostProcessor(
new SchemaWalkerProvider(List.of(new DefaultSchemaWalker<>(new ExampleJsonValueGenerator()))))),
new SwaggerSchemaUtil(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.github.springwolf.core.asyncapi.components.postprocessors.ExampleGeneratorPostProcessor;
import io.github.springwolf.core.asyncapi.schemas.SwaggerSchemaService;
import io.github.springwolf.core.asyncapi.schemas.SwaggerSchemaUtil;
import io.github.springwolf.core.asyncapi.schemas.converters.SchemaTitleModelConverter;
import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties;
import io.github.springwolf.core.fixtures.ClasspathUtil;
import io.swagger.v3.core.util.Json;
Expand Down Expand Up @@ -40,7 +41,7 @@

class DefaultXmlComponentsServiceIntegrationTest {
private final SwaggerSchemaService schemaService = new SwaggerSchemaService(
List.of(),
List.of(new SchemaTitleModelConverter()),
List.of(new ExampleGeneratorPostProcessor(new SchemaWalkerProvider(List.of(
new DefaultSchemaWalker<>(new ExampleXmlValueGenerator(new DefaultExampleXmlValueSerializer())))))),
new SwaggerSchemaUtil(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.github.springwolf.core.asyncapi.components.postprocessors.ExampleGeneratorPostProcessor;
import io.github.springwolf.core.asyncapi.schemas.SwaggerSchemaService;
import io.github.springwolf.core.asyncapi.schemas.SwaggerSchemaUtil;
import io.github.springwolf.core.asyncapi.schemas.converters.SchemaTitleModelConverter;
import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties;
import io.github.springwolf.core.fixtures.ClasspathUtil;
import io.swagger.v3.core.util.Json;
Expand Down Expand Up @@ -53,7 +54,7 @@ class DefaultYamlComponentsServiceIntegrationTest {
new ExampleJsonValueGenerator(), new DefaultExampleYamlValueSerializer(), springwolfConfigProperties);

private final SwaggerSchemaService schemaService = new SwaggerSchemaService(
List.of(),
List.of(new SchemaTitleModelConverter()),
List.of(new ExampleGeneratorPostProcessor(
new SchemaWalkerProvider(List.of(new DefaultSchemaWalker<>(exampleYamlValueGenerator))))),
new SwaggerSchemaUtil(),
Expand Down

0 comments on commit 9e896f8

Please sign in to comment.