Skip to content

Commit 77f5090

Browse files
committed
WIP
1 parent 80d86bd commit 77f5090

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

bundler/bundler.go

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@ package bundler
77
import (
88
"errors"
99
"fmt"
10+
"strings"
11+
"sync"
1012

13+
"github.com/davecgh/go-spew/spew"
1114
"github.com/pb33f/libopenapi"
1215
"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"
1420
"github.com/pb33f/libopenapi/index"
21+
"gopkg.in/yaml.v3"
1522
)
1623

1724
// ErrInvalidModel is returned when the model is not usable.
@@ -66,11 +73,11 @@ func BundleBytes(bytes []byte, configuration *datamodel.DocumentConfiguration, o
6673
// document will be a valid OpenAPI specification, containing no references.
6774
//
6875
// 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) {
7077
return bundle(model, BundleOptions{RelativeRefHandling: RefHandlingInline})
7178
}
7279

73-
func bundle(model *v3.Document, opts BundleOptions) (_ []byte, err error) {
80+
func bundle(model *highV3.Document, opts BundleOptions) (_ []byte, err error) {
7481
rolodex := model.Rolodex
7582

7683
idx := rolodex.GetRootIndex()
@@ -99,12 +106,59 @@ func bundle(model *v3.Document, opts BundleOptions) (_ []byte, err error) {
99106
if err := bundleRefTarget(sequenced, mappedReference, bundledComponents, opts); err != nil {
100107
return nil, err
101108
}
109+
110+
model, err = composeDocument(model, bundledComponents)
111+
if err != nil {
112+
return nil, err
113+
}
102114
}
103115
}
104116

105117
return model.Render()
106118
}
107119

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+
108162
func bundleRefTarget(ref, mappedRef *index.ReferenceNode, bundledComponents map[string]*index.ReferenceNode, opts BundleOptions) error {
109163
idx := ref.Index
110164
if mappedRef == nil {

test_specs/minimal_remote_refs/openapi.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,27 @@ paths:
2929
responses:
3030
"200":
3131
$ref: ./schemas/components.openapi.yaml#/components/responses/ListAccounts
32+
/api/v1/example:
33+
get:
34+
summary: TODO
35+
description: TODO
36+
tags:
37+
- Account
38+
operationId: placeholder
39+
responses:
40+
"200":
41+
description: >
42+
Foo
43+
content:
44+
application/json:
45+
schema:
3246
components:
3347
securitySchemes:
3448
BearerAuth:
3549
type: http
3650
scheme: bearer
51+
schemas:
52+
Foobar:
53+
type: string
54+
description: >
55+
Something nice to say.

0 commit comments

Comments
 (0)