Skip to content

Commit 98a085c

Browse files
sam0r040timonback
andcommitted
chore(core): move wrapper logic for MultiFormatSchema to MultiFormatSchema, change back public api method from registerSimpleSchema to registerSchema to prevent breaking change, rename SwaggerSchemaService.postProcessSimpleSchema to postProcessSchemaWithoutRef to clarify the intention of the method
Co-authored-by: Timon Back <timonback@users.noreply.github.com>
1 parent ad80cfa commit 98a085c

File tree

14 files changed

+42
-45
lines changed

14 files changed

+42
-45
lines changed

springwolf-asyncapi/src/main/java/io/github/springwolf/asyncapi/v3/model/schema/MultiFormatSchema.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,12 @@ public class MultiFormatSchema extends ExtendableObject {
4949
*/
5050
@JsonProperty(value = "schema")
5151
private Object schema;
52+
53+
public static MultiFormatSchema of(Object schema) {
54+
// if payloadSchema.payload is already an instance of MultiFormatSchema, do not wrap again.
55+
return (schema instanceof MultiFormatSchema mfs)
56+
? mfs
57+
: MultiFormatSchema.builder().schema(schema).build();
58+
59+
}
5260
}

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/ComponentsService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ public interface ComponentsService {
4141

4242
/**
4343
* registers the given schema with this {@link ComponentsService}
44-
* @param headers the schema to register, typically a header schema
44+
* @param schemaWithoutRef the schema to register, typically a header schema
4545
* @return the title attribute of the given schema
4646
*/
47-
String registerSimpleSchema(SchemaObject headers);
47+
String registerSchema(SchemaObject schemaWithoutRef);
4848

4949
/**
5050
* Provides a map of all registered messages.

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/DefaultComponentsService.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import io.github.springwolf.asyncapi.v3.model.schema.SchemaFormat;
99
import io.github.springwolf.asyncapi.v3.model.schema.SchemaObject;
1010
import io.github.springwolf.core.asyncapi.schemas.SwaggerSchemaService;
11-
import io.github.springwolf.core.configuration.properties.PayloadSchemaFormat;
1211
import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties;
12+
import lombok.AllArgsConstructor;
1313
import lombok.extern.slf4j.Slf4j;
1414

1515
import java.lang.reflect.Type;
@@ -23,10 +23,11 @@
2323
* in the resulting AsyncApi object.
2424
*/
2525
@Slf4j
26+
@AllArgsConstructor
2627
public class DefaultComponentsService implements ComponentsService {
2728

2829
private final SwaggerSchemaService schemaService;
29-
private final SchemaFormat payloadSchemaFormat;
30+
private final SpringwolfConfigProperties springwolfConfigProperties;
3031

3132
/**
3233
* maps a schema name (key) to a detected corresponding {@link ComponentSchema}.
@@ -35,15 +36,6 @@ public class DefaultComponentsService implements ComponentsService {
3536

3637
private final Map<String, Message> messages = new HashMap<>();
3738

38-
public DefaultComponentsService(
39-
SwaggerSchemaService schemaService, SpringwolfConfigProperties springwolfConfigProperties) {
40-
this.schemaService = schemaService;
41-
42-
PayloadSchemaFormat payloadSchemaFormat =
43-
springwolfConfigProperties.getDocket().getPayloadSchemaFormat();
44-
this.payloadSchemaFormat = payloadSchemaFormat.getSchemaFormat();
45-
}
46-
4739
/**
4840
* Provides a map of all registered schemas.
4941
*
@@ -64,7 +56,8 @@ public Map<String, ComponentSchema> getSchemas() {
6456
*/
6557
@Override
6658
public ComponentSchema resolvePayloadSchema(Type type, String contentType) {
67-
59+
SchemaFormat payloadSchemaFormat =
60+
springwolfConfigProperties.getDocket().getPayloadSchemaFormat().getSchemaFormat();
6861
SwaggerSchemaService.ExtractedSchemas payload =
6962
schemaService.resolveSchema(type, contentType, payloadSchemaFormat);
7063
payload.referencedSchemas().forEach(schemas::putIfAbsent);
@@ -75,21 +68,21 @@ public ComponentSchema resolvePayloadSchema(Type type, String contentType) {
7568
* registers the given schema with this {@link ComponentsService}.
7669
* <p>NOTE</p>
7770
* Use only with schemas with max. one level of properties. Providing {@link SchemaObject}s with deep
78-
* property hierarchy will result in an corrupted result.
71+
* property hierarchy will result in a corrupted result.
7972
* <br/>
8073
* A typical usecase for this method is registering of header schemas, which have typically a simple structure.
8174
*
82-
* @param headers the schema to register, typically a header schema
75+
* @param schemaWithoutRef the schema to register, typically a header schema
8376
* @return the title attribute of the given schema
8477
*/
8578
@Override
86-
public String registerSimpleSchema(SchemaObject headers) {
87-
log.debug("Registering schema for {}", headers.getTitle());
79+
public String registerSchema(SchemaObject schemaWithoutRef) {
80+
log.debug("Registering schema for {}", schemaWithoutRef.getTitle());
8881

89-
ComponentSchema processedSchema = schemaService.postProcessSimpleSchema(headers);
90-
this.schemas.putIfAbsent(headers.getTitle(), processedSchema);
82+
ComponentSchema processedSchema = schemaService.postProcessSchemaWithoutRef(schemaWithoutRef);
83+
this.schemas.putIfAbsent(schemaWithoutRef.getTitle(), processedSchema);
9184

92-
return headers.getTitle();
85+
return schemaWithoutRef.getTitle();
9386
}
9487

9588
/**

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/common/message/AsyncAnnotationMessageService.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,13 @@ public MessageObject buildMessage(AsyncOperation operationData, Method method) {
3737
PayloadSchemaObject payloadSchema = payloadAsyncOperationService.extractSchema(operationData, method);
3838

3939
SchemaObject headerSchema = AsyncAnnotationUtil.getAsyncHeaders(operationData, stringValueResolver);
40-
String headerSchemaName = this.componentsService.registerSimpleSchema(headerSchema);
40+
String headerSchemaName = this.componentsService.registerSchema(headerSchema);
4141

4242
Map<String, MessageBinding> messageBinding =
4343
AsyncAnnotationUtil.processMessageBindingFromAnnotation(method, messageBindingProcessors);
4444

45-
// if payloadSchema.payload is already an instance of MultiFormatSchema, do not wrap again.
46-
MultiFormatSchema multiFormatSchema = (payloadSchema.payload() instanceof MultiFormatSchema mfs)
47-
? mfs
48-
: MultiFormatSchema.builder().schema(payloadSchema.payload()).build();
49-
50-
var messagePayload = MessagePayload.of(multiFormatSchema);
45+
MultiFormatSchema multiFormatSchema = MultiFormatSchema.of(payloadSchema.payload());
46+
MessagePayload messagePayload = MessagePayload.of(multiFormatSchema);
5147

5248
var builder = MessageObject.builder()
5349
.messageId(payloadSchema.name())

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/common/message/SpringAnnotationMessageService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public MessageObject buildMessage(
3131
MethodAnnotation annotation, PayloadSchemaObject payloadSchema, SchemaObject headers) {
3232
SchemaObject headerSchema = asyncHeadersBuilder.buildHeaders(payloadSchema);
3333
SchemaObject mergedHeaderSchema = HeaderSchemaObjectMerger.merge(headerSchema, headers);
34-
String headerModelName = componentsService.registerSimpleSchema(mergedHeaderSchema);
34+
String headerModelName = componentsService.registerSchema(mergedHeaderSchema);
3535

3636
Map<String, MessageBinding> messageBinding = bindingFactory.buildMessageBinding(annotation, mergedHeaderSchema);
3737

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/common/message/SpringAnnotationMessagesService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private MessageObject buildMessage(ClassAnnotation classAnnotation, Method metho
6262
SchemaObject headerSchema = asyncHeadersBuilder.buildHeaders(payloadSchema);
6363
SchemaObject headers = headerClassExtractor.extractHeader(method, payloadSchema);
6464
SchemaObject mergedHeaderSchema = HeaderSchemaObjectMerger.merge(headerSchema, headers);
65-
String headerSchemaName = componentsService.registerSimpleSchema(mergedHeaderSchema);
65+
String headerSchemaName = componentsService.registerSchema(mergedHeaderSchema);
6666

6767
Map<String, MessageBinding> messageBinding = bindingFactory.buildMessageBinding(classAnnotation, headerSchema);
6868

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/common/payload/internal/PayloadService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public PayloadSchemaObject buildSchema(String contentType, Type payloadType) {
5555
public PayloadSchemaObject useUnusedPayload() {
5656
ComponentSchema schema = PAYLOAD_NOT_USED.schema();
5757
if (schema != null && schema.getSchema() != null) {
58-
this.componentsService.registerSimpleSchema(schema.getSchema());
58+
this.componentsService.registerSchema(schema.getSchema());
5959
}
6060
return PAYLOAD_NOT_USED;
6161
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,27 @@ public record ExtractedSchemas(ComponentSchema rootSchema, Map<String, Component
6464
* postprocessors and converts the result back to a {@link SchemaObject}
6565
* <p>NOTE</p>
6666
* The conversion between the AsyncApi {@link SchemaObject} and Swagger schema instance is not a 'full conversion'. Only
67-
* root attributes of the schema an the first level of properties a converted. Providing {@link SchemaObject}s with deep
68-
* property hierarchy will result in an corrupted result.
67+
* root attributes of the schema and the first level of properties are converted. Providing {@link SchemaObject}s with deep
68+
* property hierarchy will result in a corrupted result.
6969
* <br/>
7070
* A typical usecase for this method is postprocessing of header schemas, which have typically a simple structure.
7171
*
72-
* @param headers
72+
* @param schemaWithoutRef
7373
* @return
7474
*/
75-
public ComponentSchema postProcessSimpleSchema(SchemaObject headers) {
76-
String schemaName = headers.getTitle();
75+
public ComponentSchema postProcessSchemaWithoutRef(SchemaObject schemaWithoutRef) {
76+
String schemaName = schemaWithoutRef.getTitle();
7777

7878
// create a swagger schema to invoke the postprocessors. Copy attributes vom headers to (Swagger) headerSchema
7979
ObjectSchema headerSchema = new ObjectSchema();
8080
headerSchema.setName(schemaName);
81-
headerSchema.setTitle(headers.getTitle());
82-
headerSchema.setDescription(headers.getDescription());
81+
headerSchema.setTitle(schemaWithoutRef.getTitle());
82+
headerSchema.setDescription(schemaWithoutRef.getDescription());
8383

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

springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/scanners/common/message/AsyncAnnotationMessageServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void setUp() {
4848
.when(stringValueResolver)
4949
.resolveStringValue(any());
5050

51-
when(componentsService.registerSimpleSchema(any())).thenReturn("headerSchemaName");
51+
when(componentsService.registerSchema(any())).thenReturn("headerSchemaName");
5252
when(messageBindingProcessor.process(any())).thenReturn(Optional.empty());
5353

5454
when(payloadAsyncOperationService.extractSchema(any(), any())).thenReturn(payloadSchema);

springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/scanners/common/payload/PayloadAsyncOperationServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,6 @@ void shouldReturnPayloadNotUsed() {
111111
SchemaObject schema = result.schema().getSchema();
112112
assertThat(schema.getTitle()).isEqualTo("PayloadNotUsed");
113113
assertThat(schema.getDescription()).isEqualTo("No payload specified");
114-
verify(componentsService).registerSimpleSchema(PAYLOAD_NOT_USED.schema().getSchema());
114+
verify(componentsService).registerSchema(PAYLOAD_NOT_USED.schema().getSchema());
115115
}
116116
}

0 commit comments

Comments
 (0)