|
9 | 9 | import io.swagger.v3.oas.models.OpenAPI; |
10 | 10 | import io.swagger.v3.oas.models.info.Contact; |
11 | 11 | import io.swagger.v3.oas.models.info.Info; |
| 12 | +import io.swagger.v3.oas.models.media.ArraySchema; |
| 13 | +import io.swagger.v3.oas.models.media.Schema; |
12 | 14 | import io.swagger.v3.oas.models.servers.Server; |
13 | 15 | import io.swagger.v3.oas.models.tags.Tag; |
| 16 | +import org.springdoc.core.customizers.OpenApiCustomizer; |
14 | 17 | import org.springdoc.core.properties.SpringDocConfigProperties; |
15 | 18 | import org.springdoc.core.providers.ObjectMapperProvider; |
16 | 19 | import org.springframework.beans.factory.annotation.Value; |
17 | 20 | import org.springframework.context.annotation.Bean; |
18 | 21 | import org.springframework.context.annotation.Configuration; |
19 | 22 |
|
| 23 | +import java.util.LinkedHashMap; |
20 | 24 | import java.util.List; |
| 25 | +import java.util.Map; |
21 | 26 |
|
22 | 27 | @Configuration |
23 | 28 | public class SwaggerConfiguration { |
@@ -82,6 +87,120 @@ public OpenAPI customOpenAPI() { |
82 | 87 | )); |
83 | 88 | } |
84 | 89 |
|
| 90 | + @Bean |
| 91 | + public OpenApiCustomizer netexModelCompatibilityCustomizer() { |
| 92 | + return openApi -> { |
| 93 | + Map<String, Schema> schemas = openApi.getComponents().getSchemas(); |
| 94 | + if (schemas == null) return; |
| 95 | + |
| 96 | + // Fix TariffZoneRefs_RelStructure: rename tariffZoneRef_ back to tariffZoneRef |
| 97 | + Schema tariffZoneRefs = schemas.get("TariffZoneRefs_RelStructure"); |
| 98 | + if (tariffZoneRefs != null && tariffZoneRefs.getProperties() != null) { |
| 99 | + Map<String, Schema> props = tariffZoneRefs.getProperties(); |
| 100 | + if (props.containsKey("tariffZoneRef_")) { |
| 101 | + Schema tariffZoneRefProp = props.remove("tariffZoneRef_"); |
| 102 | + // Restore original schema: array of TariffZoneRef with XML name |
| 103 | + Schema restoredProp = new ArraySchema() |
| 104 | + .items(new Schema<>().$ref("#/components/schemas/TariffZoneRef")); |
| 105 | + restoredProp.xml(new io.swagger.v3.oas.models.media.XML().name("TariffZoneRef")); |
| 106 | + props.put("tariffZoneRef", restoredProp); |
| 107 | + // Re-sort properties alphabetically |
| 108 | + Map<String, Schema> sorted = new LinkedHashMap<>(); |
| 109 | + props.entrySet().stream() |
| 110 | + .sorted(Map.Entry.comparingByKey()) |
| 111 | + .forEach(e -> sorted.put(e.getKey(), e.getValue())); |
| 112 | + tariffZoneRefs.setProperties(sorted); |
| 113 | + // Restore required field |
| 114 | + tariffZoneRefs.setRequired(List.of("tariffZoneRef")); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // Fix Quays_RelStructure: restore quayRefOrQuay items to empty schema |
| 119 | + Schema quaysRel = schemas.get("Quays_RelStructure"); |
| 120 | + if (quaysRel != null && quaysRel.getProperties() != null) { |
| 121 | + Map<String, Schema> props = quaysRel.getProperties(); |
| 122 | + Schema quayProp = props.get("quayRefOrQuay"); |
| 123 | + if (quayProp != null && quayProp.getItems() != null) { |
| 124 | + quayProp.setItems(new Schema<>()); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // Fix ParkingAreas_RelStructure: rename parkingAreaRefOrParkingArea_ back |
| 129 | + renameProperty(schemas, "ParkingAreas_RelStructure", |
| 130 | + "parkingAreaRefOrParkingArea_", "parkingAreaRefOrParkingArea"); |
| 131 | + |
| 132 | + // Fix StopPlacesInFrame_RelStructure: rename stopPlace_ back to stopPlace |
| 133 | + Schema stopPlacesInFrame = schemas.get("StopPlacesInFrame_RelStructure"); |
| 134 | + if (stopPlacesInFrame != null && stopPlacesInFrame.getProperties() != null) { |
| 135 | + Map<String, Schema> spProps = stopPlacesInFrame.getProperties(); |
| 136 | + if (spProps.containsKey("stopPlace_")) { |
| 137 | + spProps.remove("stopPlace_"); |
| 138 | + Schema stopPlaceProp = new ArraySchema() |
| 139 | + .items(new Schema<>().$ref("#/components/schemas/StopPlace")); |
| 140 | + stopPlaceProp.xml(new io.swagger.v3.oas.models.media.XML().name("StopPlace")); |
| 141 | + spProps.put("stopPlace", stopPlaceProp); |
| 142 | + // Re-sort alphabetically |
| 143 | + Map<String, Schema> sorted = new LinkedHashMap<>(); |
| 144 | + spProps.entrySet().stream() |
| 145 | + .sorted(Map.Entry.comparingByKey()) |
| 146 | + .forEach(e -> sorted.put(e.getKey(), e.getValue())); |
| 147 | + stopPlacesInFrame.setProperties(sorted); |
| 148 | + stopPlacesInFrame.setRequired(List.of("stopPlace")); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + // Fix StopPlaceRefs_RelStructure: restore items ref and required |
| 153 | + restoreJaxbElementArrayProp(schemas, "StopPlaceRefs_RelStructure", |
| 154 | + "stopPlaceRef", "StopPlaceRefStructure", "StopPlaceRef"); |
| 155 | + |
| 156 | + // Fix ParkingAreaRefs_RelStructure: restore items ref and required |
| 157 | + restoreJaxbElementArrayProp(schemas, "ParkingAreaRefs_RelStructure", |
| 158 | + "parkingAreaRef", "ParkingAreaRefStructure", "ParkingAreaRef"); |
| 159 | + |
| 160 | + // Restore TariffZoneRef schema (removed in new model, still referenced) |
| 161 | + if (!schemas.containsKey("TariffZoneRef")) { |
| 162 | + schemas.put("TariffZoneRef", schemas.getOrDefault("ZoneRefStructure", new Schema<>())); |
| 163 | + } |
| 164 | + }; |
| 165 | + } |
| 166 | + |
| 167 | + private static void restoreJaxbElementArrayProp(Map<String, Schema> schemas, |
| 168 | + String schemaName, String propName, |
| 169 | + String itemsRef, String xmlName) { |
| 170 | + Schema schema = schemas.get(schemaName); |
| 171 | + if (schema != null && schema.getProperties() != null) { |
| 172 | + Map<String, Schema> props = schema.getProperties(); |
| 173 | + Schema prop = props.get(propName); |
| 174 | + if (prop != null) { |
| 175 | + prop.setItems(new Schema<>().$ref("#/components/schemas/" + itemsRef)); |
| 176 | + prop.xml(new io.swagger.v3.oas.models.media.XML().name(xmlName)); |
| 177 | + schema.setRequired(List.of(propName)); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + private static void renameProperty(Map<String, Schema> schemas, String schemaName, |
| 183 | + String oldPropName, String newPropName) { |
| 184 | + Schema schema = schemas.get(schemaName); |
| 185 | + if (schema != null && schema.getProperties() != null) { |
| 186 | + Map<String, Schema> props = schema.getProperties(); |
| 187 | + if (props.containsKey(oldPropName)) { |
| 188 | + Schema prop = props.remove(oldPropName); |
| 189 | + // Reset items to empty schema (unwrapped JAXBElements) |
| 190 | + if (prop.getItems() != null) { |
| 191 | + prop.setItems(new Schema<>()); |
| 192 | + } |
| 193 | + props.put(newPropName, prop); |
| 194 | + // Re-sort alphabetically |
| 195 | + Map<String, Schema> sorted = new LinkedHashMap<>(); |
| 196 | + props.entrySet().stream() |
| 197 | + .sorted(Map.Entry.comparingByKey()) |
| 198 | + .forEach(e -> sorted.put(e.getKey(), e.getValue())); |
| 199 | + schema.setProperties(sorted); |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + |
85 | 204 | @Bean |
86 | 205 | public ObjectMapperProvider springDocObjectMapperProvider() { |
87 | 206 | return new ObjectMapperProvider(new SpringDocConfigProperties()) { |
|
0 commit comments