Skip to content

Commit

Permalink
Refactor/rename to payload schema object (#922)
Browse files Browse the repository at this point in the history
* refactor(core): rename NamedSchemaObject to PayloadSchemaObject

extracted from #890

Co-authored-by: David Beck <[email protected]>

* refactor(core): use SchemaType constants

---------

Co-authored-by: David Beck <[email protected]>
  • Loading branch information
timonback and David Beck authored Aug 16, 2024
1 parent d017164 commit f3f412f
Show file tree
Hide file tree
Showing 48 changed files with 229 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.github.springwolf.asyncapi.v3.model.AsyncAPI;
import io.github.springwolf.asyncapi.v3.model.components.Components;
import io.github.springwolf.asyncapi.v3.model.schema.SchemaObject;
import io.github.springwolf.asyncapi.v3.model.schema.SchemaType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -43,7 +44,7 @@ public void shouldAddJsonSchemaExtensionTest() throws JsonProcessingException {
// given
AsyncAPI asyncAPI = createAsyncApi();
SchemaObject schemaObject = new SchemaObject();
schemaObject.setType("object");
schemaObject.setType(SchemaType.OBJECT);
asyncAPI.getComponents().setSchemas(Map.of("schema", schemaObject));

when(jsonSchemaGenerator.fromSchema(any(), any())).thenReturn("mock-string");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.github.springwolf.asyncapi.v3.model.channel.message.MessageReference;
import io.github.springwolf.asyncapi.v3.model.components.ComponentSchema;
import io.github.springwolf.asyncapi.v3.model.schema.SchemaObject;
import io.github.springwolf.asyncapi.v3.model.schema.SchemaType;
import io.github.springwolf.core.asyncapi.schemas.SwaggerSchemaUtil;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.models.media.ArraySchema;
Expand Down Expand Up @@ -50,14 +51,14 @@ public void validateJsonSchemaTest(String expectedJsonSchema, Supplier<Schema<?>

// ref cycle ping -> pingField -> pong -> pongField -> ping (repeat)
SchemaObject pingSchema = new SchemaObject();
pingSchema.setType("object");
pingSchema.setType(SchemaType.OBJECT);
pingSchema.setProperties(Map.of("pingfield", ComponentSchema.of(MessageReference.toSchema("PongSchema"))));
SchemaObject pongSchema = new SchemaObject();
pongSchema.setType("object");
pongSchema.setType(SchemaType.OBJECT);
pongSchema.setProperties(Map.of("pongField", ComponentSchema.of(MessageReference.toSchema("PingSchema"))));

SchemaObject stringSchema = new SchemaObject();
stringSchema.setType("string");
stringSchema.setType(SchemaType.STRING);

Map<String, SchemaObject> definitions =
Map.of("StringRef", stringSchema, "PingSchema", pingSchema, "PongSchema", pongSchema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.github.springwolf.asyncapi.v3.model.operation.Operation;
import io.github.springwolf.asyncapi.v3.model.operation.OperationAction;
import io.github.springwolf.asyncapi.v3.model.schema.SchemaObject;
import io.github.springwolf.asyncapi.v3.model.schema.SchemaType;
import io.github.springwolf.asyncapi.v3.model.server.Server;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -52,11 +53,11 @@ void shouldSerializeKafkaOperationBinding() throws IOException {
"kafka",
KafkaOperationBinding.builder()
.groupId(SchemaObject.builder()
.type("string")
.type(SchemaType.STRING)
.enumValues(List.of("myGroupId"))
.build())
.clientId(SchemaObject.builder()
.type("string")
.type(SchemaType.STRING)
.enumValues(List.of("myClientId"))
.build())
.build()))
Expand Down Expand Up @@ -140,7 +141,7 @@ void shouldSerializeKafkaMessage() throws IOException {
KafkaMessageBinding.builder()
.key(
SchemaObject.builder()
.type("string")
.type(SchemaType.STRING)
.enumValues(List.of("myKey"))
.build())
.schemaIdLocation("payload")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.github.springwolf.asyncapi.v3.jackson.DefaultAsyncApiSerializerService;
import io.github.springwolf.asyncapi.v3.model.AsyncAPI;
import io.github.springwolf.asyncapi.v3.model.schema.SchemaObject;
import io.github.springwolf.asyncapi.v3.model.schema.SchemaType;
import io.github.springwolf.asyncapi.v3.model.server.Server;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -58,12 +59,12 @@ void shouldSerializeMQTTServerExample2() throws IOException {
"mqtt",
MQTTServerBinding.builder()
.sessionExpiryInterval(SchemaObject.builder()
.type("integer")
.type(SchemaType.INTEGER)
.minimum(new BigDecimal("30"))
.maximum(new BigDecimal("1200"))
.build())
.maximumPacketSize(SchemaObject.builder()
.type("integer")
.type(SchemaType.INTEGER)
.minimum(new BigDecimal("256"))
.build())
.build()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ private AsyncAPI getAsyncAPITestObject() {
"kafka",
KafkaMessageBinding.builder()
// FIXME: We should have a SchemaString (Schema<String>)
.key(SchemaObject.builder().type("string").build())
.key(SchemaObject.builder()
.type(SchemaType.STRING)
.build())
.build()))
.build();
Map<String, Message> messages = Map.of(message.getMessageId(), message);
Expand Down Expand Up @@ -100,9 +102,9 @@ private AsyncAPI getAsyncAPITestObject() {
.build();

SchemaObject examplePayloadSchema = new SchemaObject();
examplePayloadSchema.setType("object");
examplePayloadSchema.setType(SchemaType.OBJECT);
SchemaObject stringSchema = new SchemaObject();
stringSchema.setType("string");
stringSchema.setType(SchemaType.STRING);
examplePayloadSchema.setProperties(Map.of("s", stringSchema));
Map<String, SchemaObject> schemas = Map.of("ExamplePayload", examplePayloadSchema);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.github.springwolf.asyncapi.v3.model.operation.OperationTraits;
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.github.springwolf.asyncapi.v3.model.security_scheme.SecurityScheme;
import io.github.springwolf.asyncapi.v3.model.security_scheme.SecurityType;
import io.github.springwolf.asyncapi.v3.model.server.Server;
Expand All @@ -40,16 +41,16 @@ void shouldCreateSimpleAsyncAPI() throws IOException {
var userSignUpMessage = MessageObject.builder()
.messageId("UserSignedUp")
.payload(MessagePayload.of(SchemaObject.builder()
.type("object")
.type(SchemaType.OBJECT)
.properties(Map.of(
"displayName",
SchemaObject.builder()
.type("string")
.type(SchemaType.STRING)
.description("Name of the user")
.build(),
"email",
SchemaObject.builder()
.type("string")
.type(SchemaType.STRING)
.format("email")
.description("Email of the user")
.build()))
Expand Down Expand Up @@ -284,11 +285,11 @@ void shouldCreateStreetlightsKafkaAsyncAPI() throws IOException {
.schemas(Map.of(
"lightMeasuredPayload",
SchemaObject.builder()
.type("object")
.type(SchemaType.OBJECT)
.properties(Map.of(
"lumens",
SchemaObject.builder()
.type("integer")
.type(SchemaType.INTEGER)
.minimum(BigDecimal.ZERO)
.description("Light intensity measured in lumens.")
.build(),
Expand All @@ -297,11 +298,11 @@ void shouldCreateStreetlightsKafkaAsyncAPI() throws IOException {
.build(),
"turnOnOffPayload",
SchemaObject.builder()
.type("object")
.type(SchemaType.OBJECT)
.properties(Map.of(
"command",
SchemaObject.builder()
.type("string")
.type(SchemaType.STRING)
.enumValues(List.of("on", "off"))
.description("Whether to turn on or off the light.")
.build(),
Expand All @@ -310,11 +311,11 @@ void shouldCreateStreetlightsKafkaAsyncAPI() throws IOException {
.build(),
"dimLightPayload",
SchemaObject.builder()
.type("object")
.type(SchemaType.OBJECT)
.properties(Map.of(
"percentage",
SchemaObject.builder()
.type("integer")
.type(SchemaType.INTEGER)
.description(
"Percentage to which the light should be dimmed to.")
.minimum(BigDecimal.ZERO)
Expand All @@ -325,7 +326,7 @@ void shouldCreateStreetlightsKafkaAsyncAPI() throws IOException {
.build(),
"sentAt",
SchemaObject.builder()
.type("string")
.type(SchemaType.STRING)
.format("date-time")
.description("Date and time when the message was sent.")
.build()))
Expand All @@ -349,11 +350,11 @@ void shouldCreateStreetlightsKafkaAsyncAPI() throws IOException {
"commonHeaders",
MessageTrait.builder()
.headers(MessageHeaders.of(SchemaObject.builder()
.type("object")
.type(SchemaType.OBJECT)
.properties(Map.of(
"my-app-header",
SchemaObject.builder()
.type("integer")
.type(SchemaType.INTEGER)
.minimum(BigDecimal.ZERO)
.maximum(new BigDecimal("100"))
.build()))
Expand All @@ -366,7 +367,7 @@ void shouldCreateStreetlightsKafkaAsyncAPI() throws IOException {
"kafka",
KafkaOperationBinding.builder()
.clientId(SchemaObject.builder()
.type("string")
.type(SchemaType.STRING)
.enumValues(List.of("my-app-id"))
.build())
.build()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.github.springwolf.asyncapi.v3.model.channel.message.MessageTrait;
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 org.junit.jupiter.api.Test;

import java.io.IOException;
Expand All @@ -35,22 +36,22 @@ void shouldSerializeMessage() throws IOException {
Tag.builder().name("signup").build(),
Tag.builder().name("register").build()))
.headers(MessageHeaders.of(SchemaObject.builder()
.type("object")
.type(SchemaType.OBJECT)
.properties(Map.of(
"correlationId",
SchemaObject.builder()
.description("Correlation ID set by application")
.type("string")
.type(SchemaType.STRING)
.build(),
"applicationInstanceId",
SchemaObject.builder()
.description(
"Unique identifier for a given instance of the publishing application")
.type("string")
.type(SchemaType.STRING)
.build()))
.build()))
.payload(MessagePayload.of(SchemaObject.builder()
.type("object")
.type(SchemaType.OBJECT)
.properties(Map.of(
"user", SchemaReference.fromSchema("userCreate"),
"signup", SchemaReference.fromSchema("signup")))
Expand Down
Loading

0 comments on commit f3f412f

Please sign in to comment.