Skip to content

Commit d1aa036

Browse files
authored
refactor/use asyncapi headers schema (#658)
* refactor(core): switch to asyncapi headers * refactor(core): remove unused jackson-annotations dependency
1 parent 074f5c0 commit d1aa036

File tree

24 files changed

+200
-177
lines changed

24 files changed

+200
-177
lines changed

springwolf-core/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ dependencies {
1313
implementation "io.swagger.core.v3:swagger-core-jakarta:${swaggerVersion}"
1414

1515
implementation "com.fasterxml.jackson.core:jackson-core:${jacksonVersion}"
16-
implementation "com.fasterxml.jackson.core:jackson-annotations:${jacksonVersion}"
1716
implementation "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}"
1817

1918
implementation "org.slf4j:slf4j-api:${slf4jApiVersion}"

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
import io.github.springwolf.asyncapi.v3.model.channel.message.MessageObject;
66
import io.github.springwolf.asyncapi.v3.model.channel.message.MessageReference;
77
import io.github.springwolf.asyncapi.v3.model.schema.SchemaObject;
8-
import io.github.springwolf.core.asyncapi.components.headers.AsyncHeaders;
98

109
import java.util.Map;
1110

1211
public interface ComponentsService {
1312

1413
Map<String, SchemaObject> getSchemas();
1514

16-
String registerSchema(AsyncHeaders headers);
15+
String registerSchema(SchemaObject headers);
1716

1817
String registerSchema(Class<?> type);
1918

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
import io.github.springwolf.asyncapi.v3.model.channel.message.MessageReference;
77
import io.github.springwolf.asyncapi.v3.model.schema.SchemaObject;
88
import io.github.springwolf.core.asyncapi.annotations.AsyncApiPayload;
9-
import io.github.springwolf.core.asyncapi.components.headers.AsyncHeaders;
109
import io.github.springwolf.core.asyncapi.components.postprocessors.SchemasPostProcessor;
1110
import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties;
1211
import io.swagger.v3.core.converter.ModelConverter;
1312
import io.swagger.v3.core.converter.ModelConverters;
1413
import io.swagger.v3.core.jackson.TypeNameResolver;
15-
import io.swagger.v3.oas.models.media.MapSchema;
14+
import io.swagger.v3.oas.models.media.ObjectSchema;
1615
import io.swagger.v3.oas.models.media.Schema;
1716
import io.swagger.v3.oas.models.media.StringSchema;
1817
import lombok.extern.slf4j.Slf4j;
@@ -62,18 +61,22 @@ public Map<String, SchemaObject> getSchemas() {
6261
}
6362

6463
@Override
65-
public String registerSchema(AsyncHeaders headers) {
66-
log.debug("Registering schema for {}", headers.getSchemaName());
64+
public String registerSchema(SchemaObject headers) {
65+
log.debug("Registering schema for {}", headers.getTitle());
6766

68-
MapSchema headerSchema = new MapSchema();
69-
headerSchema.setName(headers.getSchemaName());
67+
ObjectSchema headerSchema = new ObjectSchema();
68+
headerSchema.setName(headers.getTitle());
7069
headerSchema.setDescription(headers.getDescription());
71-
headerSchema.properties(headers);
70+
Map<String, Schema> properties = headers.getProperties().entrySet().stream()
71+
.map((property) -> Map.entry(property.getKey(), (Schema<?>)
72+
swaggerSchemaUtil.mapToSwagger((SchemaObject) property.getValue())))
73+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
74+
headerSchema.setProperties(properties);
7275

73-
this.schemas.put(headers.getSchemaName(), headerSchema);
76+
this.schemas.put(headers.getTitle(), headerSchema);
7477
postProcessSchema(headerSchema, DEFAULT_CONTENT_TYPE);
7578

76-
return headers.getSchemaName();
79+
return headers.getTitle();
7780
}
7881

7982
@Override

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,14 @@ public SchemaObject mapSchema(Schema value) {
141141

142142
return builder.build();
143143
}
144+
145+
public Schema mapToSwagger(SchemaObject asyncApiSchema) {
146+
Schema swaggerSchema = new Schema();
147+
swaggerSchema.setType("string");
148+
swaggerSchema.setDescription(asyncApiSchema.getDescription());
149+
swaggerSchema.setExamples(asyncApiSchema.getExamples());
150+
swaggerSchema.setEnum(asyncApiSchema.getEnumValues());
151+
152+
return swaggerSchema;
153+
}
144154
}

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

Lines changed: 0 additions & 32 deletions
This file was deleted.

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

Lines changed: 0 additions & 38 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// SPDX-License-Identifier: Apache-2.0
22
package io.github.springwolf.core.asyncapi.components.headers;
33

4+
import io.github.springwolf.asyncapi.v3.model.schema.SchemaObject;
5+
46
public interface AsyncHeadersBuilder {
5-
AsyncHeaders buildHeaders(Class<?> payloadType);
7+
SchemaObject buildHeaders(Class<?> payloadType);
68
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
// SPDX-License-Identifier: Apache-2.0
22
package io.github.springwolf.core.asyncapi.components.headers;
33

4+
import io.github.springwolf.asyncapi.v3.model.schema.SchemaObject;
5+
6+
import java.util.List;
7+
import java.util.Map;
8+
49
public class AsyncHeadersNotDocumented implements AsyncHeadersBuilder {
510
/**
611
* Per default, if no headers are explicitly defined, {@link AsyncHeadersNotUsed#NOT_USED} is used.
712
* There can be headers, but don't have to be.
813
*/
9-
public static final AsyncHeaders NOT_DOCUMENTED =
10-
new AsyncHeaders("HeadersNotDocumented", "There can be headers, but they are not explicitly documented.");
14+
public static final SchemaObject NOT_DOCUMENTED = new SchemaObject();
15+
16+
static {
17+
NOT_DOCUMENTED.setType("object");
18+
NOT_DOCUMENTED.setTitle("HeadersNotDocumented");
19+
NOT_DOCUMENTED.setDescription("There can be headers, but they are not explicitly documented.");
20+
NOT_DOCUMENTED.setProperties(Map.of());
21+
NOT_DOCUMENTED.setExamples(List.of(new Object()));
22+
}
1123

1224
@Override
13-
public AsyncHeaders buildHeaders(Class<?> payloadType) {
25+
public SchemaObject buildHeaders(Class<?> payloadType) {
1426
return NOT_DOCUMENTED;
1527
}
1628
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
// SPDX-License-Identifier: Apache-2.0
22
package io.github.springwolf.core.asyncapi.components.headers;
33

4+
import io.github.springwolf.asyncapi.v3.model.schema.SchemaObject;
5+
6+
import java.util.List;
7+
import java.util.Map;
8+
49
public class AsyncHeadersNotUsed implements AsyncHeadersBuilder {
510
/**
611
* Explicitly document that no headers are used.
712
*/
8-
public static final AsyncHeaders NOT_USED = new AsyncHeaders("HeadersNotUsed", "No headers are present.");
13+
public static final SchemaObject NOT_USED = new SchemaObject();
14+
15+
static {
16+
NOT_USED.setType("object");
17+
NOT_USED.setTitle("HeadersNotUsed");
18+
NOT_USED.setDescription("No headers are present.");
19+
NOT_USED.setProperties(Map.of());
20+
NOT_USED.setExamples(List.of(new Object()));
21+
}
922

1023
@Override
11-
public AsyncHeaders buildHeaders(Class<?> payloadType) {
24+
public SchemaObject buildHeaders(Class<?> payloadType) {
1225
return NOT_USED;
1326
}
1427
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
import io.github.springwolf.asyncapi.v3.model.operation.Operation;
1212
import io.github.springwolf.asyncapi.v3.model.operation.OperationAction;
1313
import io.github.springwolf.asyncapi.v3.model.schema.MultiFormatSchema;
14+
import io.github.springwolf.asyncapi.v3.model.schema.SchemaObject;
1415
import io.github.springwolf.asyncapi.v3.model.schema.SchemaReference;
1516
import io.github.springwolf.core.asyncapi.annotations.AsyncOperation;
1617
import io.github.springwolf.core.asyncapi.components.ComponentsService;
17-
import io.github.springwolf.core.asyncapi.components.headers.AsyncHeaders;
1818
import io.github.springwolf.core.asyncapi.scanners.bindings.messages.MessageBindingProcessor;
1919
import io.github.springwolf.core.asyncapi.scanners.bindings.operations.OperationBindingProcessor;
2020
import io.github.springwolf.core.asyncapi.scanners.common.payload.PayloadClassExtractor;
@@ -92,7 +92,7 @@ protected MessageObject buildMessage(AsyncOperation operationData, Method method
9292

9393
String modelName = this.componentsService.registerSchema(
9494
payloadType, operationData.message().contentType());
95-
AsyncHeaders asyncHeaders = AsyncAnnotationUtil.getAsyncHeaders(operationData, resolver);
95+
SchemaObject asyncHeaders = AsyncAnnotationUtil.getAsyncHeaders(operationData, resolver);
9696
String headerModelName = this.componentsService.registerSchema(asyncHeaders);
9797
var headers = MessageHeaders.of(MessageReference.toSchema(headerModelName));
9898

0 commit comments

Comments
 (0)