diff --git a/springwolf-examples/springwolf-kafka-example/src/main/java/io/github/springwolf/examples/kafka/dtos/ExamplePayloadDto.java b/springwolf-examples/springwolf-kafka-example/src/main/java/io/github/springwolf/examples/kafka/dtos/ExamplePayloadDto.java index e72c6ccfc..9ff1d8a59 100644 --- a/springwolf-examples/springwolf-kafka-example/src/main/java/io/github/springwolf/examples/kafka/dtos/ExamplePayloadDto.java +++ b/springwolf-examples/springwolf-kafka-example/src/main/java/io/github/springwolf/examples/kafka/dtos/ExamplePayloadDto.java @@ -4,8 +4,10 @@ import io.swagger.v3.oas.annotations.media.Schema; import lombok.AllArgsConstructor; import lombok.Data; +import lombok.Getter; import lombok.NoArgsConstructor; import lombok.RequiredArgsConstructor; +import lombok.ToString; import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED; @@ -45,12 +47,26 @@ public class ExamplePayloadDto { @Schema(description = "Some enum field", example = "FOO2", requiredMode = REQUIRED) private ExampleEnum someEnum; + @Schema(enumAsRef = true, example = "OK") + @Getter @RequiredArgsConstructor + @ToString public enum ExampleEnum { - FOO1(1), - FOO2(2), - FOO3(3); + OK(200, "OK"), + BAD_REQUEST(400, "Bad Request"), + UNAUTHORIZED(401, "Unauthorized"), + NOT_FOUND(404, "Not Found"), + INTERNAL_SERVER_ERROR(500, "Internal Server Error"), + ; - private final int code; + private final int value; + private final String reasonPhrase; + + /** + * @return true if messageStatus's value indicates an error (value >= 400) + */ + public boolean isError() { + return value >= 400; + } } } diff --git a/springwolf-examples/springwolf-kafka-example/src/test/java/io/github/springwolf/examples/kafka/KafkaProducerSystemTest.java b/springwolf-examples/springwolf-kafka-example/src/test/java/io/github/springwolf/examples/kafka/KafkaProducerSystemTest.java index 47ea4d743..4bb809372 100644 --- a/springwolf-examples/springwolf-kafka-example/src/test/java/io/github/springwolf/examples/kafka/KafkaProducerSystemTest.java +++ b/springwolf-examples/springwolf-kafka-example/src/test/java/io/github/springwolf/examples/kafka/KafkaProducerSystemTest.java @@ -23,7 +23,6 @@ import java.util.List; import java.util.concurrent.TimeUnit; -import static io.github.springwolf.examples.kafka.dtos.ExamplePayloadDto.ExampleEnum.FOO1; import static org.assertj.core.api.Assertions.assertThat; /** @@ -71,7 +70,7 @@ void producerCanUseSpringwolfConfigurationToSendMessage() throws JsonProcessingE headers.put("kafka_offset", List.of("0")); headers.put("kafka_receivedMessageKey", List.of("string")); - ExamplePayloadDto payload = new ExamplePayloadDto("foo", 5, FOO1); + ExamplePayloadDto payload = new ExamplePayloadDto("foo", 5, ExamplePayloadDto.ExampleEnum.OK); String payloadAsString = new ObjectMapper().writeValueAsString(payload).replaceAll("\"", "\\\\\""); String message = "{\n" // + " \"bindings\": {},\n" diff --git a/springwolf-examples/springwolf-kafka-example/src/test/resources/asyncapi.json b/springwolf-examples/springwolf-kafka-example/src/test/resources/asyncapi.json index 5541261b0..af5751038 100644 --- a/springwolf-examples/springwolf-kafka-example/src/test/resources/asyncapi.json +++ b/springwolf-examples/springwolf-kafka-example/src/test/resources/asyncapi.json @@ -951,7 +951,7 @@ "examples": [ { "example": { - "someEnum": "FOO2", + "someEnum": "OK", "someLong": 5, "someString": "some string value" }, @@ -971,10 +971,13 @@ "someEnum": { "description": "Some enum field", "enum": [ - "FOO1", - "FOO2", - "FOO3" + "OK", + "BAD_REQUEST", + "UNAUTHORIZED", + "NOT_FOUND", + "INTERNAL_SERVER_ERROR" ], + "title": "ExampleEnum", "type": "string" }, "someLong": { @@ -1013,16 +1016,7 @@ "type": "object", "properties": { "someEnum": { - "type": "string", - "description": "Some enum field", - "enum": [ - "FOO1", - "FOO2", - "FOO3" - ], - "examples": [ - "FOO2" - ] + "$ref": "#/components/schemas/io.github.springwolf.examples.kafka.dtos.ExamplePayloadDto$ExampleEnum" }, "someLong": { "type": "integer", @@ -1044,7 +1038,7 @@ "description": "Example payload model demonstrating markdown text styling:\n**bold**, *cursive* and underlined\n", "examples": [ { - "someEnum": "FOO2", + "someEnum": "OK", "someLong": 5, "someString": "some string value" } @@ -1060,10 +1054,13 @@ "someEnum": { "description": "Some enum field", "enum": [ - "FOO1", - "FOO2", - "FOO3" + "OK", + "BAD_REQUEST", + "UNAUTHORIZED", + "NOT_FOUND", + "INTERNAL_SERVER_ERROR" ], + "title": "ExampleEnum", "type": "string" }, "someLong": { @@ -1085,6 +1082,34 @@ "type": "object" } }, + "io.github.springwolf.examples.kafka.dtos.ExamplePayloadDto$ExampleEnum": { + "title": "ExampleEnum", + "type": "string", + "description": "Some enum field", + "enum": [ + "OK", + "BAD_REQUEST", + "UNAUTHORIZED", + "NOT_FOUND", + "INTERNAL_SERVER_ERROR" + ], + "examples": [ + "OK" + ], + "x-json-schema": { + "$schema": "https://json-schema.org/draft-04/schema#", + "description": "Some enum field", + "enum": [ + "OK", + "BAD_REQUEST", + "UNAUTHORIZED", + "NOT_FOUND", + "INTERNAL_SERVER_ERROR" + ], + "title": "ExampleEnum", + "type": "string" + } + }, "io.github.springwolf.examples.kafka.dtos.NestedPayloadDto": { "title": "NestedPayloadDto", "type": "object", @@ -1112,7 +1137,7 @@ { "examplePayloads": [ { - "someEnum": "FOO2", + "someEnum": "OK", "someLong": 5, "someString": "some string value" } @@ -1133,10 +1158,13 @@ "someEnum": { "description": "Some enum field", "enum": [ - "FOO1", - "FOO2", - "FOO3" + "OK", + "BAD_REQUEST", + "UNAUTHORIZED", + "NOT_FOUND", + "INTERNAL_SERVER_ERROR" ], + "title": "ExampleEnum", "type": "string" }, "someLong": { diff --git a/springwolf-examples/springwolf-kafka-example/src/test/resources/asyncapi.yaml b/springwolf-examples/springwolf-kafka-example/src/test/resources/asyncapi.yaml index f1552a6c2..c92221795 100644 --- a/springwolf-examples/springwolf-kafka-example/src/test/resources/asyncapi.yaml +++ b/springwolf-examples/springwolf-kafka-example/src/test/resources/asyncapi.yaml @@ -648,7 +648,7 @@ components: description: Another payload model examples: - example: - someEnum: FOO2 + someEnum: OK someLong: 5 someString: some string value foo: bar @@ -666,9 +666,12 @@ components: someEnum: description: Some enum field enum: - - FOO1 - - FOO2 - - FOO3 + - OK + - BAD_REQUEST + - UNAUTHORIZED + - NOT_FOUND + - INTERNAL_SERVER_ERROR + title: ExampleEnum type: string someLong: description: Some long field @@ -708,14 +711,7 @@ components: type: object properties: someEnum: - type: string - description: Some enum field - enum: - - FOO1 - - FOO2 - - FOO3 - examples: - - FOO2 + $ref: "#/components/schemas/io.github.springwolf.examples.kafka.dtos.ExamplePayloadDto$ExampleEnum" someLong: type: integer description: Some long field @@ -744,7 +740,7 @@ components: Example payload model demonstrating markdown text styling: **bold**, *cursive* and underlined examples: - - someEnum: FOO2 + - someEnum: OK someLong: 5 someString: some string value required: @@ -759,9 +755,12 @@ components: someEnum: description: Some enum field enum: - - FOO1 - - FOO2 - - FOO3 + - OK + - BAD_REQUEST + - UNAUTHORIZED + - NOT_FOUND + - INTERNAL_SERVER_ERROR + title: ExampleEnum type: string someLong: description: Some long field @@ -788,6 +787,29 @@ components: - someString title: ExamplePayloadDto type: object + io.github.springwolf.examples.kafka.dtos.ExamplePayloadDto$ExampleEnum: + title: ExampleEnum + type: string + description: Some enum field + enum: + - OK + - BAD_REQUEST + - UNAUTHORIZED + - NOT_FOUND + - INTERNAL_SERVER_ERROR + examples: + - OK + x-json-schema: + $schema: https://json-schema.org/draft-04/schema# + description: Some enum field + enum: + - OK + - BAD_REQUEST + - UNAUTHORIZED + - NOT_FOUND + - INTERNAL_SERVER_ERROR + title: ExampleEnum + type: string io.github.springwolf.examples.kafka.dtos.NestedPayloadDto: title: NestedPayloadDto type: object @@ -807,7 +829,7 @@ components: description: Payload model with nested complex types examples: - examplePayloads: - - someEnum: FOO2 + - someEnum: OK someLong: 5 someString: some string value someStrings: @@ -825,9 +847,12 @@ components: someEnum: description: Some enum field enum: - - FOO1 - - FOO2 - - FOO3 + - OK + - BAD_REQUEST + - UNAUTHORIZED + - NOT_FOUND + - INTERNAL_SERVER_ERROR + title: ExampleEnum type: string someLong: description: Some long field