@@ -162,73 +162,13 @@ func renderServiceRoutes(service spec.Service, groups []spec.Group, paths swagge
162
162
}
163
163
if defineStruct , ok := route .RequestType .(spec.DefineStruct ); ok {
164
164
for _ , member := range defineStruct .Members {
165
- if member .Name == "" {
166
- memberDefineStruct , _ := member .Type .(spec.DefineStruct )
167
- for _ , m := range memberDefineStruct .Members {
168
- if strings .Contains (m .Tag , "header" ) {
169
- tempKind := swaggerMapTypes [strings .Replace (m .Type .Name (), "[]" , "" , - 1 )]
170
- ftype , format , ok := primitiveSchema (tempKind , m .Type .Name ())
171
- if ! ok {
172
- ftype = tempKind .String ()
173
- format = "UNKNOWN"
174
- }
175
- sp := swaggerParameterObject {In : "header" , Type : ftype , Format : format }
176
-
177
- for _ , tag := range m .Tags () {
178
- sp .Name = tag .Name
179
- if len (tag .Options ) == 0 {
180
- sp .Required = true
181
- continue
182
- }
183
-
184
- required := true
185
- for _ , option := range tag .Options {
186
- if strings .HasPrefix (option , optionsOption ) {
187
- segs := strings .SplitN (option , equalToken , 2 )
188
- if len (segs ) == 2 {
189
- sp .Enum = strings .Split (segs [1 ], optionSeparator )
190
- }
191
- }
192
-
193
- if strings .HasPrefix (option , rangeOption ) {
194
- segs := strings .SplitN (option , equalToken , 2 )
195
- if len (segs ) == 2 {
196
- min , max , ok := parseRangeOption (segs [1 ])
197
- if ok {
198
- sp .Schema .Minimum = min
199
- sp .Schema .Maximum = max
200
- }
201
- }
202
- }
203
-
204
- if strings .HasPrefix (option , defaultOption ) {
205
- segs := strings .Split (option , equalToken )
206
- if len (segs ) == 2 {
207
- sp .Default = segs [1 ]
208
- }
209
- } else if strings .HasPrefix (option , optionalOption ) || strings .HasPrefix (option , omitemptyOption ) {
210
- required = false
211
- }
212
-
213
- if strings .HasPrefix (option , exampleOption ) {
214
- segs := strings .Split (option , equalToken )
215
- if len (segs ) == 2 {
216
- sp .Example = segs [1 ]
217
- }
218
- }
219
- }
220
- sp .Required = required
221
- }
222
- sp .Description = strings .TrimLeft (m .Comment , "//" )
223
- parameters = append (parameters , sp )
224
- }
225
- }
226
- continue
165
+ if hasHeaderParameters (member ) {
166
+ parameters = parseHeader (member , parameters )
227
167
}
228
168
}
229
169
if strings .ToUpper (route .Method ) == http .MethodGet {
230
170
for _ , member := range defineStruct .Members {
231
- if strings . Contains (member . Tag , "path" ) {
171
+ if hasPathParameters (member ) || hasHeaderParameters ( member ) {
232
172
continue
233
173
}
234
174
if embedStruct , isEmbed := member .Type .(spec.DefineStruct ); isEmbed {
@@ -483,7 +423,7 @@ func renderReplyAsDefinition(d swaggerDefinitionsObject, m messageMap, p []spec.
483
423
schema .Title = defineStruct .Name ()
484
424
485
425
for _ , member := range defineStruct .Members {
486
- if hasPathParameters (member ) {
426
+ if hasPathParameters (member ) || hasHeaderParameters ( member ) {
487
427
continue
488
428
}
489
429
kv := keyVal {Value : schemaOfField (member )}
@@ -494,8 +434,7 @@ func renderReplyAsDefinition(d swaggerDefinitionsObject, m messageMap, p []spec.
494
434
if kv .Key == "" {
495
435
memberStruct , _ := member .Type .(spec.DefineStruct )
496
436
for _ , m := range memberStruct .Members {
497
-
498
- if strings .Contains (m .Tag , "header" ) {
437
+ if hasHeaderParameters (m ) || hasPathParameters (m ) {
499
438
continue
500
439
}
501
440
@@ -560,6 +499,15 @@ func hasPathParameters(member spec.Member) bool {
560
499
return false
561
500
}
562
501
502
+ func hasHeaderParameters (member spec.Member ) bool {
503
+ for _ , tag := range member .Tags () {
504
+ if tag .Key == "header" {
505
+ return true
506
+ }
507
+ }
508
+ return false
509
+ }
510
+
563
511
func schemaOfField (member spec.Member ) swaggerSchemaObject {
564
512
ret := swaggerSchemaObject {}
565
513
@@ -751,3 +699,73 @@ func contains(s []string, str string) bool {
751
699
752
700
return false
753
701
}
702
+
703
+ func parseHeader (m spec.Member , parameters []swaggerParameterObject ) []swaggerParameterObject {
704
+
705
+ tempKind := swaggerMapTypes [strings .Replace (m .Type .Name (), "[]" , "" , - 1 )]
706
+ ftype , format , ok := primitiveSchema (tempKind , m .Type .Name ())
707
+ if ! ok {
708
+ ftype = tempKind .String ()
709
+ format = "UNKNOWN"
710
+ }
711
+ sp := swaggerParameterObject {In : "header" , Type : ftype , Format : format }
712
+
713
+ for _ , tag := range m .Tags () {
714
+ sp .Name = tag .Name
715
+ if len (tag .Options ) == 0 {
716
+ sp .Required = true
717
+ continue
718
+ }
719
+
720
+ required := true
721
+ for _ , option := range tag .Options {
722
+ if strings .HasPrefix (option , optionsOption ) {
723
+ segs := strings .SplitN (option , equalToken , 2 )
724
+ if len (segs ) == 2 {
725
+ sp .Enum = strings .Split (segs [1 ], optionSeparator )
726
+ }
727
+ }
728
+
729
+ if strings .HasPrefix (option , rangeOption ) {
730
+ segs := strings .SplitN (option , equalToken , 2 )
731
+ if len (segs ) == 2 {
732
+ min , max , ok := parseRangeOption (segs [1 ])
733
+ if ok {
734
+ sp .Schema .Minimum = min
735
+ sp .Schema .Maximum = max
736
+ }
737
+ }
738
+ }
739
+
740
+ if strings .HasPrefix (option , defaultOption ) {
741
+ segs := strings .Split (option , equalToken )
742
+ if len (segs ) == 2 {
743
+ sp .Default = segs [1 ]
744
+ }
745
+ } else if strings .HasPrefix (option , optionalOption ) || strings .HasPrefix (option , omitemptyOption ) {
746
+ required = false
747
+ }
748
+
749
+ if strings .HasPrefix (option , exampleOption ) {
750
+ segs := strings .Split (option , equalToken )
751
+ if len (segs ) == 2 {
752
+ sp .Example = segs [1 ]
753
+ }
754
+ }
755
+ }
756
+ sp .Required = required
757
+ }
758
+ sp .Description = strings .TrimLeft (m .Comment , "//" )
759
+ if m .Name == "" {
760
+ memberDefineStruct , ok := m .Type .(spec.DefineStruct )
761
+ if ! ok {
762
+ return parameters
763
+ }
764
+ for _ , cm := range memberDefineStruct .Members {
765
+ if hasHeaderParameters (cm ) {
766
+ parameters = parseHeader (cm , parameters )
767
+ }
768
+ }
769
+ }
770
+ return append (parameters , sp )
771
+ }
0 commit comments