Skip to content

Commit

Permalink
fix jsontypeinfo schema example generation
Browse files Browse the repository at this point in the history
  • Loading branch information
SheheryarAamir committed Jul 21, 2024
1 parent b61078d commit eae01be
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 9 deletions.
2 changes: 2 additions & 0 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,6 @@ ext {
swaggerVersion = '2.2.22'

testcontainersVersion = '1.20.0'

springdoc = '2.6.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ private Optional<T> buildExample(String name, Schema schema, Map<String, Schema>
return buildExample(name, resolvedSchema.get(), definitions, visited);
}

Optional<T> jsonTypeInfoSchema = buildFromJsonTypeInfo(name, schema, definitions, visited);
if (jsonTypeInfoSchema.isPresent()) {
return jsonTypeInfoSchema;
}

String type = schema.getType();
return switch (type) {
case "array" -> buildArrayExample(schema, definitions, visited);
Expand Down Expand Up @@ -207,6 +212,29 @@ private String getFirstEnumValue(Schema schema) {
return null;
}

private Optional<T> buildFromJsonTypeInfo(
String name, Schema schema, Map<String, Schema> definitions, Set<Schema> visited) {
if (visited.contains(schema)) {
return exampleValueGenerator.createEmptyObjectExample();
}
visited.add(schema);

final List<Schema> schemasAllOf = schema.getAllOf();
final List<Schema> schemasAnyOf = schema.getAnyOf();
final List<Schema> schemasOneOf = schema.getOneOf();
if (!CollectionUtils.isEmpty(schemasAllOf)) {
return buildFromObjectSchemaWithAllOf(name, schemasAllOf, definitions, visited);
} else if (!CollectionUtils.isEmpty(schemasAnyOf)) {
return buildExample(name, schemasAnyOf.get(0), definitions, visited);
} else if (!CollectionUtils.isEmpty(schemasOneOf)) {
return buildExample(name, schemasOneOf.get(0), definitions, visited);
}

visited.remove(schema);

return Optional.empty();
}

private Optional<T> buildFromObjectSchema(
String name, Schema schema, Map<String, Schema> definitions, Set<Schema> visited) {
if (visited.contains(schema)) {
Expand All @@ -218,17 +246,8 @@ private Optional<T> buildFromObjectSchema(

final Map<String, Schema> properties = schema.getProperties();
final Object additionalProperties = schema.getAdditionalProperties();
final List<Schema> schemasAllOf = schema.getAllOf();
final List<Schema> schemasAnyOf = schema.getAnyOf();
final List<Schema> schemasOneOf = schema.getOneOf();
if (properties != null) {
exampleValue = buildFromObjectSchemaWithProperties(name, properties, definitions, visited);
} else if (!CollectionUtils.isEmpty(schemasAllOf)) {
exampleValue = buildFromObjectSchemaWithAllOf(name, schemasAllOf, definitions, visited);
} else if (!CollectionUtils.isEmpty(schemasAnyOf)) {
exampleValue = buildExample(name, schemasAnyOf.get(0), definitions, visited);
} else if (!CollectionUtils.isEmpty(schemasOneOf)) {
exampleValue = buildExample(name, schemasOneOf.get(0), definitions, visited);
} else if (schema instanceof MapSchema && additionalProperties instanceof Schema<?>) {
exampleValue = buildMapExample(name, (Schema) additionalProperties, definitions, visited);
} else {
Expand Down
4 changes: 4 additions & 0 deletions springwolf-examples/springwolf-jms-example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ dependencies {
implementation "org.springframework:spring-jms"
implementation "org.springframework.boot:spring-boot"
implementation "org.springframework.boot:spring-boot-autoconfigure"
implementation "com.fasterxml.jackson.core:jackson-annotations:${jacksonVersion}"


runtimeOnly "org.springframework.boot:spring-boot-starter-web"
runtimeOnly "org.springframework.boot:spring-boot-starter-artemis"

Expand All @@ -47,6 +50,7 @@ dependencies {

testImplementation "org.testcontainers:testcontainers:${testcontainersVersion}"
testImplementation "org.testcontainers:junit-jupiter:${testcontainersVersion}"

}

docker {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import io.github.springwolf.examples.jms.dtos.AnotherPayloadDto;
import io.github.springwolf.examples.jms.dtos.ExamplePayloadDto;
import io.github.springwolf.examples.jms.dtos.JsonTypeInfoPayloadDto;
import io.github.springwolf.examples.jms.producers.AnotherProducer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -30,4 +31,9 @@ public void receiveExamplePayload(ExamplePayloadDto payload) {
public void receiveAnotherPayload(AnotherPayloadDto payload) {
log.info("Received new message in another-queue: {}", payload.toString());
}

@JmsListener(destination = "json-type-info-queue")
public void receiveJsonTypeInfoPayload(JsonTypeInfoPayloadDto payload) {
log.info("Received new message in json-type-info-queue: {}", payload.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.springwolf.examples.jms.dtos;

import com.fasterxml.jackson.annotation.JsonTypeName;
import io.swagger.v3.oas.annotations.media.Schema;

import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.NOT_REQUIRED;

@JsonTypeName("exampleOne")
@Schema(description = "Json Type Info Example One model")
public record JsonTypeInfoExampleOne(
@Schema(description = "Foo field", example = "fooValue", requiredMode = NOT_REQUIRED) String foo)
implements JsonTypeInfoInterface {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.springwolf.examples.jms.dtos;

import com.fasterxml.jackson.annotation.JsonTypeName;
import io.swagger.v3.oas.annotations.media.Schema;

import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.NOT_REQUIRED;

@JsonTypeName("exampleTwo")
@Schema(description = "Json Type Info Example Two model")
public record JsonTypeInfoExampleTwo(
@Schema(description = "Boo field", example = "booValue", requiredMode = NOT_REQUIRED) String boo)
implements JsonTypeInfoInterface {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.springwolf.examples.jms.dtos;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import io.swagger.v3.oas.annotations.media.Schema;

@JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = JsonTypeInfoExampleOne.class, name = "exampleOne"),
@JsonSubTypes.Type(value = JsonTypeInfoExampleTwo.class, name = "exampleTwo")
})
@Schema(oneOf = {JsonTypeInfoExampleOne.class, JsonTypeInfoExampleTwo.class})
public interface JsonTypeInfoInterface {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.springwolf.examples.jms.dtos;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "Json Type Info Payload Dto model")
public record JsonTypeInfoPayloadDto(
@Schema(description = "", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
JsonTypeInfoInterface jsonTypeInfoInterface) {}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@
"bindingVersion": "0.0.1"
}
}
},
"json-type-info-queue": {
"address": "json-type-info-queue",
"messages": {
"io.github.springwolf.examples.jms.dtos.JsonTypeInfoPayloadDto": {
"$ref": "#/components/messages/io.github.springwolf.examples.jms.dtos.JsonTypeInfoPayloadDto"
}
},
"bindings": {
"jms": {
"bindingVersion": "0.0.1"
}
}
}
},
"components": {
Expand Down Expand Up @@ -128,6 +141,104 @@
"someEnum",
"someString"
]
},
"io.github.springwolf.examples.jms.dtos.JsonTypeInfoExampleOne": {
"type": "object",
"description": "Json Type Info Example One model",
"examples": [
{
"foo": "fooValue",
"type": "string"
}
],
"allOf": [
{
"$ref": "#/components/schemas/io.github.springwolf.examples.jms.dtos.JsonTypeInfoInterface"
},
{
"type": "object",
"properties": {
"foo": {
"type": "string",
"description": "Foo field",
"examples": [
"fooValue"
]
}
}
}
]
},
"io.github.springwolf.examples.jms.dtos.JsonTypeInfoExampleTwo": {
"type": "object",
"description": "Json Type Info Example Two model",
"examples": [
{
"boo": "booValue",
"type": "string"
}
],
"allOf": [
{
"$ref": "#/components/schemas/io.github.springwolf.examples.jms.dtos.JsonTypeInfoInterface"
},
{
"type": "object",
"properties": {
"boo": {
"type": "string",
"description": "Boo field",
"examples": [
"booValue"
]
}
}
}
]
},
"io.github.springwolf.examples.jms.dtos.JsonTypeInfoInterface": {
"discriminator": "type",
"type": "object",
"properties": {
"type": {
"type": "string"
}
},
"examples": [
{
"foo": "fooValue",
"type": "string"
}
],
"required": [
"type"
],
"oneOf": [
{
"$ref": "#/components/schemas/io.github.springwolf.examples.jms.dtos.JsonTypeInfoExampleOne"
},
{
"$ref": "#/components/schemas/io.github.springwolf.examples.jms.dtos.JsonTypeInfoExampleTwo"
}
]
},
"io.github.springwolf.examples.jms.dtos.JsonTypeInfoPayloadDto": {
"title": "JsonTypeInfoPayloadDto",
"type": "object",
"properties": {
"jsonTypeInfoInterface": {
"$ref": "#/components/schemas/io.github.springwolf.examples.jms.dtos.JsonTypeInfoInterface"
}
},
"description": "Json Type Info Payload Dto model",
"examples": [
{
"jsonTypeInfoInterface": {
"foo": "fooValue",
"type": "string"
}
}
]
}
},
"messages": {
Expand Down Expand Up @@ -167,6 +278,24 @@
"bindingVersion": "0.0.1"
}
}
},
"io.github.springwolf.examples.jms.dtos.JsonTypeInfoPayloadDto": {
"headers": {
"$ref": "#/components/schemas/HeadersNotDocumented"
},
"payload": {
"schemaFormat": "application/vnd.aai.asyncapi+json;version=3.0.0",
"schema": {
"$ref": "#/components/schemas/io.github.springwolf.examples.jms.dtos.JsonTypeInfoPayloadDto"
}
},
"name": "io.github.springwolf.examples.jms.dtos.JsonTypeInfoPayloadDto",
"title": "JsonTypeInfoPayloadDto",
"bindings": {
"jms": {
"bindingVersion": "0.0.1"
}
}
}
}
},
Expand Down Expand Up @@ -219,6 +348,20 @@
"$ref": "#/channels/example-queue/messages/io.github.springwolf.examples.jms.dtos.ExamplePayloadDto"
}
]
},
"json-type-info-queue_receive_receiveJsonTypeInfoPayload": {
"action": "receive",
"channel": {
"$ref": "#/channels/json-type-info-queue"
},
"bindings": {
"jms": { }
},
"messages": [
{
"$ref": "#/channels/json-type-info-queue/messages/io.github.springwolf.examples.jms.dtos.JsonTypeInfoPayloadDto"
}
]
}
}
}

0 comments on commit eae01be

Please sign in to comment.