@@ -24,6 +24,9 @@ public class OpenApiBundler {
2424 private final DocumentStore documents ;
2525 private final Bucket root ;
2626 private final URI rootDocumentUri ;
27+ private final Object rootDocument ;
28+ private final OpenApiVersion version ;
29+
2730
2831 private final Map <String , @ Nullable Object > schemas = new LinkedHashMap <> ();
2932 private final Map <String , @ Nullable Object > responses = new LinkedHashMap <> ();
@@ -34,20 +37,20 @@ public class OpenApiBundler {
3437 private final Map <String , @ Nullable Object > securitySchemes = new LinkedHashMap <> ();
3538 private final Map <String , @ Nullable Object > links = new LinkedHashMap <> ();
3639 private final Map <String , @ Nullable Object > callbacks = new LinkedHashMap <> ();
37- private final Map <String , @ Nullable Object > paths = new LinkedHashMap <> ();
40+ private final Map <String , @ Nullable Object > pathItems = new LinkedHashMap <> ();
3841
3942 public OpenApiBundler (Context context , DocumentStore documents , Bucket root ) {
4043 this .context = context ;
4144 this .documents = documents .copy ();
4245 this .root = root ;
4346 this .rootDocumentUri = root .getScope ().getDocumentUri ();
47+ this .rootDocument = documents .get (rootDocumentUri );
48+ this .version = OpenApiVersionParser .parseVersion (rootDocument );
4449 }
4550
46- public Map <String , @ Nullable Object > bundle () {
47- URI documentUri = root .getScope ().getDocumentUri ();
48- Object document = documents .get (documentUri );
4951
50- Bucket bundled = createBucket (root .getScope (), document , root .getLocation ());
52+ public Map <String , @ Nullable Object > bundle () {
53+ Bucket bundled = createBucket (root .getScope (), rootDocument , root .getLocation ());
5154 walkBucket (bundled );
5255
5356 Map <String , @ Nullable Object > rawValues = bundled .getRawValues ();
@@ -80,7 +83,7 @@ private void mergeMaps (Map<String, @Nullable Object> components) {
8083 mergeMap (components , "securitySchemes" , securitySchemes );
8184 mergeMap (components , "links" , links );
8285 mergeMap (components , "callbacks" , callbacks );
83- mergeMap (components , "paths " , paths );
86+ mergeMap (components , "pathItems " , pathItems );
8487 }
8588
8689 private void mergeMap (
@@ -160,6 +163,9 @@ private boolean isExternalDocument (URI documentUri) {
160163
161164 JsonPointer refPointer = reference .getPointer ();
162165 String refName = refPointer .tail ();
166+ if (refName .isEmpty ()) {
167+ refName = documentUri .getPath ();
168+ }
163169 RawValue refValue = getRefValue (documentBucket , refPointer );
164170 Bucket refBucket = getRefBucket (refPointer , refValue );
165171
@@ -192,7 +198,11 @@ private boolean isExternalDocument (URI documentUri) {
192198 bundleCallback (bucketValues , refName , refValue );
193199
194200 } else if (isPathRef (location ) && external ) {
195- result = bundlePath30 (bucketValues , refValue );
201+ if (version == OpenApiVersion .V30 ) {
202+ result = bundlePathItem30 (bucketValues , refValue );
203+ } else {
204+ bundlePathItem (bucketValues , refName , refValue );
205+ }
196206 }
197207
198208 // walk unseen ref
@@ -259,7 +269,12 @@ private void bundleCallback (Map<String, @Nullable Object> rawValues, String ref
259269 rawValues .put (Keywords .REF , createRefPointer ("callbacks" , refName ));
260270 }
261271
262- private Runnable bundlePath30 (Map <String , @ Nullable Object > rawValues , RawValue refValue ) {
272+ private void bundlePathItem (Map <String , @ Nullable Object > rawValues , String refName , RawValue refValue ) {
273+ pathItems .put (refName , refValue .getValue ());
274+ rawValues .put (Keywords .REF , createRefPointer ("pathItems" , refName ));
275+ }
276+
277+ private Runnable bundlePathItem30 (Map <String , @ Nullable Object > rawValues , RawValue refValue ) {
263278 // OpenAPI 3.0 has no /components/paths => inline it
264279
265280 Map <String , @ Nullable Object > replacement = asObject (refValue .getValue ());
@@ -312,7 +327,7 @@ private Bucket createBucket(Scope scope, @Nullable Object source, JsonPointer lo
312327 }
313328
314329 private String createRefPointer (String type , String refName ) {
315- return String .format ("#/components/%s/%s" , type , refName );
330+ return String .format ("#/components/%s/%s" , type , JsonPointerSupport . encode ( refName ) );
316331 }
317332
318333 private void walkSchema (Scope currentScope , Object value , JsonPointer location ) {
0 commit comments