Skip to content

Commit 94ad09f

Browse files
committed
Merge branch 'padiazg-feature-addroute-n-buildstream'
2 parents 0973396 + 2eefb9f commit 94ad09f

File tree

18 files changed

+575
-5
lines changed

18 files changed

+575
-5
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
#internal/dist/openapi.yaml
44
testing_out.yaml
55
./testing_out.yaml
6-
internal/dist/openapi.yaml
6+
internal/dist/openapi.yaml
7+
.vscode

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ other parts of the system for that matter.
119119
To run examples, and checkout hosted documentation via Swagger UI, issue the following command:
120120
121121
```sh
122-
$ go run ./examples/*.go
122+
$ go run ./examples/file_output/*.go
123+
# or uncomment line 40 and comment line 38 in internal/dist/index.html before running:
124+
$ go run ./examples/stream_output/*.go
123125
```
124126
125127
And navigate to `http://localhost:3005/docs/api/` in case that you didn't change anything before running the example

build.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func getPathFromFirstElement(cbs []ConfigBuilder) string {
3838
func (o *OAS) BuildDocs(conf ...ConfigBuilder) error {
3939
o.initCallStackForRoutes()
4040

41-
yml, err := marshalToYAML(o)
41+
yml, err := o.marshalToYAML()
4242
if err != nil {
4343
return fmt.Errorf("marshaling issue occurred: %w", err)
4444
}
@@ -51,7 +51,23 @@ func (o *OAS) BuildDocs(conf ...ConfigBuilder) error {
5151
return nil
5252
}
5353

54-
func marshalToYAML(oas *OAS) ([]byte, error) {
54+
// BuildStream marshals the OAS struct to YAML and writes it to a stream.
55+
//
56+
// Returns an error if there is any.
57+
func (o *OAS) BuildStream(w io.Writer) error {
58+
yml, err := o.marshalToYAML()
59+
if err != nil {
60+
return fmt.Errorf("marshaling issue occurred: %w", err)
61+
}
62+
63+
err = writeAndFlush(yml, w)
64+
if err != nil {
65+
return fmt.Errorf("writing issue occurred: %w", err)
66+
}
67+
return nil
68+
}
69+
70+
func (oas *OAS) marshalToYAML() ([]byte, error) {
5571
transformedOAS := oas.transformToHybridOAS()
5672

5773
yml, err := yaml.Marshal(transformedOAS)

build_test.go

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package docs
22

3-
import "testing"
3+
import (
4+
"bytes"
5+
"testing"
6+
)
47

58
func TestUnitBuild(t *testing.T) {
69
t.Parallel()
@@ -109,3 +112,124 @@ func TestUnitGetPathFromFirstElem(t *testing.T) {
109112
}
110113

111114
// QUICK CHECK TESTS ARE COMING WITH NEXT RELEASE.
115+
116+
func TestOAS_BuildStream(t *testing.T) {
117+
tests := []struct {
118+
name string
119+
oas *OAS
120+
wantW string
121+
wantErr bool
122+
}{
123+
{
124+
name: "success",
125+
oas: &OAS{
126+
OASVersion: "3.0.1",
127+
Info: Info{
128+
Title: "Test",
129+
Description: "Test object",
130+
},
131+
Components: Components{
132+
Component{
133+
Schemas: Schemas{Schema{
134+
Name: "schema_testing",
135+
Properties: SchemaProperties{
136+
SchemaProperty{
137+
Name: "EnumProp",
138+
Type: "enum",
139+
Description: "short desc",
140+
Enum: []string{"enum", "test", "strSlc"},
141+
},
142+
SchemaProperty{
143+
Name: "intProp",
144+
Type: "integer",
145+
Format: "int64",
146+
Description: "short desc",
147+
Default: 1337,
148+
},
149+
},
150+
XML: XMLEntry{Name: "XML entry test"},
151+
}},
152+
SecuritySchemes: SecuritySchemes{SecurityScheme{
153+
Name: "ses_scheme_testing",
154+
In: "not empty",
155+
Flows: SecurityFlows{SecurityFlow{
156+
Type: "implicit",
157+
AuthURL: "http://petstore.swagger.io/oauth/dialog",
158+
Scopes: SecurityScopes{
159+
SecurityScope{
160+
Name: "write:pets",
161+
Description: "Write to Pets",
162+
},
163+
SecurityScope{
164+
Name: "read:pets",
165+
Description: "Read Pets",
166+
},
167+
},
168+
}},
169+
}},
170+
},
171+
},
172+
},
173+
wantErr: false,
174+
wantW: `openapi: 3.0.1
175+
info:
176+
title: Test
177+
description: Test object
178+
termsOfService: ""
179+
contact:
180+
email: ""
181+
license:
182+
name: ""
183+
url: ""
184+
version: ""
185+
externalDocs:
186+
description: ""
187+
url: ""
188+
servers: []
189+
tags: []
190+
paths: {}
191+
components:
192+
schemas:
193+
schema_testing:
194+
$ref: ""
195+
properties:
196+
EnumProp:
197+
description: short desc
198+
enum:
199+
- enum
200+
- test
201+
- strSlc
202+
type: enum
203+
intProp:
204+
default: 1337
205+
description: short desc
206+
format: int64
207+
type: integer
208+
type: ""
209+
xml:
210+
name: XML entry test
211+
securitySchemes:
212+
ses_scheme_testing:
213+
flows:
214+
implicit:
215+
authorizationUrl: http://petstore.swagger.io/oauth/dialog
216+
scopes:
217+
read:pets: Read Pets
218+
write:pets: Write to Pets
219+
in: not empty
220+
`,
221+
},
222+
}
223+
for _, tt := range tests {
224+
t.Run(tt.name, func(t *testing.T) {
225+
w := &bytes.Buffer{}
226+
if err := tt.oas.BuildStream(w); (err != nil) != tt.wantErr {
227+
t.Errorf("OAS.BuildStream() error = %v, wantErr %v", err, tt.wantErr)
228+
return
229+
}
230+
if gotW := w.String(); gotW != tt.wantW {
231+
t.Errorf("OAS.BuildStream() = [%v], want {%v}", gotW, tt.wantW)
232+
}
233+
})
234+
}
235+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)