Skip to content

Commit 7c2294d

Browse files
committed
WIP
1 parent 0973396 commit 7c2294d

File tree

3 files changed

+153
-16
lines changed

3 files changed

+153
-16
lines changed

build.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ func getPathFromFirstElement(cbs []ConfigBuilder) string {
3535
// BuildDocs marshals the OAS struct to YAML and saves it to the chosen output file.
3636
//
3737
// Returns an error if there is any.
38-
func (o *OAS) BuildDocs(conf ...ConfigBuilder) error {
39-
o.initCallStackForRoutes()
38+
func (oas *OAS) BuildDocs(conf ...ConfigBuilder) error {
39+
oas.initCallStackForRoutes()
4040

41-
yml, err := marshalToYAML(o)
41+
yml, err := oas.marshalToYAML()
4242
if err != nil {
4343
return fmt.Errorf("marshaling issue occurred: %w", err)
4444
}
@@ -51,7 +51,24 @@ 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 (oas *OAS) BuildStream(w io.Writer) error {
58+
yml, err := oas.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+
68+
return nil
69+
}
70+
71+
func (oas *OAS) marshalToYAML() ([]byte, error) {
5572
transformedOAS := oas.transformToHybridOAS()
5673

5774
yml, err := yaml.Marshal(transformedOAS)
@@ -111,17 +128,17 @@ type hybridOAS struct {
111128
Components componentsMap `yaml:"components"`
112129
}
113130

114-
func (o *OAS) transformToHybridOAS() hybridOAS {
131+
func (oas *OAS) transformToHybridOAS() hybridOAS {
115132
ho := hybridOAS{}
116133

117-
ho.OpenAPI = o.OASVersion
118-
ho.Info = o.Info
119-
ho.ExternalDocs = o.ExternalDocs
120-
ho.Servers = o.Servers
121-
ho.Tags = o.Tags
134+
ho.OpenAPI = oas.OASVersion
135+
ho.Info = oas.Info
136+
ho.ExternalDocs = oas.ExternalDocs
137+
ho.Servers = oas.Servers
138+
ho.Tags = oas.Tags
122139

123-
ho.Paths = makeAllPathsMap(&o.Paths)
124-
ho.Components = makeComponentsMap(&o.Components)
140+
ho.Paths = makeAllPathsMap(&oas.Paths)
141+
ho.Components = makeComponentsMap(&oas.Components)
125142

126143
return ho
127144
}

build_test.go

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

3-
import "testing"
3+
import (
4+
"bytes"
5+
"testing"
6+
)
7+
8+
const (
9+
buildStreamTestWant = `openapi: 3.0.1
10+
info:
11+
title: Test
12+
description: Test object
13+
termsOfService: ""
14+
contact:
15+
email: ""
16+
license:
17+
name: ""
18+
url: ""
19+
version: ""
20+
externalDocs:
21+
description: ""
22+
url: ""
23+
servers: []
24+
tags: []
25+
paths: {}
26+
components:
27+
schemas:
28+
schema_testing:
29+
properties:
30+
EnumProp:
31+
description: short desc
32+
enum:
33+
- enum
34+
- test
35+
- strSlc
36+
type: enum
37+
intProp:
38+
default: 1337
39+
description: short desc
40+
format: int64
41+
type: integer
42+
type: ""
43+
xml:
44+
name: XML entry test
45+
securitySchemes:
46+
ses_scheme_testing:
47+
flows:
48+
implicit:
49+
authorizationUrl: http://petstore.swagger.io/oauth/dialog
50+
scopes:
51+
read:pets: Read Pets
52+
write:pets: Write to Pets
53+
in: not empty
54+
`
55+
)
456

557
func TestUnitBuild(t *testing.T) {
658
t.Parallel()
@@ -109,3 +161,68 @@ func TestUnitGetPathFromFirstElem(t *testing.T) {
109161
}
110162

111163
// QUICK CHECK TESTS ARE COMING WITH NEXT RELEASE.
164+
165+
func TestOAS_BuildStream(t *testing.T) {
166+
t.Parallel()
167+
168+
tests := []struct {
169+
name string
170+
oas *OAS
171+
wantW string
172+
wantErr bool
173+
}{
174+
{
175+
name: "success",
176+
oas: &OAS{
177+
OASVersion: "3.0.1",
178+
Info: Info{Title: "Test", Description: "Test object"},
179+
Components: Components{
180+
Component{
181+
Schemas: Schemas{Schema{
182+
Name: "schema_testing",
183+
Properties: SchemaProperties{
184+
SchemaProperty{
185+
Name: "EnumProp", Type: "enum", Description: "short desc",
186+
Enum: []string{"enum", "test", "strSlc"},
187+
},
188+
SchemaProperty{
189+
Name: "intProp", Type: "integer", Format: "int64",
190+
Description: "short desc", Default: 1337,
191+
},
192+
},
193+
XML: XMLEntry{Name: "XML entry test"},
194+
}},
195+
SecuritySchemes: SecuritySchemes{SecurityScheme{
196+
Name: "ses_scheme_testing",
197+
In: "not empty",
198+
Flows: SecurityFlows{SecurityFlow{
199+
Type: "implicit",
200+
AuthURL: "http://petstore.swagger.io/oauth/dialog",
201+
Scopes: SecurityScopes{
202+
SecurityScope{Name: "write:pets", Description: "Write to Pets"},
203+
SecurityScope{Name: "read:pets", Description: "Read Pets"},
204+
},
205+
}},
206+
}},
207+
},
208+
},
209+
},
210+
wantErr: false,
211+
wantW: buildStreamTestWant,
212+
},
213+
}
214+
215+
t.Parallel()
216+
for _, tt := range tests {
217+
t.Run(tt.name, func(t *testing.T) {
218+
w := &bytes.Buffer{}
219+
if err := tt.oas.BuildStream(w); (err != nil) != tt.wantErr {
220+
t.Errorf("OAS.BuildStream() error = %v, wantErr %v", err, tt.wantErr)
221+
return
222+
}
223+
if gotW := w.String(); gotW != tt.wantW {
224+
t.Errorf("OAS.BuildStream() = [%v], want {%v}", gotW, tt.wantW)
225+
}
226+
})
227+
}
228+
}

server.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ const (
1818
defaultIndexPath = "/index.html"
1919
fwSlashSuffix = "/"
2020
sigContSleeperMilliseconds = 20
21+
readHeaderTimeout = 60 * time.Second // same than nginx
2122
)
2223

2324
// ConfigSwaggerUI represents a structure which will be used to pass required configuration params to
24-
// the ServeSwaggerUI func.
25+
//
26+
// the ServeSwaggerUI func.
2527
type ConfigSwaggerUI struct {
2628
Route string
2729
Port string
@@ -155,8 +157,9 @@ func (c *ConfigSwaggerUI) initializeDefaultHTTPServer() {
155157
fileServer := http.FileServer(c.initFS)
156158

157159
c.httpServer = &http.Server{
158-
Addr: fmt.Sprintf(":%s", c.Port),
159-
Handler: http.StripPrefix(strings.TrimRight(c.Route, fwSlashSuffix), fileServer),
160+
Addr: fmt.Sprintf(":%s", c.Port),
161+
Handler: http.StripPrefix(strings.TrimRight(c.Route, fwSlashSuffix), fileServer),
162+
ReadHeaderTimeout: readHeaderTimeout,
160163
}
161164
}
162165

0 commit comments

Comments
 (0)