Skip to content

Commit f05b0b2

Browse files
committed
reproduce: springwolfGH-723 enum serialization
1 parent 7b959f3 commit f05b0b2

File tree

4 files changed

+116
-48
lines changed

4 files changed

+116
-48
lines changed

springwolf-examples/springwolf-kafka-example/src/main/java/io/github/springwolf/examples/kafka/dtos/ExamplePayloadDto.java

+20-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import io.swagger.v3.oas.annotations.media.Schema;
55
import lombok.AllArgsConstructor;
66
import lombok.Data;
7+
import lombok.Getter;
78
import lombok.NoArgsConstructor;
89
import lombok.RequiredArgsConstructor;
10+
import lombok.ToString;
911

1012
import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;
1113

@@ -45,12 +47,26 @@ public class ExamplePayloadDto {
4547
@Schema(description = "Some enum field", example = "FOO2", requiredMode = REQUIRED)
4648
private ExampleEnum someEnum;
4749

50+
@Schema(enumAsRef = true, example = "OK")
51+
@Getter
4852
@RequiredArgsConstructor
53+
@ToString
4954
public enum ExampleEnum {
50-
FOO1(1),
51-
FOO2(2),
52-
FOO3(3);
55+
OK(200, "OK"),
56+
BAD_REQUEST(400, "Bad Request"),
57+
UNAUTHORIZED(401, "Unauthorized"),
58+
NOT_FOUND(404, "Not Found"),
59+
INTERNAL_SERVER_ERROR(500, "Internal Server Error"),
60+
;
5361

54-
private final int code;
62+
private final int value;
63+
private final String reasonPhrase;
64+
65+
/**
66+
* @return true if messageStatus's value indicates an error (value >= 400)
67+
*/
68+
public boolean isError() {
69+
return value >= 400;
70+
}
5571
}
5672
}

springwolf-examples/springwolf-kafka-example/src/test/java/io/github/springwolf/examples/kafka/KafkaProducerSystemTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.List;
2424
import java.util.concurrent.TimeUnit;
2525

26-
import static io.github.springwolf.examples.kafka.dtos.ExamplePayloadDto.ExampleEnum.FOO1;
2726
import static org.assertj.core.api.Assertions.assertThat;
2827

2928
/**
@@ -71,7 +70,7 @@ void producerCanUseSpringwolfConfigurationToSendMessage() throws JsonProcessingE
7170
headers.put("kafka_offset", List.of("0"));
7271
headers.put("kafka_receivedMessageKey", List.of("string"));
7372

74-
ExamplePayloadDto payload = new ExamplePayloadDto("foo", 5, FOO1);
73+
ExamplePayloadDto payload = new ExamplePayloadDto("foo", 5, ExamplePayloadDto.ExampleEnum.OK);
7574
String payloadAsString = new ObjectMapper().writeValueAsString(payload).replaceAll("\"", "\\\\\"");
7675
String message = "{\n" //
7776
+ " \"bindings\": {},\n"

springwolf-examples/springwolf-kafka-example/src/test/resources/asyncapi.json

+50-22
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@
951951
"examples": [
952952
{
953953
"example": {
954-
"someEnum": "FOO2",
954+
"someEnum": "OK",
955955
"someLong": 5,
956956
"someString": "some string value"
957957
},
@@ -971,10 +971,13 @@
971971
"someEnum": {
972972
"description": "Some enum field",
973973
"enum": [
974-
"FOO1",
975-
"FOO2",
976-
"FOO3"
974+
"OK",
975+
"BAD_REQUEST",
976+
"UNAUTHORIZED",
977+
"NOT_FOUND",
978+
"INTERNAL_SERVER_ERROR"
977979
],
980+
"title": "ExampleEnum",
978981
"type": "string"
979982
},
980983
"someLong": {
@@ -1013,16 +1016,7 @@
10131016
"type": "object",
10141017
"properties": {
10151018
"someEnum": {
1016-
"type": "string",
1017-
"description": "Some enum field",
1018-
"enum": [
1019-
"FOO1",
1020-
"FOO2",
1021-
"FOO3"
1022-
],
1023-
"examples": [
1024-
"FOO2"
1025-
]
1019+
"$ref": "#/components/schemas/io.github.springwolf.examples.kafka.dtos.ExamplePayloadDto$ExampleEnum"
10261020
},
10271021
"someLong": {
10281022
"type": "integer",
@@ -1044,7 +1038,7 @@
10441038
"description": "Example payload model demonstrating markdown text styling:\n**bold**, *cursive* and <u>underlined</u>\n",
10451039
"examples": [
10461040
{
1047-
"someEnum": "FOO2",
1041+
"someEnum": "OK",
10481042
"someLong": 5,
10491043
"someString": "some string value"
10501044
}
@@ -1060,10 +1054,13 @@
10601054
"someEnum": {
10611055
"description": "Some enum field",
10621056
"enum": [
1063-
"FOO1",
1064-
"FOO2",
1065-
"FOO3"
1057+
"OK",
1058+
"BAD_REQUEST",
1059+
"UNAUTHORIZED",
1060+
"NOT_FOUND",
1061+
"INTERNAL_SERVER_ERROR"
10661062
],
1063+
"title": "ExampleEnum",
10671064
"type": "string"
10681065
},
10691066
"someLong": {
@@ -1085,6 +1082,34 @@
10851082
"type": "object"
10861083
}
10871084
},
1085+
"io.github.springwolf.examples.kafka.dtos.ExamplePayloadDto$ExampleEnum": {
1086+
"title": "ExampleEnum",
1087+
"type": "string",
1088+
"description": "Some enum field",
1089+
"enum": [
1090+
"OK",
1091+
"BAD_REQUEST",
1092+
"UNAUTHORIZED",
1093+
"NOT_FOUND",
1094+
"INTERNAL_SERVER_ERROR"
1095+
],
1096+
"examples": [
1097+
"OK"
1098+
],
1099+
"x-json-schema": {
1100+
"$schema": "https://json-schema.org/draft-04/schema#",
1101+
"description": "Some enum field",
1102+
"enum": [
1103+
"OK",
1104+
"BAD_REQUEST",
1105+
"UNAUTHORIZED",
1106+
"NOT_FOUND",
1107+
"INTERNAL_SERVER_ERROR"
1108+
],
1109+
"title": "ExampleEnum",
1110+
"type": "string"
1111+
}
1112+
},
10881113
"io.github.springwolf.examples.kafka.dtos.NestedPayloadDto": {
10891114
"title": "NestedPayloadDto",
10901115
"type": "object",
@@ -1112,7 +1137,7 @@
11121137
{
11131138
"examplePayloads": [
11141139
{
1115-
"someEnum": "FOO2",
1140+
"someEnum": "OK",
11161141
"someLong": 5,
11171142
"someString": "some string value"
11181143
}
@@ -1133,10 +1158,13 @@
11331158
"someEnum": {
11341159
"description": "Some enum field",
11351160
"enum": [
1136-
"FOO1",
1137-
"FOO2",
1138-
"FOO3"
1161+
"OK",
1162+
"BAD_REQUEST",
1163+
"UNAUTHORIZED",
1164+
"NOT_FOUND",
1165+
"INTERNAL_SERVER_ERROR"
11391166
],
1167+
"title": "ExampleEnum",
11401168
"type": "string"
11411169
},
11421170
"someLong": {

springwolf-examples/springwolf-kafka-example/src/test/resources/asyncapi.yaml

+45-20
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ components:
648648
description: Another payload model
649649
examples:
650650
- example:
651-
someEnum: FOO2
651+
someEnum: OK
652652
someLong: 5
653653
someString: some string value
654654
foo: bar
@@ -666,9 +666,12 @@ components:
666666
someEnum:
667667
description: Some enum field
668668
enum:
669-
- FOO1
670-
- FOO2
671-
- FOO3
669+
- OK
670+
- BAD_REQUEST
671+
- UNAUTHORIZED
672+
- NOT_FOUND
673+
- INTERNAL_SERVER_ERROR
674+
title: ExampleEnum
672675
type: string
673676
someLong:
674677
description: Some long field
@@ -708,14 +711,7 @@ components:
708711
type: object
709712
properties:
710713
someEnum:
711-
type: string
712-
description: Some enum field
713-
enum:
714-
- FOO1
715-
- FOO2
716-
- FOO3
717-
examples:
718-
- FOO2
714+
$ref: "#/components/schemas/io.github.springwolf.examples.kafka.dtos.ExamplePayloadDto$ExampleEnum"
719715
someLong:
720716
type: integer
721717
description: Some long field
@@ -744,7 +740,7 @@ components:
744740
Example payload model demonstrating markdown text styling:
745741
**bold**, *cursive* and <u>underlined</u>
746742
examples:
747-
- someEnum: FOO2
743+
- someEnum: OK
748744
someLong: 5
749745
someString: some string value
750746
required:
@@ -759,9 +755,12 @@ components:
759755
someEnum:
760756
description: Some enum field
761757
enum:
762-
- FOO1
763-
- FOO2
764-
- FOO3
758+
- OK
759+
- BAD_REQUEST
760+
- UNAUTHORIZED
761+
- NOT_FOUND
762+
- INTERNAL_SERVER_ERROR
763+
title: ExampleEnum
765764
type: string
766765
someLong:
767766
description: Some long field
@@ -788,6 +787,29 @@ components:
788787
- someString
789788
title: ExamplePayloadDto
790789
type: object
790+
io.github.springwolf.examples.kafka.dtos.ExamplePayloadDto$ExampleEnum:
791+
title: ExampleEnum
792+
type: string
793+
description: Some enum field
794+
enum:
795+
- OK
796+
- BAD_REQUEST
797+
- UNAUTHORIZED
798+
- NOT_FOUND
799+
- INTERNAL_SERVER_ERROR
800+
examples:
801+
- OK
802+
x-json-schema:
803+
$schema: https://json-schema.org/draft-04/schema#
804+
description: Some enum field
805+
enum:
806+
- OK
807+
- BAD_REQUEST
808+
- UNAUTHORIZED
809+
- NOT_FOUND
810+
- INTERNAL_SERVER_ERROR
811+
title: ExampleEnum
812+
type: string
791813
io.github.springwolf.examples.kafka.dtos.NestedPayloadDto:
792814
title: NestedPayloadDto
793815
type: object
@@ -807,7 +829,7 @@ components:
807829
description: Payload model with nested complex types
808830
examples:
809831
- examplePayloads:
810-
- someEnum: FOO2
832+
- someEnum: OK
811833
someLong: 5
812834
someString: some string value
813835
someStrings:
@@ -825,9 +847,12 @@ components:
825847
someEnum:
826848
description: Some enum field
827849
enum:
828-
- FOO1
829-
- FOO2
830-
- FOO3
850+
- OK
851+
- BAD_REQUEST
852+
- UNAUTHORIZED
853+
- NOT_FOUND
854+
- INTERNAL_SERVER_ERROR
855+
title: ExampleEnum
831856
type: string
832857
someLong:
833858
description: Some long field

0 commit comments

Comments
 (0)