|
20 | 20 | import io.swagger.v3.parser.ResolverCache; |
21 | 21 | import io.swagger.v3.parser.models.RefFormat; |
22 | 22 | import io.swagger.v3.parser.models.RefType; |
| 23 | +import io.swagger.v3.parser.util.RefUtils; |
| 24 | + |
23 | 25 | import org.apache.commons.lang3.StringUtils; |
24 | 26 | import org.slf4j.LoggerFactory; |
25 | 27 |
|
26 | 28 | import java.net.URI; |
| 29 | +import java.util.Collection; |
27 | 30 | import java.util.LinkedHashMap; |
28 | 31 | import java.util.Map; |
| 32 | +import java.util.Objects; |
29 | 33 |
|
30 | 34 | import static io.swagger.v3.parser.util.RefUtils.computeDefinitionName; |
31 | 35 | import static io.swagger.v3.parser.util.RefUtils.computeRefFormat; |
| 36 | +import static io.swagger.v3.parser.util.RefUtils.getExternalPath; |
32 | 37 | import static io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat; |
33 | 38 |
|
34 | 39 | public final class ExternalRefProcessor { |
@@ -177,37 +182,40 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) { |
177 | 182 | return newRef; |
178 | 183 | } |
179 | 184 |
|
180 | | - private void processProperties(Map<String,Schema> subProps, String file) { |
181 | | - if (subProps != null) { |
182 | | - for (Map.Entry<String, Schema> prop : subProps.entrySet()) { |
183 | | - if (prop.getValue().get$ref() != null) { |
184 | | - processRefSchema(prop.getValue(), file); |
185 | | - } else if (prop.getValue() instanceof ArraySchema) { |
186 | | - ArraySchema arrayProp = (ArraySchema) prop.getValue(); |
187 | | - if (arrayProp.getItems() != null && arrayProp.getItems().get$ref() != null && |
188 | | - StringUtils.isNotBlank(arrayProp.getItems().get$ref())) { |
189 | | - processRefSchema(arrayProp.getItems(), file); |
190 | | - } |
191 | | - if (arrayProp.getItems() != null && arrayProp.getItems().getProperties() != null ) { |
192 | | - processProperties(arrayProp.getItems().getProperties(), file); |
193 | | - } |
194 | | - } else if (prop.getValue().getAdditionalProperties() != null && prop.getValue().getAdditionalProperties() instanceof Schema) { |
195 | | - Schema mapProp = (Schema) prop.getValue().getAdditionalProperties(); |
196 | | - if (mapProp.get$ref() != null) { |
197 | | - processRefSchema(mapProp, file); |
198 | | - } else if (mapProp.getAdditionalProperties() instanceof ArraySchema && |
199 | | - ((ArraySchema) mapProp.getAdditionalProperties()).getItems()!= null && |
200 | | - ((ArraySchema) mapProp.getAdditionalProperties()).getItems().get$ref() != null |
201 | | - && StringUtils.isNotBlank(((ArraySchema) mapProp.getAdditionalProperties()).getItems().get$ref())) { |
202 | | - processRefSchema(((ArraySchema) mapProp.getAdditionalProperties()).getItems(), file); |
203 | | - } |
204 | | - }else if (prop.getValue() instanceof ObjectSchema){ |
205 | | - ObjectSchema objProp = (ObjectSchema) prop.getValue(); |
206 | | - if(objProp.getProperties() != null ){ |
207 | | - processProperties(objProp.getProperties(),file); |
208 | | - } |
209 | | - } |
| 185 | + private void processProperty(Schema property, String file) { |
| 186 | + if (property != null) { |
| 187 | + if (StringUtils.isNotBlank(property.get$ref())) { |
| 188 | + processRefSchema(property, file); |
210 | 189 | } |
| 190 | + if (property.getProperties() != null) { |
| 191 | + processProperties(property.getProperties(), file); |
| 192 | + } |
| 193 | + if (property instanceof ArraySchema) { |
| 194 | + processProperty(((ArraySchema) property).getItems(), file); |
| 195 | + } |
| 196 | + if (property.getAdditionalProperties() instanceof Schema) { |
| 197 | + processProperty(((Schema) property.getAdditionalProperties()), file); |
| 198 | + } |
| 199 | + if (property instanceof ComposedSchema) { |
| 200 | + ComposedSchema composed = (ComposedSchema) property; |
| 201 | + processProperties(composed.getAllOf(), file); |
| 202 | + processProperties(composed.getAnyOf(), file); |
| 203 | + processProperties(composed.getOneOf(), file); |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + private void processProperties(Collection<Schema> properties, String file) { |
| 209 | + if (properties != null) { |
| 210 | + for (Schema property : properties) { |
| 211 | + processProperty(property, file); |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + private void processProperties(Map<String, Schema> properties, String file) { |
| 217 | + if (properties != null) { |
| 218 | + processProperties(properties.values(), file); |
211 | 219 | } |
212 | 220 | } |
213 | 221 |
|
@@ -685,8 +693,10 @@ private void processRefSchema(Schema subRef, String externalFile) { |
685 | 693 | return; |
686 | 694 | } |
687 | 695 | String $ref = subRef.get$ref(); |
| 696 | + String subRefExternalPath = getExternalPath(subRef.get$ref()) |
| 697 | + .orElse(null); |
688 | 698 |
|
689 | | - if (format.equals(RefFormat.RELATIVE)) { |
| 699 | + if (format.equals(RefFormat.RELATIVE) && !Objects.equals(subRefExternalPath, externalFile)) { |
690 | 700 | $ref = constructRef(subRef, externalFile); |
691 | 701 | subRef.set$ref($ref); |
692 | 702 | }else { |
|
0 commit comments