@@ -7,11 +7,18 @@ package bundler
7
7
import (
8
8
"errors"
9
9
"fmt"
10
+ "strings"
11
+ "sync"
10
12
13
+ "github.com/davecgh/go-spew/spew"
11
14
"github.com/pb33f/libopenapi"
12
15
"github.com/pb33f/libopenapi/datamodel"
13
- v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
16
+ highV3 "github.com/pb33f/libopenapi/datamodel/high/v3"
17
+ "github.com/pb33f/libopenapi/datamodel/low"
18
+ "github.com/pb33f/libopenapi/datamodel/low/base"
19
+ lowV3 "github.com/pb33f/libopenapi/datamodel/low/v3"
14
20
"github.com/pb33f/libopenapi/index"
21
+ "gopkg.in/yaml.v3"
15
22
)
16
23
17
24
// ErrInvalidModel is returned when the model is not usable.
@@ -66,11 +73,11 @@ func BundleBytes(bytes []byte, configuration *datamodel.DocumentConfiguration, o
66
73
// document will be a valid OpenAPI specification, containing no references.
67
74
//
68
75
// Circular references will not be resolved and will be skipped.
69
- func BundleDocument (model * v3 .Document ) ([]byte , error ) {
76
+ func BundleDocument (model * highV3 .Document ) ([]byte , error ) {
70
77
return bundle (model , BundleOptions {RelativeRefHandling : RefHandlingInline })
71
78
}
72
79
73
- func bundle (model * v3 .Document , opts BundleOptions ) (_ []byte , err error ) {
80
+ func bundle (model * highV3 .Document , opts BundleOptions ) (_ []byte , err error ) {
74
81
rolodex := model .Rolodex
75
82
76
83
idx := rolodex .GetRootIndex ()
@@ -99,12 +106,59 @@ func bundle(model *v3.Document, opts BundleOptions) (_ []byte, err error) {
99
106
if err := bundleRefTarget (sequenced , mappedReference , bundledComponents , opts ); err != nil {
100
107
return nil , err
101
108
}
109
+
110
+ model , err = composeDocument (model , bundledComponents )
111
+ if err != nil {
112
+ return nil , err
113
+ }
102
114
}
103
115
}
104
116
105
117
return model .Render ()
106
118
}
107
119
120
+ func composeDocument (model * highV3.Document , comps map [string ]* index.ReferenceNode ) (* highV3.Document , error ) {
121
+ lowModel := model .GoLow ()
122
+
123
+ components := lowModel .Components
124
+
125
+ for def , component := range comps {
126
+ defParts := strings .Split (def , "/" )
127
+ // TODO: use constant from low model labels
128
+ if len (defParts ) != 4 || defParts [1 ] != lowV3 .ComponentsLabel {
129
+ return nil , fmt .Errorf ("unsupported component section: %s" , def )
130
+ }
131
+ spew .Dump (component )
132
+
133
+ switch defParts [2 ] {
134
+ case "schemas" :
135
+ key := low.KeyReference [string ]{
136
+ Value : defParts [3 ],
137
+ KeyNode : & yaml.Node {
138
+ Kind : yaml .ScalarNode ,
139
+ Style : yaml .TaggedStyle ,
140
+ Tag : "!!str" ,
141
+ Value : defParts [3 ],
142
+ },
143
+ }
144
+ value := low.ValueReference [* base.SchemaProxy ]{
145
+ Reference : low.Reference {},
146
+ Value : & base.SchemaProxy {
147
+ Reference : low.Reference {},
148
+ NodeMap : & low.NodeMap {Nodes : & sync.Map {}},
149
+ },
150
+ ValueNode : & yaml.Node {},
151
+ }
152
+ components .Value .Schemas .Value .Set (key , value )
153
+
154
+ default :
155
+ return nil , fmt .Errorf ("unsupported component type: %s" , defParts [2 ])
156
+ }
157
+ }
158
+
159
+ return nil , nil
160
+ }
161
+
108
162
func bundleRefTarget (ref , mappedRef * index.ReferenceNode , bundledComponents map [string ]* index.ReferenceNode , opts BundleOptions ) error {
109
163
idx := ref .Index
110
164
if mappedRef == nil {
0 commit comments