From ef4e47af06726dd3e4c94ae11259b8c78a84279d Mon Sep 17 00:00:00 2001 From: sam0r040 <93372330+sam0r040@users.noreply.github.com> Date: Fri, 17 Jan 2025 18:01:54 +0100 Subject: [PATCH] test(kafka-example): add polymorphism with oneOf to VehicleBase (#1152) --- .../kafka/dtos/discriminator/EnginePower.java | 14 ++ .../kafka/dtos/discriminator/VehicleBase.java | 3 + .../src/test/resources/asyncapi.json | 169 ++++++++++++++++-- .../src/test/resources/asyncapi.yaml | 115 ++++++++++-- .../src/test/resources/groups/vehicles.json | 96 +++++++++- 5 files changed, 367 insertions(+), 30 deletions(-) create mode 100644 springwolf-examples/springwolf-kafka-example/src/main/java/io/github/springwolf/examples/kafka/dtos/discriminator/EnginePower.java diff --git a/springwolf-examples/springwolf-kafka-example/src/main/java/io/github/springwolf/examples/kafka/dtos/discriminator/EnginePower.java b/springwolf-examples/springwolf-kafka-example/src/main/java/io/github/springwolf/examples/kafka/dtos/discriminator/EnginePower.java new file mode 100644 index 000000000..a4f345fff --- /dev/null +++ b/springwolf-examples/springwolf-kafka-example/src/main/java/io/github/springwolf/examples/kafka/dtos/discriminator/EnginePower.java @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: Apache-2.0 +package io.github.springwolf.examples.kafka.dtos.discriminator; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +@Data +@AllArgsConstructor +public class EnginePower { + private int hp; + private int torque; +} diff --git a/springwolf-examples/springwolf-kafka-example/src/main/java/io/github/springwolf/examples/kafka/dtos/discriminator/VehicleBase.java b/springwolf-examples/springwolf-kafka-example/src/main/java/io/github/springwolf/examples/kafka/dtos/discriminator/VehicleBase.java index faf449ee5..b3ca25b51 100644 --- a/springwolf-examples/springwolf-kafka-example/src/main/java/io/github/springwolf/examples/kafka/dtos/discriminator/VehicleBase.java +++ b/springwolf-examples/springwolf-kafka-example/src/main/java/io/github/springwolf/examples/kafka/dtos/discriminator/VehicleBase.java @@ -15,6 +15,7 @@ @JsonSubTypes.Type(value = VehicleGasolinePayloadDto.class, name = "VehicleGasolinePayloadDto"), }) @Schema( + oneOf = {VehicleElectricPayloadDto.class, VehicleGasolinePayloadDto.class}, subTypes = {VehicleElectricPayloadDto.class, VehicleGasolinePayloadDto.class}, discriminatorProperty = "vehicleType", discriminatorMapping = { @@ -30,4 +31,6 @@ public abstract class VehicleBase { private String powerSource; private int topSpeed; + + private EnginePower enginePower; } 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 150c14dca..fe008326e 100644 --- a/springwolf-examples/springwolf-kafka-example/src/test/resources/asyncapi.json +++ b/springwolf-examples/springwolf-kafka-example/src/test/resources/asyncapi.json @@ -1269,11 +1269,46 @@ "type": "string" } }, + "io.github.springwolf.examples.kafka.dtos.discriminator.EnginePower": { + "title": "EnginePower", + "type": "object", + "properties": { + "hp": { + "type": "integer", + "format": "int32" + }, + "torque": { + "type": "integer", + "format": "int32" + } + }, + "examples": [ + { + "hp": 0, + "torque": 0 + } + ], + "x-json-schema": { + "$schema": "https://json-schema.org/draft-04/schema#", + "properties": { + "hp": { + "format": "int32", + "type": "integer" + }, + "torque": { } + }, + "title": "EnginePower", + "type": "object" + } + }, "io.github.springwolf.examples.kafka.dtos.discriminator.VehicleBase": { "discriminator": "vehicleType", "title": "VehicleBase", "type": "object", "properties": { + "enginePower": { + "$ref": "#/components/schemas/io.github.springwolf.examples.kafka.dtos.discriminator.EnginePower" + }, "powerSource": { "type": "string" }, @@ -1288,22 +1323,73 @@ "description": "Demonstrates the use of discriminator for polymorphic deserialization (not publishable)", "examples": [ { + "batteryCapacity": 0, + "chargeTime": 0, + "enginePower": { + "hp": 0, + "torque": 0 + }, "powerSource": "string", "topSpeed": 0, "vehicleType": "string" } ], + "oneOf": [ + { + "$ref": "#/components/schemas/io.github.springwolf.examples.kafka.dtos.discriminator.VehicleElectricPayloadDto" + }, + { + "$ref": "#/components/schemas/io.github.springwolf.examples.kafka.dtos.discriminator.VehicleGasolinePayloadDto" + } + ], "x-json-schema": { "$schema": "https://json-schema.org/draft-04/schema#", "description": "Demonstrates the use of discriminator for polymorphic deserialization (not publishable)", + "oneOf": [ + { + "allOf": [ + { }, + { + "properties": { + "batteryCapacity": { }, + "chargeTime": { + "format": "int32", + "type": "integer" + } + }, + "type": "object" + } + ], + "description": "Electric vehicle implementation of VehicleBase", + "type": "object" + }, + { + "allOf": [ + { }, + { + "properties": { + "fuelCapacity": { } + }, + "type": "object" + } + ], + "description": "Gasoline vehicle implementation of VehicleBase", + "type": "object" + } + ], "properties": { + "enginePower": { + "properties": { + "hp": { }, + "torque": { } + }, + "title": "EnginePower", + "type": "object" + }, "powerSource": { "type": "string" }, - "topSpeed": { - "format": "int32", - "type": "integer" - }, + "topSpeed": { }, "vehicleType": { } }, "title": "VehicleBase", @@ -1317,6 +1403,10 @@ { "batteryCapacity": 0, "chargeTime": 0, + "enginePower": { + "hp": 0, + "torque": 0 + }, "powerSource": "string", "topSpeed": 0, "vehicleType": "string" @@ -1345,14 +1435,38 @@ "allOf": [ { "description": "Demonstrates the use of discriminator for polymorphic deserialization (not publishable)", + "oneOf": [ + { }, + { + "allOf": [ + { }, + { + "properties": { + "fuelCapacity": { + "format": "int32", + "type": "integer" + } + }, + "type": "object" + } + ], + "description": "Gasoline vehicle implementation of VehicleBase", + "type": "object" + } + ], "properties": { + "enginePower": { + "properties": { + "hp": { }, + "torque": { } + }, + "title": "EnginePower", + "type": "object" + }, "powerSource": { "type": "string" }, - "topSpeed": { - "format": "int32", - "type": "integer" - }, + "topSpeed": { }, "vehicleType": { } }, "title": "VehicleBase", @@ -1375,6 +1489,10 @@ "description": "Gasoline vehicle implementation of VehicleBase", "examples": [ { + "enginePower": { + "hp": 0, + "torque": 0 + }, "fuelCapacity": 0, "powerSource": "string", "topSpeed": 0, @@ -1400,14 +1518,39 @@ "allOf": [ { "description": "Demonstrates the use of discriminator for polymorphic deserialization (not publishable)", + "oneOf": [ + { + "allOf": [ + { }, + { + "properties": { + "batteryCapacity": { }, + "chargeTime": { + "format": "int32", + "type": "integer" + } + }, + "type": "object" + } + ], + "description": "Electric vehicle implementation of VehicleBase", + "type": "object" + }, + { } + ], "properties": { + "enginePower": { + "properties": { + "hp": { }, + "torque": { } + }, + "title": "EnginePower", + "type": "object" + }, "powerSource": { "type": "string" }, - "topSpeed": { - "format": "int32", - "type": "integer" - }, + "topSpeed": { }, "vehicleType": { } }, "title": "VehicleBase", @@ -2004,4 +2147,4 @@ ] } } -} \ No newline at end of file +} 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 36143209c..96b3f1495 100644 --- a/springwolf-examples/springwolf-kafka-example/src/test/resources/asyncapi.yaml +++ b/springwolf-examples/springwolf-kafka-example/src/test/resources/asyncapi.yaml @@ -936,11 +936,35 @@ components: type: string title: YamlPayloadDto type: string + io.github.springwolf.examples.kafka.dtos.discriminator.EnginePower: + title: EnginePower + type: object + properties: + hp: + type: integer + format: int32 + torque: + type: integer + format: int32 + examples: + - hp: 0 + torque: 0 + x-json-schema: + $schema: https://json-schema.org/draft-04/schema# + properties: + hp: + format: int32 + type: integer + torque: {} + title: EnginePower + type: object io.github.springwolf.examples.kafka.dtos.discriminator.VehicleBase: discriminator: vehicleType title: VehicleBase type: object properties: + enginePower: + $ref: "#/components/schemas/io.github.springwolf.examples.kafka.dtos.discriminator.EnginePower" powerSource: type: string topSpeed: @@ -951,19 +975,49 @@ components: description: Demonstrates the use of discriminator for polymorphic deserialization (not publishable) examples: - - powerSource: string + - batteryCapacity: 0 + chargeTime: 0 + enginePower: + hp: 0 + torque: 0 + powerSource: string topSpeed: 0 vehicleType: string + oneOf: + - $ref: "#/components/schemas/io.github.springwolf.examples.kafka.dtos.discriminator.VehicleElectricPayloadDto" + - $ref: "#/components/schemas/io.github.springwolf.examples.kafka.dtos.discriminator.VehicleGasolinePayloadDto" x-json-schema: $schema: https://json-schema.org/draft-04/schema# description: Demonstrates the use of discriminator for polymorphic deserialization (not publishable) + oneOf: + - allOf: + - {} + - properties: + batteryCapacity: {} + chargeTime: + format: int32 + type: integer + type: object + description: Electric vehicle implementation of VehicleBase + type: object + - allOf: + - {} + - properties: + fuelCapacity: {} + type: object + description: Gasoline vehicle implementation of VehicleBase + type: object properties: + enginePower: + properties: + hp: {} + torque: {} + title: EnginePower + type: object powerSource: type: string - topSpeed: - format: int32 - type: integer + topSpeed: {} vehicleType: {} title: VehicleBase type: object @@ -973,6 +1027,9 @@ components: examples: - batteryCapacity: 0 chargeTime: 0 + enginePower: + hp: 0 + torque: 0 powerSource: string topSpeed: 0 vehicleType: string @@ -991,12 +1048,27 @@ components: allOf: - description: Demonstrates the use of discriminator for polymorphic deserialization (not publishable) + oneOf: + - {} + - allOf: + - {} + - properties: + fuelCapacity: + format: int32 + type: integer + type: object + description: Gasoline vehicle implementation of VehicleBase + type: object properties: + enginePower: + properties: + hp: {} + torque: {} + title: EnginePower + type: object powerSource: type: string - topSpeed: - format: int32 - type: integer + topSpeed: {} vehicleType: {} title: VehicleBase type: object @@ -1010,7 +1082,10 @@ components: type: object description: Gasoline vehicle implementation of VehicleBase examples: - - fuelCapacity: 0 + - enginePower: + hp: 0 + torque: 0 + fuelCapacity: 0 powerSource: string topSpeed: 0 vehicleType: string @@ -1026,12 +1101,28 @@ components: allOf: - description: Demonstrates the use of discriminator for polymorphic deserialization (not publishable) + oneOf: + - allOf: + - {} + - properties: + batteryCapacity: {} + chargeTime: + format: int32 + type: integer + type: object + description: Electric vehicle implementation of VehicleBase + type: object + - {} properties: + enginePower: + properties: + hp: {} + torque: {} + title: EnginePower + type: object powerSource: type: string - topSpeed: - format: int32 - type: integer + topSpeed: {} vehicleType: {} title: VehicleBase type: object @@ -1410,4 +1501,4 @@ operations: kafka: bindingVersion: 0.5.0 messages: - - $ref: "#/channels/yaml-topic/messages/io.github.springwolf.examples.kafka.dtos.YamlPayloadDto" \ No newline at end of file + - $ref: "#/channels/yaml-topic/messages/io.github.springwolf.examples.kafka.dtos.YamlPayloadDto" diff --git a/springwolf-examples/springwolf-kafka-example/src/test/resources/groups/vehicles.json b/springwolf-examples/springwolf-kafka-example/src/test/resources/groups/vehicles.json index d986d88fc..600fe7e9b 100644 --- a/springwolf-examples/springwolf-kafka-example/src/test/resources/groups/vehicles.json +++ b/springwolf-examples/springwolf-kafka-example/src/test/resources/groups/vehicles.json @@ -75,11 +75,46 @@ "type": "object" } }, + "io.github.springwolf.examples.kafka.dtos.discriminator.EnginePower": { + "title": "EnginePower", + "type": "object", + "properties": { + "hp": { + "type": "integer", + "format": "int32" + }, + "torque": { + "type": "integer", + "format": "int32" + } + }, + "examples": [ + { + "hp": 0, + "torque": 0 + } + ], + "x-json-schema": { + "$schema": "https://json-schema.org/draft-04/schema#", + "properties": { + "hp": { + "format": "int32", + "type": "integer" + }, + "torque": { } + }, + "title": "EnginePower", + "type": "object" + } + }, "io.github.springwolf.examples.kafka.dtos.discriminator.VehicleBase": { "discriminator": "vehicleType", "title": "VehicleBase", "type": "object", "properties": { + "enginePower": { + "$ref": "#/components/schemas/io.github.springwolf.examples.kafka.dtos.discriminator.EnginePower" + }, "powerSource": { "type": "string" }, @@ -94,22 +129,73 @@ "description": "Demonstrates the use of discriminator for polymorphic deserialization (not publishable)", "examples": [ { + "batteryCapacity": 0, + "chargeTime": 0, + "enginePower": { + "hp": 0, + "torque": 0 + }, "powerSource": "string", "topSpeed": 0, "vehicleType": "string" } ], + "oneOf": [ + { + "$ref": "#/components/schemas/io.github.springwolf.examples.kafka.dtos.discriminator.VehicleElectricPayloadDto" + }, + { + "$ref": "#/components/schemas/io.github.springwolf.examples.kafka.dtos.discriminator.VehicleGasolinePayloadDto" + } + ], "x-json-schema": { "$schema": "https://json-schema.org/draft-04/schema#", "description": "Demonstrates the use of discriminator for polymorphic deserialization (not publishable)", + "oneOf": [ + { + "allOf": [ + { }, + { + "properties": { + "batteryCapacity": { }, + "chargeTime": { + "format": "int32", + "type": "integer" + } + }, + "type": "object" + } + ], + "description": "Electric vehicle implementation of VehicleBase", + "type": "object" + }, + { + "allOf": [ + { }, + { + "properties": { + "fuelCapacity": { } + }, + "type": "object" + } + ], + "description": "Gasoline vehicle implementation of VehicleBase", + "type": "object" + } + ], "properties": { + "enginePower": { + "properties": { + "hp": { }, + "torque": { } + }, + "title": "EnginePower", + "type": "object" + }, "powerSource": { "type": "string" }, - "topSpeed": { - "format": "int32", - "type": "integer" - }, + "topSpeed": { }, "vehicleType": { } }, "title": "VehicleBase", @@ -156,4 +242,4 @@ ] } } -} \ No newline at end of file +}