Skip to content

Commit a333d57

Browse files
authored
refactor(core): move and improve SchemaObject to swagger schema mappi… (#1508)
* refactor(core): move and improve SchemaObject to swagger schema mapping * test(core): add missing test for SwaggerSchemaUtil
1 parent a827743 commit a333d57

File tree

7 files changed

+265
-54
lines changed

7 files changed

+265
-54
lines changed

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/schemas/SwaggerSchemaMapper.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import io.github.springwolf.asyncapi.v3.model.schema.SchemaType;
1010
import io.github.springwolf.core.configuration.properties.PayloadSchemaFormat;
1111
import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties;
12+
import io.swagger.v3.oas.models.media.ObjectSchema;
1213
import io.swagger.v3.oas.models.media.Schema;
1314
import lombok.RequiredArgsConstructor;
14-
import org.springframework.lang.Nullable;
1515

1616
import java.util.ArrayList;
1717
import java.util.HashSet;
@@ -220,7 +220,7 @@ private static void assignType(Schema swaggerSchema, SchemaObject.SchemaObjectBu
220220
* @param schema
221221
* @return
222222
*/
223-
public Object unwrapSchema(Object schema) {
223+
private Object unwrapSchema(Object schema) {
224224
if (schema instanceof ComponentSchema componentSchema) {
225225
Object unwrappedSchema = componentSchema.getSchema();
226226
if (unwrappedSchema == null) {
@@ -250,36 +250,37 @@ public Object unwrapSchema(Object schema) {
250250
* @param schema Object representing an schema.
251251
* @return the resulting Schema
252252
*/
253-
@Nullable
254253
public Schema<?> mapToSwagger(Object schema) {
255254
// first unwrap ComponentSchema and MultiFormatSchema:
256255
Object unwrappedSchema = unwrapSchema(schema);
257256

258257
if (unwrappedSchema instanceof Schema<?> swaggerSchema) {
259258
return swaggerSchema;
260259
}
261-
262260
if (unwrappedSchema instanceof SchemaObject schemaObject) {
263261
return mapSchemaObjectToSwagger(schemaObject);
264262
}
265263
if (unwrappedSchema instanceof SchemaReference schemaReference) {
266264
return mapSchemaReferenceToSwagger(schemaReference);
267265
}
266+
268267
throw new RuntimeException("Could not convert '" + schema + "' to a Swagger Schema");
269268
}
270269

271270
/**
272271
* transforms the given asyncApiSchema {@link SchemaObject} to a Swagger schema object.
273272
* <p>Note</p>
274273
* This method does not perform a 'deep' transformation, only the root attributes of asyncApiSchema
275-
* are mapped to the Swagger schema. The properties of asyncApiSchema will not be mapped to the
276-
* Swagger schema.
274+
* are mapped to the Swagger schema (best effort).
277275
*
278276
* @param asyncApiSchema
279-
* @return
277+
* @return swagger Schema
280278
*/
281279
private Schema mapSchemaObjectToSwagger(SchemaObject asyncApiSchema) {
282-
Schema swaggerSchema = new Schema();
280+
Schema swaggerSchema = new ObjectSchema();
281+
swaggerSchema.setName(asyncApiSchema.getTitle());
282+
swaggerSchema.setTitle(asyncApiSchema.getTitle());
283+
283284
if (asyncApiSchema.getType() != null) {
284285
swaggerSchema.setType(asyncApiSchema.getType().stream()
285286
.filter(type -> !type.equals(SchemaType.NULL))
@@ -292,6 +293,13 @@ private Schema mapSchemaObjectToSwagger(SchemaObject asyncApiSchema) {
292293
swaggerSchema.setExamples(asyncApiSchema.getExamples());
293294
swaggerSchema.setEnum(asyncApiSchema.getEnumValues());
294295

296+
if (asyncApiSchema.getProperties() != null) {
297+
Map<String, Schema> properties = asyncApiSchema.getProperties().entrySet().stream()
298+
.map((property) -> Map.entry(property.getKey(), (Schema<?>) mapToSwagger(property.getValue())))
299+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
300+
swaggerSchema.setProperties(properties);
301+
}
302+
295303
return swaggerSchema;
296304
}
297305

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/schemas/SwaggerSchemaService.java

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import io.swagger.v3.core.util.Json;
1616
import io.swagger.v3.core.util.PrimitiveType;
1717
import io.swagger.v3.core.util.RefUtils;
18-
import io.swagger.v3.oas.models.media.ObjectSchema;
1918
import io.swagger.v3.oas.models.media.Schema;
2019
import lombok.RequiredArgsConstructor;
2120
import lombok.extern.slf4j.Slf4j;
@@ -29,7 +28,6 @@
2928
import java.util.List;
3029
import java.util.Map;
3130
import java.util.function.Function;
32-
import java.util.stream.Collectors;
3331

3432
import static io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties.ConfigDocket.DEFAULT_CONTENT_TYPE;
3533

@@ -59,25 +57,10 @@ public record ExtractedSchemas(ComponentSchema rootSchema, Map<String, Component
5957
* @return
6058
*/
6159
public ComponentSchema postProcessSchemaWithoutRef(SchemaObject schemaWithoutRef) {
62-
String schemaName = schemaWithoutRef.getTitle();
63-
64-
// create a swagger schema to invoke the postprocessors. Copy attributes vom headers to (Swagger) headerSchema
65-
ObjectSchema headerSchema = new ObjectSchema();
66-
headerSchema.setName(schemaName);
67-
headerSchema.setTitle(schemaWithoutRef.getTitle());
68-
headerSchema.setDescription(schemaWithoutRef.getDescription());
69-
70-
// transform properties of headers to a properties Map of Swagger schemas.
71-
// (Only one level, no deep transformation, see SwaggerSchemaUtil#mapToSwagger)
72-
//
73-
Map<String, Schema> properties = schemaWithoutRef.getProperties().entrySet().stream()
74-
.map((property) ->
75-
Map.entry(property.getKey(), (Schema<?>) swaggerSchemaMapper.mapToSwagger(property.getValue())))
76-
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
77-
headerSchema.setProperties(properties);
60+
Schema headerSchema = swaggerSchemaMapper.mapToSwagger(schemaWithoutRef);
7861

7962
// call postprocessors
80-
Map<String, Schema> newSchemasToProcess = Map.of(schemaName, headerSchema);
63+
Map<String, Schema> newSchemasToProcess = Map.of(headerSchema.getName(), headerSchema);
8164
postProcessSchemas(newSchemasToProcess, new HashMap<>(newSchemasToProcess), DEFAULT_CONTENT_TYPE);
8265

8366
// convert Swagger schema back to an AsyncApi SchemaObject

springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/SwaggerSchemaMapperTest.java

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.math.BigDecimal;
2222
import java.util.List;
23+
import java.util.Map;
2324
import java.util.Set;
2425
import java.util.stream.Stream;
2526

@@ -638,6 +639,20 @@ void mapMaxItems() {
638639

639640
@Nested
640641
class MapToSwagger {
642+
@Test
643+
void mapNameAndTitle() {
644+
// given
645+
SchemaObject schema = new SchemaObject();
646+
schema.setTitle("title");
647+
648+
// when
649+
Schema<?> swaggerSchema = swaggerSchemaMapper.mapToSwagger(schema);
650+
651+
// then
652+
assertThat(swaggerSchema.getTitle()).isEqualTo(schema.getTitle());
653+
assertThat(swaggerSchema.getName()).isEqualTo(schema.getTitle());
654+
}
655+
641656
@Test
642657
void mapDescription() {
643658
// given
@@ -651,6 +666,21 @@ void mapDescription() {
651666
assertThat(swaggerSchema.getDescription()).isEqualTo(schema.getDescription());
652667
}
653668

669+
@Test
670+
void mapFormat() {
671+
// given
672+
SchemaObject schema = new SchemaObject();
673+
schema.setType(SchemaType.STRING);
674+
schema.setFormat("email");
675+
676+
// when
677+
Schema<?> swaggerSchema = swaggerSchemaMapper.mapToSwagger(schema);
678+
679+
// then
680+
assertThat(swaggerSchema.getType()).isEqualTo(SchemaType.STRING);
681+
assertThat(swaggerSchema.getFormat()).isEqualTo("email");
682+
}
683+
654684
@Test
655685
void mapExamples() {
656686
// given
@@ -704,5 +734,79 @@ void mapType() {
704734
// then
705735
assertThat(swaggerSchema.getType()).isEqualTo(SchemaType.STRING);
706736
}
737+
738+
@Test
739+
void mapProperties() {
740+
// given
741+
SchemaObject property = new SchemaObject();
742+
property.setType(SchemaType.STRING);
743+
744+
SchemaObject schema = new SchemaObject();
745+
schema.setProperties(Map.of("property", ComponentSchema.of(property)));
746+
747+
// when
748+
Schema<?> swaggerSchema = swaggerSchemaMapper.mapToSwagger(schema);
749+
750+
// then
751+
assertThat(swaggerSchema.getProperties()).hasSize(1).containsKey("property");
752+
assertThat((swaggerSchema.getProperties().get("property")).getType())
753+
.isEqualTo(SchemaType.STRING);
754+
}
755+
756+
@Test
757+
void mapComponentSchemaSchema() {
758+
// given
759+
SchemaObject schema = new SchemaObject();
760+
schema.setType(SchemaType.STRING);
761+
762+
MultiFormatSchema multiFormatSchema = new MultiFormatSchema(SchemaFormat.DEFAULT.toString(), schema);
763+
ComponentSchema componentSchema = ComponentSchema.of(multiFormatSchema);
764+
765+
// when
766+
Schema<?> swaggerSchema = swaggerSchemaMapper.mapToSwagger(componentSchema);
767+
768+
// then
769+
assertThat(swaggerSchema.getType()).isEqualTo(SchemaType.STRING);
770+
}
771+
772+
@Test
773+
void mapMultiFormatSchema() {
774+
// given
775+
SchemaObject schema = new SchemaObject();
776+
schema.setType(SchemaType.STRING);
777+
778+
MultiFormatSchema multiFormatSchema = new MultiFormatSchema(SchemaFormat.DEFAULT.toString(), schema);
779+
780+
// when
781+
Schema<?> swaggerSchema = swaggerSchemaMapper.mapToSwagger(multiFormatSchema);
782+
783+
// then
784+
assertThat(swaggerSchema.getType()).isEqualTo(SchemaType.STRING);
785+
}
786+
787+
@Test
788+
void mapReference() {
789+
// given
790+
SchemaReference reference = new SchemaReference("#/components/schemas/MySchema");
791+
792+
// when
793+
Schema<?> swaggerSchema = swaggerSchemaMapper.mapToSwagger(reference);
794+
795+
// then
796+
assertThat(swaggerSchema.get$ref()).isEqualTo(reference.getRef());
797+
}
798+
799+
@Test
800+
void doNotMapAlreadyMappedSchema() {
801+
// given
802+
Schema<?> schema = new Schema<>();
803+
schema.setType(SchemaType.STRING);
804+
805+
// when
806+
Schema<?> swaggerSchema = swaggerSchemaMapper.mapToSwagger(schema);
807+
808+
// then
809+
assertThat(swaggerSchema).isSameAs(schema);
810+
}
707811
}
708812
}

0 commit comments

Comments
 (0)