Skip to content

Commit

Permalink
reproduce: springwolfGH-723 enum serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
timonback committed Nov 9, 2024
1 parent 7b959f3 commit f05b0b2
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@
"examples": [
{
"example": {
"someEnum": "FOO2",
"someEnum": "OK",
"someLong": 5,
"someString": "some string value"
},
Expand All @@ -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": {
Expand Down Expand Up @@ -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",
Expand All @@ -1044,7 +1038,7 @@
"description": "Example payload model demonstrating markdown text styling:\n**bold**, *cursive* and <u>underlined</u>\n",
"examples": [
{
"someEnum": "FOO2",
"someEnum": "OK",
"someLong": 5,
"someString": "some string value"
}
Expand All @@ -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": {
Expand All @@ -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",
Expand Down Expand Up @@ -1112,7 +1137,7 @@
{
"examplePayloads": [
{
"someEnum": "FOO2",
"someEnum": "OK",
"someLong": 5,
"someString": "some string value"
}
Expand All @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ components:
description: Another payload model
examples:
- example:
someEnum: FOO2
someEnum: OK
someLong: 5
someString: some string value
foo: bar
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -744,7 +740,7 @@ components:
Example payload model demonstrating markdown text styling:
**bold**, *cursive* and <u>underlined</u>
examples:
- someEnum: FOO2
- someEnum: OK
someLong: 5
someString: some string value
required:
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit f05b0b2

Please sign in to comment.