Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: create discriminator example #771

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions springwolf-examples/e2e/tests/publishing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ function testPublishingEveryChannelItem() {
payload === "YamlPayloadDto" || // Unable to create correct yaml payload
payload === "MonetaryAmount" || // Issue with either MonetaryAmount of ModelConverters
payload === "Message" || // Unable to instantiate ExamplePayloadProtobufDto$Message class
payload === "VehicleBase" || // Unable to publish abstract class for discriminator demo
channelName === "example-topic-routing-key" // Publishing through amqp exchange is not supported, see GH-366
) {
return; // skip
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.springwolf.examples.kafka.consumers;

import io.github.springwolf.examples.kafka.dtos.discriminator.VehicleBase;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
public class DiscriminatorConsumer {

@KafkaListener(topics = "vehicle-topic")
public void receiveExamplePayload(VehicleBase payload) {
log.info("Received new message in vehicle-topic: {}", payload.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.springwolf.examples.kafka.dtos.discriminator;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import io.swagger.v3.oas.annotations.media.DiscriminatorMapping;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "vehicleType")
@JsonSubTypes({
@JsonSubTypes.Type(value = VehicleElectricPayloadDto.class, name = "VehicleElectricPayloadDto"),
@JsonSubTypes.Type(value = VehicleGasolinePayloadDto.class, name = "VehicleGasolinePayloadDto"),
})
@Schema(
subTypes = {VehicleElectricPayloadDto.class},
discriminatorProperty = "vehicleType",
discriminatorMapping = {
@DiscriminatorMapping(value = "VehicleElectricPayloadDto", schema = VehicleElectricPayloadDto.class),
@DiscriminatorMapping(value = "VehicleGasolinePayloadDto", schema = VehicleGasolinePayloadDto.class),
},
description = "Demonstrates the use of discriminator for polymorphic deserialization (not publishable)")
@Data
@AllArgsConstructor
@NoArgsConstructor
public abstract class VehicleBase {
private String vehicleType; // added for discriminator

private String powerSource;
private int topSpeed;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.springwolf.examples.kafka.dtos.discriminator;

import com.fasterxml.jackson.annotation.JsonTypeName;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@JsonTypeName("VehicleElectricPayloadDto")
@Schema(
description = "Electric vehicle implementation of VehicleBase"

// Alternative, when you do not want to add the discriminator property vehicleType to VehicleBase
// allOf = {VehicleBase.class}
)
@NoArgsConstructor
@Data
@EqualsAndHashCode(callSuper = true)
@AllArgsConstructor
public class VehicleElectricPayloadDto extends VehicleBase {
private int batteryCapacity;
private int chargeTime;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.springwolf.examples.kafka.dtos.discriminator;

import com.fasterxml.jackson.annotation.JsonTypeName;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@JsonTypeName("VehicleGasolinePayloadDto")
@Schema(
description = "Gasoline vehicle implementation of VehicleBase"

// Alternative, when you do not want to add the discriminator property vehicleType to VehicleBase
// allOf = {VehicleBase.class}
)
@NoArgsConstructor
@Data
@EqualsAndHashCode(callSuper = true)
@AllArgsConstructor
public class VehicleGasolinePayloadDto extends VehicleBase {
private int fuelCapacity;
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void testContextWithApplicationProperties() {

@Test
void testAllChannelsAreFound() {
assertThat(asyncApiService.getAsyncAPI().getChannels()).hasSize(11);
assertThat(asyncApiService.getAsyncAPI().getChannels()).hasSize(12);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@
}
]
},
"vehicle-topic": {
"messages": {
"io.github.springwolf.examples.kafka.dtos.discriminator.VehicleBase": {
"$ref": "#/components/messages/io.github.springwolf.examples.kafka.dtos.discriminator.VehicleBase"
}
},
"bindings": {
"kafka": {
"bindingVersion": "0.5.0"
}
}
},
"xml-topic": {
"messages": {
"io.github.springwolf.examples.kafka.dtos.XmlPayloadDto": {
Expand Down Expand Up @@ -605,6 +617,39 @@
"type": "object"
}
},
"SpringKafkaDefaultHeaders-VehicleBase": {
"type": "object",
"properties": {
"__TypeId__": {
"type": "string",
"description": "Spring Type Id Header",
"enum": [
"io.github.springwolf.examples.kafka.dtos.discriminator.VehicleBase"
],
"examples": [
"io.github.springwolf.examples.kafka.dtos.discriminator.VehicleBase"
]
}
},
"examples": [
{
"__TypeId__": "io.github.springwolf.examples.kafka.dtos.discriminator.VehicleBase"
}
],
"x-json-schema": {
"$schema": "https://json-schema.org/draft-04/schema#",
"properties": {
"__TypeId__": {
"description": "Spring Type Id Header",
"enum": [
"io.github.springwolf.examples.kafka.dtos.discriminator.VehicleBase"
],
"type": "string"
}
},
"type": "object"
}
},
"SpringKafkaDefaultHeaders-XmlPayloadDto": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -1136,6 +1181,157 @@
"type": "string"
}
},
"io.github.springwolf.examples.kafka.dtos.discriminator.VehicleBase": {
"discriminator": "vehicleType",
"type": "object",
"properties": {
"powerSource": {
"type": "string"
},
"topSpeed": {
"type": "integer",
"format": "int32"
},
"vehicleType": {
"type": "string"
}
},
"description": "Demonstrates the use of discriminator for polymorphic deserialization (not publishable)",
"examples": [
{
"powerSource": "string",
"topSpeed": 0,
"vehicleType": "string"
}
],
"x-json-schema": {
"$schema": "https://json-schema.org/draft-04/schema#",
"description": "Demonstrates the use of discriminator for polymorphic deserialization (not publishable)",
"properties": {
"powerSource": {
"type": "string"
},
"topSpeed": {
"format": "int32",
"type": "integer"
},
"vehicleType": { }
},
"type": "object"
}
},
"io.github.springwolf.examples.kafka.dtos.discriminator.VehicleElectricPayloadDto": {
"type": "object",
"description": "Electric vehicle implementation of VehicleBase",
"examples": [
{
"batteryCapacity": 0,
"chargeTime": 0,
"powerSource": "string",
"topSpeed": 0,
"vehicleType": "string"
}
],
"allOf": [
{
"$ref": "#/components/schemas/io.github.springwolf.examples.kafka.dtos.discriminator.VehicleBase"
},
{
"type": "object",
"properties": {
"batteryCapacity": {
"type": "integer",
"format": "int32"
},
"chargeTime": {
"type": "integer",
"format": "int32"
}
}
}
],
"x-json-schema": {
"$schema": "https://json-schema.org/draft-04/schema#",
"allOf": [
{
"description": "Demonstrates the use of discriminator for polymorphic deserialization (not publishable)",
"properties": {
"powerSource": {
"type": "string"
},
"topSpeed": {
"format": "int32",
"type": "integer"
},
"vehicleType": { }
},
"type": "object"
},
{
"properties": {
"batteryCapacity": { },
"chargeTime": { }
},
"type": "object"
}
],
"description": "Electric vehicle implementation of VehicleBase",
"type": "object"
}
},
"io.github.springwolf.examples.kafka.dtos.discriminator.VehicleGasolinePayloadDto": {
"type": "object",
"description": "Gasoline vehicle implementation of VehicleBase",
"examples": [
{
"fuelCapacity": 0,
"powerSource": "string",
"topSpeed": 0,
"vehicleType": "string"
}
],
"allOf": [
{
"$ref": "#/components/schemas/io.github.springwolf.examples.kafka.dtos.discriminator.VehicleBase"
},
{
"type": "object",
"properties": {
"fuelCapacity": {
"type": "integer",
"format": "int32"
}
}
}
],
"x-json-schema": {
"$schema": "https://json-schema.org/draft-04/schema#",
"allOf": [
{
"description": "Demonstrates the use of discriminator for polymorphic deserialization (not publishable)",
"properties": {
"powerSource": {
"type": "string"
},
"topSpeed": {
"format": "int32",
"type": "integer"
},
"vehicleType": { }
},
"type": "object"
},
{
"properties": {
"fuelCapacity": { }
},
"type": "object"
}
],
"description": "Gasoline vehicle implementation of VehicleBase",
"type": "object"
}
},
"java.lang.Number": {
"type": "number",
"examples": [
Expand Down Expand Up @@ -1370,6 +1566,24 @@
}
}
},
"io.github.springwolf.examples.kafka.dtos.discriminator.VehicleBase": {
"headers": {
"$ref": "#/components/schemas/SpringKafkaDefaultHeaders-VehicleBase"
},
"payload": {
"schemaFormat": "application/vnd.aai.asyncapi+json;version=3.0.0",
"schema": {
"$ref": "#/components/schemas/io.github.springwolf.examples.kafka.dtos.discriminator.VehicleBase"
}
},
"name": "io.github.springwolf.examples.kafka.dtos.discriminator.VehicleBase",
"title": "VehicleBase",
"bindings": {
"kafka": {
"bindingVersion": "0.5.0"
}
}
},
"java.lang.Number": {
"headers": {
"$ref": "#/components/schemas/SpringKafkaDefaultHeaders-Integer"
Expand Down Expand Up @@ -1635,6 +1849,22 @@
}
]
},
"vehicle-topic_receive_receiveExamplePayload": {
"action": "receive",
"channel": {
"$ref": "#/channels/vehicle-topic"
},
"bindings": {
"kafka": {
"bindingVersion": "0.5.0"
}
},
"messages": [
{
"$ref": "#/channels/vehicle-topic/messages/io.github.springwolf.examples.kafka.dtos.discriminator.VehicleBase"
}
]
},
"xml-topic_receive_receiveExamplePayload": {
"action": "receive",
"channel": {
Expand Down