|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +package io.github.springwolf.core.asyncapi.schemas; |
| 3 | + |
| 4 | +import io.github.springwolf.asyncapi.v3.model.components.ComponentSchema; |
| 5 | +import io.github.springwolf.core.asyncapi.components.examples.SchemaWalkerProvider; |
| 6 | +import io.github.springwolf.core.asyncapi.components.examples.walkers.DefaultSchemaWalker; |
| 7 | +import io.github.springwolf.core.asyncapi.components.examples.walkers.json.ExampleJsonValueGenerator; |
| 8 | +import io.github.springwolf.core.asyncapi.components.postprocessors.AvroSchemaPostProcessor; |
| 9 | +import io.github.springwolf.core.asyncapi.components.postprocessors.ExampleGeneratorPostProcessor; |
| 10 | +import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties; |
| 11 | +import org.apache.avro.Schema; |
| 12 | +import org.apache.avro.specific.SpecificData; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.springframework.http.MediaType; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +import static java.util.stream.Collectors.toMap; |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | + |
| 22 | +/** |
| 23 | + * In GH-1627, it was found that it is important to finish a post-processor on all schemas first, |
| 24 | + * before moving on to the next post-processor to generate examples |
| 25 | + */ |
| 26 | +class SwaggerSchemaServicePostProcessorExecutionTest { |
| 27 | + |
| 28 | + private final SpringwolfConfigProperties springwolfConfigProperties = new SpringwolfConfigProperties(); |
| 29 | + |
| 30 | + private final SwaggerSchemaService schemaService = new SwaggerSchemaService( |
| 31 | + springwolfConfigProperties, |
| 32 | + List.of( |
| 33 | + new AvroSchemaPostProcessor(), |
| 34 | + new ExampleGeneratorPostProcessor(new SchemaWalkerProvider( |
| 35 | + List.of(new DefaultSchemaWalker<>(new ExampleJsonValueGenerator()))))), |
| 36 | + new SwaggerSchemaMapper(springwolfConfigProperties), |
| 37 | + new ModelConvertersProvider(springwolfConfigProperties, List.of())); |
| 38 | + |
| 39 | + @Test |
| 40 | + void happyPath_AvroGamma_having_multiple_AvroDelta() { |
| 41 | + // when |
| 42 | + var extractedSchema = |
| 43 | + schemaService.resolveSchema(AvroTestClasses.AvroGamma.class, MediaType.APPLICATION_JSON_VALUE); |
| 44 | + |
| 45 | + // then |
| 46 | + var examplesBySchema = extractedSchema.referencedSchemas().entrySet().stream() |
| 47 | + .collect(toMap(Map.Entry::getKey, this::getExamples)); |
| 48 | + assertThat(examplesBySchema) |
| 49 | + .containsEntry( |
| 50 | + AvroTestClasses.AvroGamma.class.getCanonicalName(), |
| 51 | + "[{\"avroDeltas\":[{\"name\":\"string\"}],\"name\":\"string\"}]") |
| 52 | + .containsEntry(AvroTestClasses.AvroDelta.class.getCanonicalName(), "[{\"name\":\"string\"}]"); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void unhappyPath_AvroAlpha_having_multiple_AvroBeta() { |
| 57 | + // when |
| 58 | + var extractedSchema = |
| 59 | + schemaService.resolveSchema(AvroTestClasses.AvroAlpha.class, MediaType.APPLICATION_JSON_VALUE); |
| 60 | + |
| 61 | + // then |
| 62 | + var examplesBySchema = extractedSchema.referencedSchemas().entrySet().stream() |
| 63 | + .collect(toMap(Map.Entry::getKey, this::getExamples)); |
| 64 | + assertThat(examplesBySchema) |
| 65 | + .containsEntry( |
| 66 | + AvroTestClasses.AvroAlpha.class.getCanonicalName(), |
| 67 | + "[{\"avroBetas\":[{\"name\":\"string\"}],\"name\":\"string\"}]") |
| 68 | + .containsEntry(AvroTestClasses.AvroBeta.class.getCanonicalName(), "[{\"name\":\"string\"}]"); |
| 69 | + } |
| 70 | + |
| 71 | + private String getExamples(Map.Entry<String, ComponentSchema> it) { |
| 72 | + return String.valueOf(it.getValue().getSchema().getExamples()); |
| 73 | + } |
| 74 | + |
| 75 | + private static class AvroTestClasses { |
| 76 | + static class AvroAlpha { |
| 77 | + |
| 78 | + public String getName() { |
| 79 | + return "alpha"; |
| 80 | + } |
| 81 | + |
| 82 | + public List<AvroBeta> getAvroBetas() { |
| 83 | + return List.of(new AvroBeta()); |
| 84 | + } |
| 85 | + |
| 86 | + public SpecificData getSpecificData() { |
| 87 | + return new SpecificData(); |
| 88 | + } |
| 89 | + |
| 90 | + public Schema getSchema() { |
| 91 | + return null; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + static class AvroBeta { |
| 96 | + |
| 97 | + public String getName() { |
| 98 | + return "beta"; |
| 99 | + } |
| 100 | + |
| 101 | + public SpecificData getSpecificData() { |
| 102 | + return new SpecificData(); |
| 103 | + } |
| 104 | + |
| 105 | + public Schema getSchema() { |
| 106 | + return null; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + static class AvroDelta { |
| 111 | + |
| 112 | + public String getName() { |
| 113 | + return "delta"; |
| 114 | + } |
| 115 | + |
| 116 | + public SpecificData getSpecificData() { |
| 117 | + return new SpecificData(); |
| 118 | + } |
| 119 | + |
| 120 | + public Schema getSchema() { |
| 121 | + return null; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + static class AvroGamma { |
| 126 | + |
| 127 | + public String getName() { |
| 128 | + return "gamma"; |
| 129 | + } |
| 130 | + |
| 131 | + public List<AvroDelta> getAvroDeltas() { |
| 132 | + return List.of(new AvroDelta()); |
| 133 | + } |
| 134 | + |
| 135 | + public SpecificData getSpecificData() { |
| 136 | + return new SpecificData(); |
| 137 | + } |
| 138 | + |
| 139 | + public Schema getSchema() { |
| 140 | + return null; |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments