-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExternalRefProcessor.java
1028 lines (883 loc) · 34.3 KB
/
ExternalRefProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package io.swagger.v3.parser.processors;
import static io.swagger.v3.parser.util.RefUtils.computeDefinitionName;
import static io.swagger.v3.parser.util.RefUtils.computeRefFormat;
import static io.swagger.v3.parser.util.RefUtils.getExternalPath;
import static io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.callbacks.Callback;
import io.swagger.v3.oas.models.examples.Example;
import io.swagger.v3.oas.models.headers.Header;
import io.swagger.v3.oas.models.links.Link;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.Discriminator;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.parser.ResolverCache;
import io.swagger.v3.parser.models.RefFormat;
import io.swagger.v3.parser.models.RefType;
import java.io.File;
import java.net.URI;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
public final class ExternalRefProcessor {
private static final org.slf4j.Logger LOGGER =
LoggerFactory.getLogger(ExternalRefProcessor.class);
private final ResolverCache cache;
private final OpenAPI openAPI;
public ExternalRefProcessor(ResolverCache cache, OpenAPI openAPI) {
this.cache = cache;
this.openAPI = openAPI;
}
private String finalNameRec(
Map<String, Schema> schemas,
String possiblyConflictingDefinitionName,
Schema newScema,
int iteration) {
String tryName =
iteration == 0
? possiblyConflictingDefinitionName
: possiblyConflictingDefinitionName + "_" + iteration;
Schema existingModel = schemas.get(tryName);
if (existingModel != null) {
// BEGIN REFERENCE FORK:
// if (existingModel.get$ref() != null) {
if (existingModel.get$ref() != null || newScema.get$ref() != null) {
// END REFERENCE FORK.
// use the new model
existingModel = null;
} else if (!newScema.equals(existingModel)) {
LOGGER.debug("A model for " + existingModel + " already exists");
return finalNameRec(schemas, possiblyConflictingDefinitionName, newScema, ++iteration);
}
}
return tryName;
}
public String processRefToExternalSchema(String $ref, RefFormat refFormat) {
String renamedRef = cache.getRenamedRef($ref);
if (renamedRef != null) {
return renamedRef;
}
final Schema schema = cache.loadRef($ref, refFormat, Schema.class);
if (schema == null) {
// stop! There's a problem. retain the original ref
LOGGER.warn(
"unable to load model reference from `"
+ $ref
+ "`. It may not be available "
+ "or the reference isn't a valid model schema");
return $ref;
}
String newRef;
if (openAPI.getComponents() == null) {
openAPI.setComponents(new Components());
}
Map<String, Schema> schemas = openAPI.getComponents().getSchemas();
if (schemas == null) {
schemas = new LinkedHashMap<>();
}
final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
newRef = finalNameRec(schemas, possiblyConflictingDefinitionName, schema, 0);
cache.putRenamedRef($ref, newRef);
Schema existingModel = schemas.get(newRef);
if (existingModel != null && existingModel.get$ref() != null) {
// use the new model
existingModel = null;
}
if (existingModel == null) {
// don't overwrite existing model reference
openAPI.getComponents().addSchemas(newRef, schema);
cache.addReferencedKey(newRef);
String file = $ref.split("#/")[0];
if (schema.get$ref() != null) {
RefFormat ref = computeRefFormat(schema.get$ref());
if (isAnExternalRefFormat(ref)) {
String schemaFullRef = schema.get$ref();
String parent = file.substring(0, file.lastIndexOf(File.separatorChar));
if (!parent.isEmpty()) {
schemaFullRef = Paths.get(parent, schemaFullRef).normalize().toString();
}
schema.set$ref(processRefToExternalSchema(schemaFullRef, ref));
} else {
processRefToExternalSchema(file + schema.get$ref(), RefFormat.RELATIVE);
}
}
if (schema instanceof ComposedSchema) {
ComposedSchema composedSchema = (ComposedSchema) schema;
if (composedSchema.getAllOf() != null) {
for (Schema item : composedSchema.getAllOf()) {
if (item.get$ref() != null) {
processRefSchema(item, file);
} else if (item.getProperties() != null) {
processProperties(item.getProperties(), file);
}
}
}
if (composedSchema.getOneOf() != null) {
for (Schema item : composedSchema.getOneOf()) {
if (item.get$ref() != null) {
if (item.get$ref() != null) {
processRefSchema(item, file);
}
}
}
}
if (composedSchema.getAnyOf() != null) {
for (Schema item : composedSchema.getAnyOf()) {
if (item.get$ref() != null) {
if (item.get$ref() != null) {
processRefSchema(item, file);
}
}
}
}
}
// Loop the properties and recursively call this method;
Map<String, Schema> subProps = schema.getProperties();
processProperties(subProps, file);
processDiscriminator(schema.getDiscriminator(), file);
if (schema.getAdditionalProperties() != null
&& schema.getAdditionalProperties() instanceof Schema) {
Schema additionalProperty = (Schema) schema.getAdditionalProperties();
if (additionalProperty.get$ref() != null) {
processRefSchema(additionalProperty, file);
} else if (additionalProperty instanceof ArraySchema) {
ArraySchema arrayProp = (ArraySchema) additionalProperty;
if (arrayProp.getItems() != null
&& arrayProp.getItems().get$ref() != null
&& StringUtils.isNotBlank(arrayProp.get$ref())) {
processRefSchema(arrayProp.getItems(), file);
}
} else if (additionalProperty.getAdditionalProperties() != null
&& additionalProperty.getAdditionalProperties() instanceof Schema) {
Schema mapProp = (Schema) additionalProperty.getAdditionalProperties();
if (mapProp.get$ref() != null) {
processRefSchema(mapProp, file);
} else if (mapProp.getAdditionalProperties() instanceof ArraySchema
&& ((ArraySchema) mapProp).getItems() != null
&& ((ArraySchema) mapProp).getItems().get$ref() != null
&& StringUtils.isNotBlank(((ArraySchema) mapProp).getItems().get$ref())) {
processRefSchema(((ArraySchema) mapProp).getItems(), file);
}
}
}
if (schema instanceof ArraySchema && ((ArraySchema) schema).getItems() != null) {
ArraySchema arraySchema = (ArraySchema) schema;
if (StringUtils.isNotBlank(arraySchema.getItems().get$ref())) {
processRefSchema(((ArraySchema) schema).getItems(), file);
} else {
processProperties(arraySchema.getItems().getProperties(), file);
}
}
}
return newRef;
}
private void processSchema(Schema property, String file) {
if (property != null) {
if (StringUtils.isNotBlank(property.get$ref())) {
processRefSchema(property, file);
}
if (property.getProperties() != null) {
processProperties(property.getProperties(), file);
}
if (property instanceof ArraySchema) {
processSchema(((ArraySchema) property).getItems(), file);
}
if (property.getAdditionalProperties() instanceof Schema) {
processSchema(((Schema) property.getAdditionalProperties()), file);
}
if (property instanceof ComposedSchema) {
ComposedSchema composed = (ComposedSchema) property;
processProperties(composed.getAllOf(), file);
processProperties(composed.getAnyOf(), file);
processProperties(composed.getOneOf(), file);
}
}
}
private void processProperties(Collection<Schema> properties, String file) {
if (properties != null) {
for (Schema property : properties) {
processSchema(property, file);
}
}
}
private void processProperties(Map<String, Schema> properties, String file) {
if (properties != null) {
processProperties(properties.values(), file);
}
}
public PathItem processRefToExternalPathItem(String $ref, RefFormat refFormat) {
final PathItem pathItem = cache.loadRef($ref, refFormat, PathItem.class);
String newRef;
Map<String, PathItem> paths = openAPI.getPaths();
if (paths == null) {
paths = new LinkedHashMap<>();
}
final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
PathItem existingPathItem = paths.get(possiblyConflictingDefinitionName);
if (existingPathItem != null) {
LOGGER.debug("A model for " + existingPathItem + " already exists");
if (existingPathItem.get$ref() != null) {
// use the new model
existingPathItem = null;
}
}
newRef = possiblyConflictingDefinitionName;
cache.putRenamedRef($ref, newRef);
if (pathItem != null) {
if (pathItem.readOperationsMap() != null) {
final Map<PathItem.HttpMethod, Operation> operationMap = pathItem.readOperationsMap();
for (PathItem.HttpMethod httpMethod : operationMap.keySet()) {
Operation operation = operationMap.get(httpMethod);
if (operation.getResponses() != null) {
final Map<String, ApiResponse> responses = operation.getResponses();
if (responses != null) {
for (String responseCode : responses.keySet()) {
ApiResponse response = responses.get(responseCode);
if (response != null) {
Schema schema = null;
if (response.getContent() != null) {
Map<String, MediaType> content = response.getContent();
for (String mediaName : content.keySet()) {
MediaType mediaType = content.get(mediaName);
if (mediaType.getSchema() != null) {
schema = mediaType.getSchema();
if (schema != null) {
processRefSchemaObject(mediaType.getSchema(), $ref);
}
if (mediaType.getExamples() != null) {
processRefExamples(mediaType.getExamples(), $ref);
}
}
}
}
if (response.getLinks() != null) {
processRefLinks(response.getLinks(), $ref);
}
}
}
}
}
if (operation.getRequestBody() != null) {
RequestBody body = operation.getRequestBody();
if (body.getContent() != null) {
Schema schema;
Map<String, MediaType> content = body.getContent();
for (String mediaName : content.keySet()) {
MediaType mediaType = content.get(mediaName);
if (mediaType.getSchema() != null) {
schema = mediaType.getSchema();
if (schema != null) {
processRefSchemaObject(mediaType.getSchema(), $ref);
}
}
}
}
}
final List<Parameter> parameters = operation.getParameters();
if (parameters != null) {
parameters
.stream()
.filter(parameter -> parameter.getSchema() != null)
.forEach(parameter -> this.processRefSchemaObject(parameter.getSchema(), $ref));
}
}
}
}
return pathItem;
}
private void processDiscriminator(Discriminator d, String file) {
if (d != null && d.getMapping() != null) {
processDiscriminatorMapping(d.getMapping(), file);
}
}
private void processDiscriminatorMapping(Map<String, String> mapping, String file) {
for (String key : mapping.keySet()) {
String ref = mapping.get(key);
Schema subtype = new Schema().$ref(ref);
processSchema(subtype, file);
mapping.put(key, subtype.get$ref());
}
}
public String processRefToExternalResponse(String $ref, RefFormat refFormat) {
String renamedRef = cache.getRenamedRef($ref);
if (renamedRef != null) {
return renamedRef;
}
final ApiResponse response = cache.loadRef($ref, refFormat, ApiResponse.class);
String newRef;
if (openAPI.getComponents() == null) {
openAPI.setComponents(new Components());
}
Map<String, ApiResponse> responses = openAPI.getComponents().getResponses();
if (responses == null) {
responses = new LinkedHashMap<>();
}
final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
ApiResponse existingResponse = responses.get(possiblyConflictingDefinitionName);
if (existingResponse != null) {
LOGGER.debug("A model for " + existingResponse + " already exists");
if (existingResponse.get$ref() != null) {
// use the new model
existingResponse = null;
}
}
newRef = possiblyConflictingDefinitionName;
cache.putRenamedRef($ref, newRef);
if (response != null) {
if (response.getContent() != null) {
processRefContent(response.getContent(), $ref);
}
if (response.getHeaders() != null) {
processRefHeaders(response.getHeaders(), $ref);
}
if (response.getLinks() != null) {
processRefLinks(response.getLinks(), $ref);
}
}
return newRef;
}
public String processRefToExternalRequestBody(String $ref, RefFormat refFormat) {
String renamedRef = cache.getRenamedRef($ref);
if (renamedRef != null) {
return renamedRef;
}
final RequestBody body = cache.loadRef($ref, refFormat, RequestBody.class);
if (body == null) {
// stop! There's a problem. retain the original ref
LOGGER.warn(
"unable to load model reference from `"
+ $ref
+ "`. It may not be available "
+ "or the reference isn't a valid model schema");
return $ref;
}
String newRef;
if (openAPI.getComponents() == null) {
openAPI.setComponents(new Components());
}
Map<String, RequestBody> bodies = openAPI.getComponents().getRequestBodies();
if (bodies == null) {
bodies = new LinkedHashMap<>();
}
final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
RequestBody existingBody = bodies.get(possiblyConflictingDefinitionName);
if (existingBody != null) {
LOGGER.debug("A model for " + existingBody + " already exists");
if (existingBody.get$ref() != null) {
// use the new model
existingBody = null;
}
}
newRef = possiblyConflictingDefinitionName;
cache.putRenamedRef($ref, newRef);
if (existingBody == null) {
// don't overwrite existing model reference
openAPI.getComponents().addRequestBodies(newRef, body);
cache.addReferencedKey(newRef);
String file = $ref.split("#/")[0];
if (body.get$ref() != null) {
RefFormat format = computeRefFormat(body.get$ref());
if (isAnExternalRefFormat(format)) {
body.set$ref(processRefToExternalRequestBody(body.get$ref(), format));
} else {
processRefToExternalRequestBody(file + body.get$ref(), RefFormat.RELATIVE);
}
} else if (body.getContent() != null) {
processRefContent(body.getContent(), $ref);
}
}
return newRef;
}
public String processRefToExternalHeader(String $ref, RefFormat refFormat) {
String renamedRef = cache.getRenamedRef($ref);
if (renamedRef != null) {
return renamedRef;
}
final Header header = cache.loadRef($ref, refFormat, Header.class);
if (header == null) {
// stop! There's a problem. retain the original ref
LOGGER.warn(
"unable to load model reference from `"
+ $ref
+ "`. It may not be available "
+ "or the reference isn't a valid model schema");
return $ref;
}
String newRef;
if (openAPI.getComponents() == null) {
openAPI.setComponents(new Components());
}
Map<String, Header> headers = openAPI.getComponents().getHeaders();
if (headers == null) {
headers = new LinkedHashMap<>();
}
final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
Header existingHeader = headers.get(possiblyConflictingDefinitionName);
if (existingHeader != null) {
LOGGER.debug("A model for " + existingHeader + " already exists");
if (existingHeader.get$ref() != null) {
// use the new model
existingHeader = null;
}
}
newRef = possiblyConflictingDefinitionName;
cache.putRenamedRef($ref, newRef);
if (existingHeader == null) {
// don't overwrite existing model reference
openAPI.getComponents().addHeaders(newRef, header);
cache.addReferencedKey(newRef);
String file = $ref.split("#/")[0];
if (header.get$ref() != null) {
RefFormat format = computeRefFormat(header.get$ref());
if (isAnExternalRefFormat(format)) {
header.set$ref(processRefToExternalHeader(header.get$ref(), format));
} else {
processRefToExternalHeader(file + header.get$ref(), RefFormat.RELATIVE);
}
}
}
if (header != null) {
if (header.getContent() != null) {
processRefContent(header.getContent(), $ref);
}
if (header.getSchema() != null) {
processRefSchemaObject(header.getSchema(), $ref);
}
}
return newRef;
}
public String processRefToExternalSecurityScheme(String $ref, RefFormat refFormat) {
String renamedRef = cache.getRenamedRef($ref);
if (renamedRef != null) {
return renamedRef;
}
final SecurityScheme securityScheme = cache.loadRef($ref, refFormat, SecurityScheme.class);
if (securityScheme == null) {
// stop! There's a problem. retain the original ref
LOGGER.warn(
"unable to load model reference from `"
+ $ref
+ "`. It may not be available "
+ "or the reference isn't a valid model schema");
return $ref;
}
String newRef;
if (openAPI.getComponents() == null) {
openAPI.setComponents(new Components());
}
Map<String, SecurityScheme> securitySchemeMap = openAPI.getComponents().getSecuritySchemes();
if (securitySchemeMap == null) {
securitySchemeMap = new LinkedHashMap<>();
}
final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
SecurityScheme existingSecurityScheme =
securitySchemeMap.get(possiblyConflictingDefinitionName);
if (existingSecurityScheme != null) {
LOGGER.debug("A model for " + existingSecurityScheme + " already exists");
if (existingSecurityScheme.get$ref() != null) {
// use the new model
existingSecurityScheme = null;
}
}
newRef = possiblyConflictingDefinitionName;
cache.putRenamedRef($ref, newRef);
if (existingSecurityScheme == null) {
// don't overwrite existing model reference
openAPI.getComponents().addSecuritySchemes(newRef, securityScheme);
cache.addReferencedKey(newRef);
String file = $ref.split("#/")[0];
if (securityScheme.get$ref() != null) {
RefFormat format = computeRefFormat(securityScheme.get$ref());
if (isAnExternalRefFormat(format)) {
securityScheme.set$ref(
processRefToExternalSecurityScheme(securityScheme.get$ref(), format));
} else {
processRefToExternalSecurityScheme(file + securityScheme.get$ref(), RefFormat.RELATIVE);
}
}
}
return newRef;
}
public String processRefToExternalLink(String $ref, RefFormat refFormat) {
String renamedRef = cache.getRenamedRef($ref);
if (renamedRef != null) {
return renamedRef;
}
final Link link = cache.loadRef($ref, refFormat, Link.class);
if (link == null) {
// stop! There's a problem. retain the original ref
LOGGER.warn(
"unable to load model reference from `"
+ $ref
+ "`. It may not be available "
+ "or the reference isn't a valid model schema");
return $ref;
}
String newRef;
if (openAPI.getComponents() == null) {
openAPI.setComponents(new Components());
}
Map<String, Link> links = openAPI.getComponents().getLinks();
if (links == null) {
links = new LinkedHashMap<>();
}
final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
Link existingLink = links.get(possiblyConflictingDefinitionName);
if (existingLink != null) {
LOGGER.debug("A model for " + existingLink + " already exists");
if (existingLink.get$ref() != null) {
// use the new model
existingLink = null;
}
}
newRef = possiblyConflictingDefinitionName;
cache.putRenamedRef($ref, newRef);
if (existingLink == null) {
// don't overwrite existing model reference
openAPI.getComponents().addLinks(newRef, link);
cache.addReferencedKey(newRef);
String file = $ref.split("#/")[0];
if (link.get$ref() != null) {
RefFormat format = computeRefFormat(link.get$ref());
if (isAnExternalRefFormat(format)) {
link.set$ref(processRefToExternalLink(link.get$ref(), format));
} else {
processRefToExternalLink(file + link.get$ref(), RefFormat.RELATIVE);
}
}
}
return newRef;
}
public String processRefToExternalExample(String $ref, RefFormat refFormat) {
String renamedRef = cache.getRenamedRef($ref);
if (renamedRef != null) {
return renamedRef;
}
final Example example = cache.loadRef($ref, refFormat, Example.class);
if (example == null) {
// stop! There's a problem. retain the original ref
LOGGER.warn(
"unable to load model reference from `"
+ $ref
+ "`. It may not be available "
+ "or the reference isn't a valid model schema");
return $ref;
}
String newRef;
if (openAPI.getComponents() == null) {
openAPI.setComponents(new Components());
}
Map<String, Example> examples = openAPI.getComponents().getExamples();
if (examples == null) {
examples = new LinkedHashMap<>();
}
final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
Example existingExample = examples.get(possiblyConflictingDefinitionName);
if (existingExample != null) {
LOGGER.debug("A model for " + existingExample + " already exists");
if (existingExample.get$ref() != null) {
// use the new model
existingExample = null;
}
}
newRef = possiblyConflictingDefinitionName;
cache.putRenamedRef($ref, newRef);
if (existingExample == null) {
// don't overwrite existing model reference
openAPI.getComponents().addExamples(newRef, example);
cache.addReferencedKey(newRef);
String file = $ref.split("#/")[0];
if (example.get$ref() != null) {
RefFormat format = computeRefFormat(example.get$ref());
if (isAnExternalRefFormat(format)) {
example.set$ref(processRefToExternalExample(example.get$ref(), format));
} else {
processRefToExternalExample(file + example.get$ref(), RefFormat.RELATIVE);
}
}
}
return newRef;
}
public String processRefToExternalParameter(String $ref, RefFormat refFormat) {
String renamedRef = cache.getRenamedRef($ref);
if (renamedRef != null) {
return renamedRef;
}
final Parameter parameter = cache.loadRef($ref, refFormat, Parameter.class);
if (parameter == null) {
// stop! There's a problem. retain the original ref
LOGGER.warn(
"unable to load model reference from `"
+ $ref
+ "`. It may not be available "
+ "or the reference isn't a valid model schema");
return $ref;
}
String newRef;
if (openAPI.getComponents() == null) {
openAPI.setComponents(new Components());
}
Map<String, Parameter> parameters = openAPI.getComponents().getParameters();
if (parameters == null) {
parameters = new LinkedHashMap<>();
}
final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
Parameter existingParameters = parameters.get(possiblyConflictingDefinitionName);
if (existingParameters != null) {
LOGGER.debug("A model for " + existingParameters + " already exists");
if (existingParameters.get$ref() != null) {
// use the new model
existingParameters = null;
}
}
newRef = possiblyConflictingDefinitionName;
cache.putRenamedRef($ref, newRef);
if (existingParameters == null) {
// don't overwrite existing model reference
openAPI.getComponents().addParameters(newRef, parameter);
cache.addReferencedKey(newRef);
String file = $ref.split("#/")[0];
if (parameter.get$ref() != null) {
RefFormat format = computeRefFormat(parameter.get$ref());
if (isAnExternalRefFormat(format)) {
parameter.set$ref(processRefToExternalParameter(parameter.get$ref(), format));
} else {
processRefToExternalParameter(file + parameter.get$ref(), RefFormat.RELATIVE);
}
}
}
if (parameter != null) {
if (parameter.getContent() != null) {
processRefContent(parameter.getContent(), $ref);
}
if (parameter.getSchema() != null) {
processRefSchemaObject(parameter.getSchema(), $ref);
}
}
return newRef;
}
public String processRefToExternalCallback(String $ref, RefFormat refFormat) {
String renamedRef = cache.getRenamedRef($ref);
if (renamedRef != null) {
return renamedRef;
}
final Callback callback = cache.loadRef($ref, refFormat, Callback.class);
if (callback == null) {
// stop! There's a problem. retain the original ref
LOGGER.warn(
"unable to load model reference from `"
+ $ref
+ "`. It may not be available "
+ "or the reference isn't a valid model schema");
return $ref;
}
String newRef;
if (openAPI.getComponents() == null) {
openAPI.setComponents(new Components());
}
Map<String, Callback> callbacks = openAPI.getComponents().getCallbacks();
if (callbacks == null) {
callbacks = new LinkedHashMap<>();
}
final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
Callback existingCallback = callbacks.get(possiblyConflictingDefinitionName);
if (existingCallback != null) {
LOGGER.debug("A model for " + existingCallback + " already exists");
if (existingCallback.get$ref() != null) {
// use the new model
existingCallback = null;
}
}
newRef = possiblyConflictingDefinitionName;
cache.putRenamedRef($ref, newRef);
if (existingCallback == null) {
// don't overwrite existing model reference
openAPI.getComponents().addCallbacks(newRef, callback);
cache.addReferencedKey(newRef);
String file = $ref.split("#/")[0];
if (callback.get$ref() != null) {
if (callback.get$ref() != null) {
RefFormat format = computeRefFormat(callback.get$ref());
if (isAnExternalRefFormat(format)) {
callback.set$ref(processRefToExternalCallback(callback.get$ref(), format));
} else {
processRefToExternalCallback(file + callback.get$ref(), RefFormat.RELATIVE);
}
}
}
}
return newRef;
}
private void processRefContent(Map<String, MediaType> content, String $ref) {
for (MediaType mediaType : content.values()) {
if (mediaType.getSchema() != null) {
processRefSchemaObject(mediaType.getSchema(), $ref);
}
if (mediaType.getExamples() != null) {
processRefExamples(mediaType.getExamples(), $ref);
}
}
}
private void processRefExamples(Map<String, Example> examples, String $ref) {
String file = $ref.split("#/")[0];
for (Example example : examples.values()) {
if (example.get$ref() != null) {
RefFormat ref = computeRefFormat(example.get$ref());
if (isAnExternalRefFormat(ref)) {
processRefExample(example, $ref);
} else {
processRefToExternalExample(file + example.get$ref(), RefFormat.RELATIVE);
}
}
}
}
private void processRefExample(Example example, String externalFile) {
RefFormat format = computeRefFormat(example.get$ref());
if (!isAnExternalRefFormat(format)) {
example.set$ref(
RefType.SCHEMAS.getInternalPrefix()
+ processRefToExternalSchema(externalFile + example.get$ref(), RefFormat.RELATIVE));
return;
}
String $ref = example.get$ref();
String subRefExternalPath = getExternalPath(example.get$ref()).orElse(null);
if (format.equals(RefFormat.RELATIVE) && !Objects.equals(subRefExternalPath, externalFile)) {
$ref = join(externalFile, example.get$ref());
example.set$ref($ref);
} else {
processRefToExternalExample($ref, format);
}
}
private void processRefSchemaObject(Schema schema, String $ref) {
String file = $ref.split("#/")[0];
if (schema.get$ref() != null) {
RefFormat ref = computeRefFormat(schema.get$ref());
if (isAnExternalRefFormat(ref)) {
processRefSchema(schema, $ref);
} else {
processRefToExternalSchema(file + schema.get$ref(), RefFormat.RELATIVE);
}
} else {
processSchema(schema, file);
}
}
private void processRefHeaders(Map<String, Header> headers, String $ref) {
String file = $ref.split("#/")[0];
for (Header header : headers.values()) {
if (header.get$ref() != null) {
RefFormat ref = computeRefFormat(header.get$ref());
if (isAnExternalRefFormat(ref)) {
processRefHeader(header, $ref);
} else {
processRefToExternalHeader(file + header.get$ref(), RefFormat.RELATIVE);
}
}
}
}
private void processRefLinks(Map<String, Link> links, String $ref) {
String file = $ref.split("#/")[0];
for (Link link : links.values()) {
if (link.get$ref() != null) {
RefFormat ref = computeRefFormat(link.get$ref());
if (isAnExternalRefFormat(ref)) {
processRefLink(link, $ref);
} else {
processRefToExternalLink(file + link.get$ref(), RefFormat.RELATIVE);
}
}
}
}
private void processRefSchema(Schema subRef, String externalFile) {
RefFormat format = computeRefFormat(subRef.get$ref());
if (!isAnExternalRefFormat(format)) {
subRef.set$ref(
RefType.SCHEMAS.getInternalPrefix()
+ processRefToExternalSchema(externalFile + subRef.get$ref(), RefFormat.RELATIVE));
return;
}
String $ref = subRef.get$ref();
String subRefExternalPath = getExternalPath(subRef.get$ref()).orElse(null);
if (format.equals(RefFormat.RELATIVE) && !Objects.equals(subRefExternalPath, externalFile)) {
$ref = constructRef(subRef, externalFile);
subRef.set$ref($ref);
} else {
processRefToExternalSchema($ref, format);
}
}
protected String constructRef(Schema refProperty, String rootLocation) {
String ref = refProperty.get$ref();
return join(rootLocation, ref);
}
private void processRefHeader(Header subRef, String externalFile) {
RefFormat format = computeRefFormat(subRef.get$ref());
if (!isAnExternalRefFormat(format)) {
subRef.set$ref(
RefType.SCHEMAS.getInternalPrefix()
+ processRefToExternalSchema(externalFile + subRef.get$ref(), RefFormat.RELATIVE));
return;
}
String $ref = subRef.get$ref();
String subRefExternalPath = getExternalPath(subRef.get$ref()).orElse(null);
if (format.equals(RefFormat.RELATIVE) && !Objects.equals(subRefExternalPath, externalFile)) {
$ref = join(externalFile, subRef.get$ref());
subRef.set$ref($ref);
} else {
processRefToExternalHeader($ref, format);
}
}
private void processRefLink(Link subRef, String externalFile) {
RefFormat format = computeRefFormat(subRef.get$ref());
if (!isAnExternalRefFormat(format)) {
subRef.set$ref(
RefType.SCHEMAS.getInternalPrefix()
+ processRefToExternalSchema(externalFile + subRef.get$ref(), RefFormat.RELATIVE));
return;
}
String $ref = subRef.get$ref();
String subRefExternalPath = getExternalPath(subRef.get$ref()).orElse(null);
if (format.equals(RefFormat.RELATIVE) && !Objects.equals(subRefExternalPath, externalFile)) {
$ref = join(externalFile, subRef.get$ref());
subRef.set$ref($ref);
} else {
processRefToExternalLink($ref, format);
}
}