Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.PrimitiveType;
import io.swagger.v3.core.util.RefUtils;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -29,7 +28,6 @@
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

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

Expand Down Expand Up @@ -69,25 +67,10 @@ public record ExtractedSchemas(ComponentSchema rootSchema, Map<String, Component
* @return
*/
public SchemaObject extractSchema(SchemaObject headers) {
String schemaName = headers.getTitle();

// create a swagger schema to invoke the postprocessors. Copy attributes vom headers to (Swagger) headerSchema
ObjectSchema headerSchema = new ObjectSchema();
headerSchema.setName(schemaName);
headerSchema.setTitle(headers.getTitle());
headerSchema.setDescription(headers.getDescription());

// transform properties of headers to a properties Map of Swagger schemas.
// (Only one level, no deep transformation, see SwaggerSchemaUtil#mapToSwagger)
//
Map<String, Schema> properties = headers.getProperties().entrySet().stream()
.map((property) ->
Map.entry(property.getKey(), (Schema<?>) swaggerSchemaUtil.mapToSwagger(property.getValue())))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
headerSchema.setProperties(properties);
Schema headerSchema = swaggerSchemaUtil.mapToSwagger(headers);

// call postprocessors
Map<String, Schema> newSchemasToProcess = Map.of(schemaName, headerSchema);
Map<String, Schema> newSchemasToProcess = Map.of(headerSchema.getName(), headerSchema);
postProcessSchemas(newSchemasToProcess, new HashMap<>(newSchemasToProcess), DEFAULT_CONTENT_TYPE);

// convert Swagger schema back to an AsnycApi SchemaObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import io.github.springwolf.asyncapi.v3.model.schema.SchemaObject;
import io.github.springwolf.asyncapi.v3.model.schema.SchemaReference;
import io.github.springwolf.asyncapi.v3.model.schema.SchemaType;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import lombok.RequiredArgsConstructor;
import org.springframework.lang.Nullable;

import java.util.ArrayList;
import java.util.HashSet;
Expand Down Expand Up @@ -204,7 +204,7 @@ private static void assignType(Schema value, SchemaObject.SchemaObjectBuilder bu
* @param schema
* @return
*/
public Object unwrapSchema(Object schema) {
private Object unwrapSchema(Object schema) {
if (schema instanceof ComponentSchema componentSchema) {
Object unwrappedSchema = componentSchema.getSchema();
if (unwrappedSchema == null) {
Expand Down Expand Up @@ -238,47 +238,55 @@ public Object unwrapSchema(Object schema) {
* @param schema Object representing an schema.
* @return the resulting Schema
*/
@Nullable
public Schema<?> mapToSwagger(Object schema) {
// first unwrap ComponentSchema and MultiFormatSchema:
Object unwrappedSchema = unwrapSchema(schema);

if (unwrappedSchema instanceof Schema<?> swaggerSchema) {
return swaggerSchema;
}

if (unwrappedSchema instanceof SchemaObject schemaObject) {
return mapSchemaObjectToSwagger(schemaObject);
}
if (unwrappedSchema instanceof SchemaReference schemaReference) {
return mapSchemaReferenceToSwagger(schemaReference);
}

throw new RuntimeException("Could not convert '" + schema + "' to a Swagger Schema");
}

/**
* transforms the given asyncApiSchema {@link SchemaObject} to a Swagger schema object.
* <p>Note</p>
* This method does not perform a 'deep' transformation, only the root attributes of asyncApiSchema
* are mapped to the Swagger schema. The properties of asyncApiSchema will not be mapped to the
* Swagger schema.
* are mapped to the Swagger schema (best effort).
* @param asyncApiSchema
* @return
* @return swagger Schema
*/
private Schema mapSchemaObjectToSwagger(SchemaObject asyncApiSchema) {
Schema swaggerSchema = new Schema();
Schema swaggerSchema = new ObjectSchema();
swaggerSchema.setName(asyncApiSchema.getTitle());
swaggerSchema.setTitle(asyncApiSchema.getTitle());

if (asyncApiSchema.getType() != null) {
swaggerSchema.setType(asyncApiSchema.getType().stream()
.filter(type -> !type.equals(SchemaType.NULL))
.findFirst()
.orElse(null));
swaggerSchema.setTypes(asyncApiSchema.getType());
}
// swaggerSchema.setFormat(asyncApiSchema.getFormat());
swaggerSchema.setFormat(asyncApiSchema.getFormat());
swaggerSchema.setDescription(asyncApiSchema.getDescription());
swaggerSchema.setExamples(asyncApiSchema.getExamples());
swaggerSchema.setEnum(asyncApiSchema.getEnumValues());

if (asyncApiSchema.getProperties() != null) {
Map<String, Schema> properties = asyncApiSchema.getProperties().entrySet().stream()
.map((property) -> Map.entry(property.getKey(), (Schema<?>) mapToSwagger(property.getValue())))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
swaggerSchema.setProperties(properties);
}

return swaggerSchema;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
package io.github.springwolf.core.asyncapi.components;

import io.github.springwolf.asyncapi.v3.model.components.ComponentSchema;
import io.github.springwolf.asyncapi.v3.model.schema.MultiFormatSchema;
import io.github.springwolf.asyncapi.v3.model.schema.SchemaFormat;
import io.github.springwolf.asyncapi.v3.model.schema.SchemaObject;
import io.github.springwolf.asyncapi.v3.model.schema.SchemaReference;
import io.github.springwolf.asyncapi.v3.model.schema.SchemaType;
Expand All @@ -15,6 +17,7 @@

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

Expand Down Expand Up @@ -497,17 +500,31 @@ void mapMaxItems() {

@Nested
class MapToSwagger {
@Test
void mapNameAndTitle() {
// given
SchemaObject schema = new SchemaObject();
schema.setTitle("title");

// when
Schema result = swaggerSchemaUtil.mapToSwagger(schema);

// then
assertThat(result.getTitle()).isEqualTo(schema.getTitle());
assertThat(result.getName()).isEqualTo(schema.getTitle());
}

@Test
void mapDescription() {
// given
SchemaObject schema = new SchemaObject();
schema.setDescription("description");

// when
Schema componentSchema = swaggerSchemaUtil.mapToSwagger(schema);
Schema result = swaggerSchemaUtil.mapToSwagger(schema);

// then
assertThat(componentSchema.getDescription()).isEqualTo(schema.getDescription());
assertThat(result.getDescription()).isEqualTo(schema.getDescription());
}

@Test
Expand All @@ -517,10 +534,10 @@ void mapExamples() {
schema.setExamples(List.of("example1", "example2"));

// when
Schema componentSchema = swaggerSchemaUtil.mapToSwagger(schema);
Schema result = swaggerSchemaUtil.mapToSwagger(schema);

// then
assertThat(componentSchema.getExamples()).isEqualTo(schema.getExamples());
assertThat(result.getExamples()).isEqualTo(schema.getExamples());
}

@Test
Expand All @@ -530,10 +547,10 @@ void mapEnum() {
schema.setEnumValues(List.of("enum1", "enum2"));

// when
Schema componentSchema = swaggerSchemaUtil.mapToSwagger(schema);
Schema result = swaggerSchemaUtil.mapToSwagger(schema);

// then
assertThat(componentSchema.getEnum()).isEqualTo(schema.getEnumValues());
assertThat(result.getEnum()).isEqualTo(schema.getEnumValues());
}

@Test
Expand All @@ -544,11 +561,11 @@ void mapNullableEnum() {
schema.setTypes(Set.of(SchemaType.STRING, SchemaType.NULL)); // nullable

// when
Schema componentSchema = swaggerSchemaUtil.mapToSwagger(schema);
Schema result = swaggerSchemaUtil.mapToSwagger(schema);

// then
assertThat(componentSchema.getEnum()).isEqualTo(schema.getEnumValues());
assertThat(componentSchema.getTypes()).isEqualTo(schema.getType());
assertThat(result.getEnum()).isEqualTo(schema.getEnumValues());
assertThat(result.getTypes()).isEqualTo(schema.getType());
}

@Test
Expand All @@ -558,10 +575,99 @@ void mapType() {
schema.setType(SchemaType.STRING);

// when
Schema componentSchema = swaggerSchemaUtil.mapToSwagger(schema);
Schema result = swaggerSchemaUtil.mapToSwagger(schema);

// then
assertThat(result.getType()).isEqualTo(SchemaType.STRING);
}

@Test
void mapFormat() {
// given
SchemaObject schema = new SchemaObject();
schema.setType(SchemaType.STRING);
schema.setFormat("email");

// when
Schema result = swaggerSchemaUtil.mapToSwagger(schema);

// then
assertThat(result.getType()).isEqualTo(SchemaType.STRING);
assertThat(result.getFormat()).isEqualTo("email");
}

@Test
void mapProperties() {
// given
SchemaObject property = new SchemaObject();
property.setType(SchemaType.STRING);

SchemaObject schema = new SchemaObject();
schema.setProperties(Map.of("property", ComponentSchema.of(property)));

// when
Schema result = swaggerSchemaUtil.mapToSwagger(schema);

// then
assertThat(result.getProperties()).hasSize(1).containsKey("property");
assertThat(((Schema) result.getProperties().get("property")).getType())
.isEqualTo(SchemaType.STRING);
}

@Test
void mapComponentSchemaSchema() {
// given
SchemaObject schema = new SchemaObject();
schema.setType(SchemaType.STRING);

MultiFormatSchema multiFormatSchema = new MultiFormatSchema(SchemaFormat.DEFAULT.toString(), schema);
ComponentSchema componentSchema = ComponentSchema.of(multiFormatSchema);

// when
Schema result = swaggerSchemaUtil.mapToSwagger(componentSchema);

// then
assertThat(result.getType()).isEqualTo(SchemaType.STRING);
}

@Test
void mapMultiFormatSchema() {
// given
SchemaObject schema = new SchemaObject();
schema.setType(SchemaType.STRING);

MultiFormatSchema multiFormatSchema = new MultiFormatSchema(SchemaFormat.DEFAULT.toString(), schema);

// when
Schema result = swaggerSchemaUtil.mapToSwagger(multiFormatSchema);

// then
assertThat(result.getType()).isEqualTo(SchemaType.STRING);
}

@Test
void mapReference() {
// given
SchemaReference reference = new SchemaReference("#/components/schemas/MySchema");

// when
Schema result = swaggerSchemaUtil.mapToSwagger(reference);

// then
assertThat(result.get$ref()).isEqualTo(reference.getRef());
}

@Test
void doNotMapAlreadyMappedSchema() {
// given
Schema schema = new Schema();
schema.setType(SchemaType.STRING);

// when
Schema result = swaggerSchemaUtil.mapToSwagger(schema);

// then
assertThat(componentSchema.getType()).isEqualTo(SchemaType.STRING);
assertThat(result).isSameAs(schema);
}
}
}
Loading
Loading