-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
openapi.go
1642 lines (1427 loc) · 58.7 KB
/
openapi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package huma
import (
"bytes"
"encoding/json"
"net/http"
"reflect"
"time"
"github.com/danielgtaylor/huma/v2/yaml"
)
type omitType int
const (
// omitNever means the field will always be included.
omitNever omitType = iota
// omitEmpty means the field will be omitted if it is empty.
omitEmpty
// omitNil means the field will be omitted if it is nil. This is used for
// fields which are `any` and may be the zero value of the type.
omitNil
)
type jsonFieldInfo struct {
name string
value any
omit omitType
}
// isEmptyValue returns true if the given value is empty. This is a copy of
// isEmptyValue from encoding/json to help determine when to write a field
// with `omitempty` set.
func isEmptyValue(v reflect.Value) bool {
switch v.Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
return v.Len() == 0
case reflect.Bool:
return !v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Interface, reflect.Pointer:
return v.IsNil()
}
return false
}
// marshalJSON marshals a list of fields and their values into JSON. It supports
// inlined extensions.
func marshalJSON(fields []jsonFieldInfo, extensions map[string]any) ([]byte, error) {
value := make(map[string]any, len(extensions)+len(fields))
for _, v := range fields {
if v.omit == omitNil && v.value == nil {
continue
}
if v.omit == omitEmpty {
if isEmptyValue(reflect.ValueOf(v.value)) {
continue
}
}
value[v.name] = v.value
}
for k, v := range extensions {
value[k] = v
}
return json.Marshal(value)
}
// Contact information to get support for the API.
//
// name: API Support
// url: https://www.example.com/support
// email: support@example.com
type Contact struct {
// Name of the contact person/organization.
Name string `yaml:"name,omitempty"`
// URL pointing to the contact information.
URL string `yaml:"url,omitempty"`
// Email address of the contact person/organization.
Email string `yaml:"email,omitempty"`
// Extensions (user-defined properties), if any. Values in this map will
// be marshalled as siblings of the other properties above.
Extensions map[string]any `yaml:",inline"`
}
func (c *Contact) MarshalJSON() ([]byte, error) {
return marshalJSON([]jsonFieldInfo{
{"name", c.Name, omitEmpty},
{"url", c.URL, omitEmpty},
{"email", c.Email, omitEmpty},
}, c.Extensions)
}
// License name & link for using the API.
//
// name: Apache 2.0
// identifier: Apache-2.0
type License struct {
// Name of the license.
Name string `yaml:"name"`
// Identifier SPDX license expression for the API. This field is mutually
// exclusive with the URL field.
Identifier string `yaml:"identifier,omitempty"`
// URL pointing to the license. This field is mutually exclusive with the
// Identifier field.
URL string `yaml:"url,omitempty"`
// Extensions (user-defined properties), if any. Values in this map will
// be marshalled as siblings of the other properties above.
Extensions map[string]any `yaml:",inline"`
}
func (l *License) MarshalJSON() ([]byte, error) {
return marshalJSON([]jsonFieldInfo{
{"name", l.Name, omitNever},
{"identifier", l.Identifier, omitEmpty},
{"url", l.URL, omitEmpty},
}, l.Extensions)
}
// Info object that provides metadata about the API. The metadata MAY be used by
// the clients if needed, and MAY be presented in editing or documentation
// generation tools for convenience.
//
// title: Sample Pet Store App
// summary: A pet store manager.
// description: This is a sample server for a pet store.
// termsOfService: https://example.com/terms/
// contact:
// name: API Support
// url: https://www.example.com/support
// email: support@example.com
// license:
// name: Apache 2.0
// url: https://www.apache.org/licenses/LICENSE-2.0.html
// version: 1.0.1
type Info struct {
// Title of the API.
Title string `yaml:"title"`
// Description of the API. CommonMark syntax MAY be used for rich text representation.
Description string `yaml:"description,omitempty"`
// TermsOfService URL for the API.
TermsOfService string `yaml:"termsOfService,omitempty"`
// Contact information to get support for the API.
Contact *Contact `yaml:"contact,omitempty"`
// License name & link for using the API.
License *License `yaml:"license,omitempty"`
// Version of the OpenAPI document (which is distinct from the OpenAPI Specification version or the API implementation version).
Version string `yaml:"version"`
// Extensions (user-defined properties), if any. Values in this map will
// be marshalled as siblings of the other properties above.
Extensions map[string]any `yaml:",inline"`
}
func (i *Info) MarshalJSON() ([]byte, error) {
return marshalJSON([]jsonFieldInfo{
{"title", i.Title, omitNever},
{"description", i.Description, omitEmpty},
{"termsOfService", i.TermsOfService, omitEmpty},
{"contact", i.Contact, omitEmpty},
{"license", i.License, omitEmpty},
{"version", i.Version, omitNever},
}, i.Extensions)
}
// ServerVariable for server URL template substitution.
type ServerVariable struct {
// Enumeration of string values to be used if the substitution options are from a limited set. The array MUST NOT be empty.
Enum []string `yaml:"enum,omitempty"`
// Default value to use for substitution, which SHALL be sent if an alternate value is not supplied.
Default string `yaml:"default"`
// Description for the server variable. CommonMark syntax MAY be used for rich text representation.
Description string `yaml:"description,omitempty"`
// Extensions (user-defined properties), if any. Values in this map will
// be marshalled as siblings of the other properties above.
Extensions map[string]any `yaml:",inline"`
}
func (v *ServerVariable) MarshalJSON() ([]byte, error) {
return marshalJSON([]jsonFieldInfo{
{"enum", v.Enum, omitEmpty},
{"default", v.Default, omitNever},
{"description", v.Description, omitEmpty},
}, v.Extensions)
}
// Server URL, optionally with variables.
//
// servers:
// - url: https://development.gigantic-server.com/v1
// description: Development server
// - url: https://staging.gigantic-server.com/v1
// description: Staging server
// - url: https://api.gigantic-server.com/v1
// description: Production server
type Server struct {
// URL to the target host. This URL supports Server Variables and MAY be relative, to indicate that the host location is relative to the location where the OpenAPI document is being served. Variable substitutions will be made when a variable is named in {brackets}.
URL string `yaml:"url"`
// Description of the host designated by the URL. CommonMark syntax MAY be used for rich text representation.
Description string `yaml:"description,omitempty"`
// Variables map between a variable name and its value. The value is used for substitution in the server’s URL template.
Variables map[string]*ServerVariable `yaml:"variables,omitempty"`
// Extensions (user-defined properties), if any. Values in this map will
// be marshalled as siblings of the other properties above.
Extensions map[string]any `yaml:",inline"`
}
func (s *Server) MarshalJSON() ([]byte, error) {
return marshalJSON([]jsonFieldInfo{
{"url", s.URL, omitNever},
{"description", s.Description, omitEmpty},
{"variables", s.Variables, omitEmpty},
}, s.Extensions)
}
// Example value of a request param or body or response header or body.
//
// requestBody:
// content:
// 'application/json':
// schema:
// $ref: '#/components/schemas/Address'
// examples:
// foo:
// summary: A foo example
// value: {"foo": "bar"}
// bar:
// summary: A bar example
// value: {"bar": "baz"}
type Example struct {
// Ref is a reference to another example. This field is mutually exclusive
// with the other fields.
Ref string `yaml:"$ref,omitempty"`
// Summary is a short summary of the example.
Summary string `yaml:"summary,omitempty"`
// Description is a long description of the example. CommonMark syntax MAY
// be used for rich text representation.
Description string `yaml:"description,omitempty"`
// Value is an embedded literal example. The `value` field and `externalValue`
// field are mutually exclusive. To represent examples of media types that
// cannot naturally represented in JSON or YAML, use a string value to contain
// the example, escaping where necessary.
Value any `yaml:"value,omitempty"`
// ExternalValue is a URI that points to the literal example. This provides
// the capability to reference examples that cannot easily be included in JSON
// or YAML documents. The `value` field and `externalValue` field are mutually
// exclusive. See the rules for resolving Relative References.
ExternalValue string `yaml:"externalValue,omitempty"`
// Extensions (user-defined properties), if any. Values in this map will
// be marshalled as siblings of the other properties above.
Extensions map[string]any `yaml:",inline"`
}
func (e *Example) MarshalJSON() ([]byte, error) {
return marshalJSON([]jsonFieldInfo{
{"$ref", e.Ref, omitEmpty},
{"summary", e.Summary, omitEmpty},
{"description", e.Description, omitEmpty},
{"value", e.Value, omitNil},
{"externalValue", e.ExternalValue, omitEmpty},
}, e.Extensions)
}
// Encoding is a single encoding definition applied to a single schema property.
//
// requestBody:
// content:
// multipart/form-data:
// schema:
// type: object
// properties:
// id:
// # default is text/plain
// type: string
// format: uuid
// address:
// # default is application/json
// type: object
// properties: {}
// historyMetadata:
// # need to declare XML format!
// description: metadata in XML format
// type: object
// properties: {}
// profileImage: {}
// encoding:
// historyMetadata:
// # require XML Content-Type in utf-8 encoding
// contentType: application/xml; charset=utf-8
// profileImage:
// # only accept png/jpeg
// contentType: image/png, image/jpeg
// headers:
// X-Rate-Limit-Limit:
// description: The number of allowed requests in the current period
// schema:
// type: integer
type Encoding struct {
// ContentType for encoding a specific property. Default value depends on the
// property type: for object - application/json; for array – the default is
// defined based on the inner type; for all other cases the default is
// application/octet-stream. The value can be a specific media type (e.g.
// application/json), a wildcard media type (e.g. image/*), or a
// comma-separated list of the two types.
ContentType string `yaml:"contentType,omitempty"`
// Headers is a map allowing additional information to be provided as headers,
// for example Content-Disposition. Content-Type is described separately and
// SHALL be ignored in this section. This property SHALL be ignored if the
// request body media type is not a multipart.
Headers map[string]*Header `yaml:"headers,omitempty"`
// Style describes how a specific property value will be serialized depending
// on its type. See Parameter Object for details on the style property. The
// behavior follows the same values as query parameters, including default
// values. This property SHALL be ignored if the request body media type is
// not application/x-www-form-urlencoded or multipart/form-data. If a value is
// explicitly defined, then the value of contentType (implicit or explicit)
// SHALL be ignored.
Style string `yaml:"style,omitempty"`
// Explode, when true, property values of type array or object generate
// separate parameters for each value of the array, or key-value-pair of the
// map. For other types of properties this property has no effect. When style
// is form, the default value is true. For all other styles, the default value
// is false. This property SHALL be ignored if the request body media type is
// not application/x-www-form-urlencoded or multipart/form-data. If a value is
// explicitly defined, then the value of contentType (implicit or explicit)
// SHALL be ignored.
Explode *bool `yaml:"explode,omitempty"`
// AllowReserved determines whether the parameter value SHOULD allow reserved
// characters, as defined by [RFC3986] :/?#[]@!$&'()*+,;= to be included
// without percent-encoding. The default value is false. This property SHALL
// be ignored if the request body media type is not
// application/x-www-form-urlencoded or multipart/form-data. If a value is
// explicitly defined, then the value of contentType (implicit or explicit)
// SHALL be ignored.
AllowReserved bool `yaml:"allowReserved,omitempty"`
// Extensions (user-defined properties), if any. Values in this map will
// be marshalled as siblings of the other properties above.
Extensions map[string]any `yaml:",inline"`
}
func (e *Encoding) MarshalJSON() ([]byte, error) {
return marshalJSON([]jsonFieldInfo{
{"contentType", e.ContentType, omitEmpty},
{"headers", e.Headers, omitEmpty},
{"style", e.Style, omitEmpty},
{"explode", e.Explode, omitEmpty},
{"allowReserved", e.AllowReserved, omitEmpty},
}, e.Extensions)
}
// MediaType object provides schema and examples for the media type identified
// by its key.
//
// application/json:
// schema:
// $ref: "#/components/schemas/Pet"
// examples:
// cat:
// summary: An example of a cat
// value:
// name: Fluffy
// petType: Cat
// color: White
// gender: male
// breed: Persian
type MediaType struct {
// Schema defining the content of the request, response, or parameter.
Schema *Schema `yaml:"schema,omitempty"`
// Example of the media type. The example object SHOULD be in the correct
// format as specified by the media type. The example field is mutually
// exclusive of the examples field. Furthermore, if referencing a schema which
// contains an example, the example value SHALL override the example provided
// by the schema.
Example any `yaml:"example,omitempty"`
// Examples of the media type. Each example object SHOULD match the media type
// and specified schema if present. The examples field is mutually exclusive
// of the example field. Furthermore, if referencing a schema which contains
// an example, the examples value SHALL override the example provided by the
// schema.
Examples map[string]*Example `yaml:"examples,omitempty"`
// Encoding is a map between a property name and its encoding information. The
// key, being the property name, MUST exist in the schema as a property. The
// encoding object SHALL only apply to requestBody objects when the media type
// is multipart or application/x-www-form-urlencoded.
Encoding map[string]*Encoding `yaml:"encoding,omitempty"`
// Extensions (user-defined properties), if any. Values in this map will
// be marshalled as siblings of the other properties above.
Extensions map[string]any `yaml:",inline"`
}
func (m *MediaType) MarshalJSON() ([]byte, error) {
return marshalJSON([]jsonFieldInfo{
{"schema", m.Schema, omitEmpty},
{"example", m.Example, omitNil},
{"examples", m.Examples, omitEmpty},
{"encoding", m.Encoding, omitEmpty},
}, m.Extensions)
}
// Param Describes a single operation parameter.
//
// A unique parameter is defined by a combination of a name and location.
//
// name: username
// in: path
// description: username to fetch
// required: true
// schema:
// type: string
type Param struct {
// Ref is a reference to another example. This field is mutually exclusive
// with the other fields.
Ref string `yaml:"$ref,omitempty"`
// Name is REQUIRED. The name of the parameter. Parameter names are case
// sensitive.
//
// - If in is "path", the name field MUST correspond to a template expression
// occurring within the path field in the Paths Object. See Path Templating
// for further information.
//
// - If in is "header" and the name field is "Accept", "Content-Type" or
// "Authorization", the parameter definition SHALL be ignored.
//
// - For all other cases, the name corresponds to the parameter name used by
// the in property.
Name string `yaml:"name,omitempty"`
// In is REQUIRED. The location of the parameter. Possible values are "query",
// "header", "path" or "cookie".
In string `yaml:"in,omitempty"`
// Description of the parameter. This could contain examples of use.
// CommonMark syntax MAY be used for rich text representation.
Description string `yaml:"description,omitempty"`
// Required determines whether this parameter is mandatory. If the parameter
// location is "path", this property is REQUIRED and its value MUST be true.
// Otherwise, the property MAY be included and its default value is false.
Required bool `yaml:"required,omitempty"`
// Deprecated specifies that a parameter is deprecated and SHOULD be
// transitioned out of usage. Default value is false.
Deprecated bool `yaml:"deprecated,omitempty"`
// AllowEmptyValue sets the ability to pass empty-valued parameters. This is
// valid only for query parameters and allows sending a parameter with an
// empty value. Default value is false. If style is used, and if behavior is
// n/a (cannot be serialized), the value of allowEmptyValue SHALL be ignored.
// Use of this property is NOT RECOMMENDED, as it is likely to be removed in a
// later revision.
AllowEmptyValue bool `yaml:"allowEmptyValue,omitempty"`
// Style describes how the parameter value will be serialized depending on the
// type of the parameter value. Default values (based on value of in): for
// query - form; for path - simple; for header - simple; for cookie - form.
Style string `yaml:"style,omitempty"`
// Explode, when true, makes parameter values of type array or object generate
// separate parameters for each value of the array or key-value pair of the
// map. For other types of parameters this property has no effect. When style
// is form, the default value is true. For all other styles, the default value
// is false.
Explode *bool `yaml:"explode,omitempty"`
// AllowReserved determines whether the parameter value SHOULD allow reserved
// characters, as defined by [RFC3986] :/?#[]@!$&'()*+,;= to be included
// without percent-encoding. This property only applies to parameters with an
// in value of query. The default value is false.
AllowReserved bool `yaml:"allowReserved,omitempty"`
// Schema defining the type used for the parameter.
Schema *Schema `yaml:"schema,omitempty"`
// Example of the parameter’s potential value. The example SHOULD match the
// specified schema and encoding properties if present. The example field is
// mutually exclusive of the examples field. Furthermore, if referencing a
// schema that contains an example, the example value SHALL override the
// example provided by the schema. To represent examples of media types that
// cannot naturally be represented in JSON or YAML, a string value can contain
// the example with escaping where necessary.
Example any `yaml:"example,omitempty"`
// Examples of the parameter’s potential value. Each example SHOULD contain a
// value in the correct format as specified in the parameter encoding. The
// examples field is mutually exclusive of the example field. Furthermore, if
// referencing a schema that contains an example, the examples value SHALL
// override the example provided by the schema.
Examples map[string]*Example `yaml:"examples,omitempty"`
// Extensions (user-defined properties), if any. Values in this map will
// be marshalled as siblings of the other properties above.
Extensions map[string]any `yaml:",inline"`
}
func (p *Param) MarshalJSON() ([]byte, error) {
return marshalJSON([]jsonFieldInfo{
{"$ref", p.Ref, omitEmpty},
{"name", p.Name, omitEmpty},
{"in", p.In, omitEmpty},
{"description", p.Description, omitEmpty},
{"required", p.Required, omitEmpty},
{"deprecated", p.Deprecated, omitEmpty},
{"allowEmptyValue", p.AllowEmptyValue, omitEmpty},
{"style", p.Style, omitEmpty},
{"explode", p.Explode, omitEmpty},
{"allowReserved", p.AllowReserved, omitEmpty},
{"schema", p.Schema, omitEmpty},
{"example", p.Example, omitNil},
{"examples", p.Examples, omitEmpty},
}, p.Extensions)
}
// Header object follows the structure of the Parameter Object with the
// following changes:
//
// - name MUST NOT be specified, it is given in the corresponding headers map.
//
// - in MUST NOT be specified, it is implicitly in header.
//
// - All traits that are affected by the location MUST be applicable to a
// location of header (for example, style).
//
// Example:
//
// description: The number of allowed requests in the current period
// schema:
// type: integer
type Header = Param
// RequestBody describes a single request body.
//
// description: user to add to the system
// content:
// 'application/json':
// schema:
// $ref: '#/components/schemas/User'
// examples:
// user:
// summary: User Example
// externalValue: 'https://foo.bar/examples/user-example.json'
type RequestBody struct {
// Ref is a reference to another example. This field is mutually exclusive
// with the other fields.
Ref string `yaml:"$ref,omitempty"`
// Description of the request body. This could contain examples of use.
// CommonMark syntax MAY be used for rich text representation.
Description string `yaml:"description,omitempty"`
// Content is REQUIRED. The content of the request body. The key is a media
// type or media type range and the value describes it. For requests that
// match multiple keys, only the most specific key is applicable. e.g.
// text/plain overrides text/*
Content map[string]*MediaType `yaml:"content"`
// Required Determines if the request body is required in the request.
// Defaults to false.
Required bool `yaml:"required,omitempty"`
// Extensions (user-defined properties), if any. Values in this map will
// be marshalled as siblings of the other properties above.
Extensions map[string]any `yaml:",inline"`
}
func (r *RequestBody) MarshalJSON() ([]byte, error) {
return marshalJSON([]jsonFieldInfo{
{"$ref", r.Ref, omitEmpty},
{"description", r.Description, omitEmpty},
{"content", r.Content, omitNever},
{"required", r.Required, omitEmpty},
}, r.Extensions)
}
// Link object represents a possible design-time link for a response. The
// presence of a link does not guarantee the caller’s ability to successfully
// invoke it, rather it provides a known relationship and traversal mechanism
// between responses and other operations.
//
// Unlike dynamic links (i.e. links provided in the response payload), the OAS
// linking mechanism does not require link information in the runtime response.
//
// For computing links, and providing instructions to execute them, a runtime
// expression is used for accessing values in an operation and using them as
// parameters while invoking the linked operation.
//
// paths:
// /users/{id}:
// parameters:
// - name: id
// in: path
// required: true
// description: the user identifier, as userId
// schema:
// type: string
// get:
// responses:
// '200':
// description: the user being returned
// content:
// application/json:
// schema:
// type: object
// properties:
// uuid: # the unique user id
// type: string
// format: uuid
// links:
// address:
// # the target link operationId
// operationId: getUserAddress
// parameters:
// # get the `id` field from the request path parameter named `id`
// userId: $request.path.id
// # the path item of the linked operation
// /users/{userid}/address:
// parameters:
// - name: userid
// in: path
// required: true
// description: the user identifier, as userId
// schema:
// type: string
// # linked operation
// get:
// operationId: getUserAddress
// responses:
// '200':
// description: the user's address
type Link struct {
// Ref is a reference to another example. This field is mutually exclusive
// with the other fields.
Ref string `yaml:"$ref,omitempty"`
// OperationRef is a relative or absolute URI reference to an OAS operation.
// This field is mutually exclusive of the operationId field, and MUST point
// to an Operation Object. Relative operationRef values MAY be used to locate
// an existing Operation Object in the OpenAPI definition. See the rules for
// resolving Relative References.
OperationRef string `yaml:"operationRef,omitempty"`
// OperationID is the name of an existing, resolvable OAS operation, as
// defined with a unique operationId. This field is mutually exclusive of the
// operationRef field.
OperationID string `yaml:"operationId,omitempty"`
// Parameters is a map representing parameters to pass to an operation as
// specified with operationId or identified via operationRef. The key is the
// parameter name to be used, whereas the value can be a constant or an
// expression to be evaluated and passed to the linked operation. The
// parameter name can be qualified using the parameter location [{in}.]{name}
// for operations that use the same parameter name in different locations
// (e.g. path.id).
Parameters map[string]any `yaml:"parameters,omitempty"`
// RequestBody is a literal value or {expression} to use as a request body
// when calling the target operation.
RequestBody any `yaml:"requestBody,omitempty"`
// Description of the link. CommonMark syntax MAY be used for rich text representation.
Description string `yaml:"description,omitempty"`
// Server object to be used by the target operation.
Server *Server `yaml:"server,omitempty"`
// Extensions (user-defined properties), if any. Values in this map will
// be marshalled as siblings of the other properties above.
Extensions map[string]any `yaml:",inline"`
}
func (l *Link) MarshalJSON() ([]byte, error) {
return marshalJSON([]jsonFieldInfo{
{"$ref", l.Ref, omitEmpty},
{"operationRef", l.OperationRef, omitEmpty},
{"operationId", l.OperationID, omitEmpty},
{"parameters", l.Parameters, omitEmpty},
{"requestBody", l.RequestBody, omitNil},
{"description", l.Description, omitEmpty},
{"server", l.Server, omitEmpty},
}, l.Extensions)
}
// Response describes a single response from an API Operation, including
// design-time, static links to operations based on the response.
//
// description: A complex object array response
// content:
// application/json:
// schema:
// type: array
// items:
// $ref: '#/components/schemas/VeryComplexType'
type Response struct {
// Ref is a reference to another example. This field is mutually exclusive
// with the other fields.
Ref string `yaml:"$ref,omitempty"`
// Description is REQUIRED. A description of the response. CommonMark syntax
// MAY be used for rich text representation.
Description string `yaml:"description,omitempty"`
// Headers maps a header name to its definition. [RFC7230] states header names
// are case insensitive. If a response header is defined with the name
// "Content-Type", it SHALL be ignored.
Headers map[string]*Param `yaml:"headers,omitempty"`
// Content is a map containing descriptions of potential response payloads.
// The key is a media type or media type range and the value describes it. For
// responses that match multiple keys, only the most specific key is
// applicable. e.g. text/plain overrides text/*
Content map[string]*MediaType `yaml:"content,omitempty"`
// Links is a map of operations links that can be followed from the response.
// The key of the map is a short name for the link, following the naming
// constraints of the names for Component Objects.
Links map[string]*Link `yaml:"links,omitempty"`
// Extensions (user-defined properties), if any. Values in this map will
// be marshalled as siblings of the other properties above.
Extensions map[string]any `yaml:",inline"`
}
func (r *Response) MarshalJSON() ([]byte, error) {
return marshalJSON([]jsonFieldInfo{
{"$ref", r.Ref, omitEmpty},
{"description", r.Description, omitEmpty},
{"headers", r.Headers, omitEmpty},
{"content", r.Content, omitEmpty},
{"links", r.Links, omitEmpty},
}, r.Extensions)
}
// Operation describes a single API operation on a path.
//
// tags:
// - pet
// summary: Updates a pet in the store with form data
// operationId: updatePetWithForm
// parameters:
// - name: petId
// in: path
// description: ID of pet that needs to be updated
// required: true
// schema:
// type: string
// requestBody:
// content:
// 'application/x-www-form-urlencoded':
// schema:
// type: object
// properties:
// name:
// description: Updated name of the pet
// type: string
// status:
// description: Updated status of the pet
// type: string
// required:
// - status
// responses:
// '200':
// description: Pet updated.
// content:
// 'application/json': {}
// 'application/xml': {}
// '405':
// description: Method Not Allowed
// content:
// 'application/json': {}
// 'application/xml': {}
// security:
// - petstore_auth:
// - write:pets
// - read:pets
type Operation struct {
// --- Huma-specific fields ---
// Method is the HTTP method for this operation
Method string `yaml:"-"`
// Path is the URL path for this operation
Path string `yaml:"-"`
// DefaultStatus is the default HTTP status code for this operation. It will
// be set to 200 or 204 if not specified, depending on whether the handler
// returns a response body.
DefaultStatus int `yaml:"-"`
// MaxBodyBytes is the maximum number of bytes to read from the request
// body. If not specified, the default is 1MB. Use -1 for unlimited. If
// the limit is reached, then an HTTP 413 error is returned.
MaxBodyBytes int64 `yaml:"-"`
// BodyReadTimeout is the maximum amount of time to wait for the request
// body to be read. If not specified, the default is 5 seconds. Use -1
// for unlimited. If the timeout is reached, then an HTTP 408 error is
// returned. This value supercedes the server's read timeout, and a value
// of -1 can unset the server's timeout.
BodyReadTimeout time.Duration `yaml:"-"`
// Errors is a list of HTTP status codes that the handler may return. If
// not specified, then a default error response is added to the OpenAPI.
// This is a convenience for handlers that return a fixed set of errors
// where you do not wish to provide each one as an OpenAPI response object.
// Each error specified here is expanded into a response object with the
// schema generated from the type returned by `huma.NewError()`.
Errors []int `yaml:"-"`
// SkipValidateParams disables validation of path, query, and header
// parameters. This can speed up request processing if you want to handle
// your own validation. Use with caution!
SkipValidateParams bool `yaml:"-"`
// SkipValidateBody disables validation of the request body. This can speed
// up request processing if you want to handle your own validation. Use with
// caution!
SkipValidateBody bool `yaml:"-"`
// Hidden will skip documenting this operation in the OpenAPI. This is
// useful for operations that are not intended to be used by clients but
// you'd still like the benefits of using Huma. Generally not recommended.
Hidden bool `yaml:"-"`
// Metadata is a map of arbitrary data that can be attached to the operation.
// This can be used to store custom data, such as custom settings for
// functions which generate operations.
Metadata map[string]any `yaml:"-"`
// Middlewares is a list of middleware functions to run before the handler.
// This is useful for adding custom logic to operations, such as logging,
// authentication, or rate limiting.
Middlewares Middlewares `yaml:"-"`
// --- OpenAPI fields ---
// Tags is a list of tags for API documentation control. Tags can be used for
// logical grouping of operations by resources or any other qualifier.
Tags []string `yaml:"tags,omitempty"`
// Summary is a short summary of what the operation does.
Summary string `yaml:"summary,omitempty"`
// Description is a verbose explanation of the operation behavior. CommonMark
// syntax MAY be used for rich text representation.
Description string `yaml:"description,omitempty"`
// ExternalDocs describes additional external documentation for this
// operation.
ExternalDocs *ExternalDocs `yaml:"externalDocs,omitempty"`
// OperationID is a unique string used to identify the operation. The id MUST
// be unique among all operations described in the API. The operationId value
// is case-sensitive. Tools and libraries MAY use the operationId to uniquely
// identify an operation, therefore, it is RECOMMENDED to follow common
// programming naming conventions.
OperationID string `yaml:"operationId,omitempty"`
// Parameters is a list of parameters that are applicable for this operation.
// If a parameter is already defined at the Path Item, the new definition will
// override it but can never remove it. The list MUST NOT include duplicated
// parameters. A unique parameter is defined by a combination of a name and
// location. The list can use the Reference Object to link to parameters that
// are defined at the OpenAPI Object’s components/parameters.
Parameters []*Param `yaml:"parameters,omitempty"`
// RequestBody applicable for this operation. The requestBody is fully
// supported in HTTP methods where the HTTP 1.1 specification [RFC7231] has
// explicitly defined semantics for request bodies. In other cases where the
// HTTP spec is vague (such as GET, HEAD and DELETE), requestBody is permitted
// but does not have well-defined semantics and SHOULD be avoided if possible.
RequestBody *RequestBody `yaml:"requestBody,omitempty"`
// Responses is the list of possible responses as they are returned from
// executing this operation.
Responses map[string]*Response `yaml:"responses,omitempty"`
// Callbacks is a map of possible out-of band callbacks related to the parent
// operation. The key is a unique identifier for the Callback Object. Each
// value in the map is a Callback Object that describes a request that may be
// initiated by the API provider and the expected responses.
Callbacks map[string]*PathItem `yaml:"callbacks,omitempty"`
// Deprecated declares this operation to be deprecated. Consumers SHOULD
// refrain from usage of the declared operation. Default value is false.
Deprecated bool `yaml:"deprecated,omitempty"`
// Security is a declaration of which security mechanisms can be used for this
// operation. The list of values includes alternative security requirement
// objects that can be used. Only one of the security requirement objects need
// to be satisfied to authorize a request. To make security optional, an empty
// security requirement ({}) can be included in the array. This definition
// overrides any declared top-level security. To remove a top-level security
// declaration, an empty array can be used.
Security []map[string][]string `yaml:"security,omitempty"`
// Servers is an alternative server array to service this operation. If an
// alternative server object is specified at the Path Item Object or Root
// level, it will be overridden by this value.
Servers []*Server `yaml:"servers,omitempty"`
// Extensions (user-defined properties), if any. Values in this map will
// be marshalled as siblings of the other properties above.
Extensions map[string]any `yaml:",inline"`
}
func (o *Operation) MarshalJSON() ([]byte, error) {
return marshalJSON([]jsonFieldInfo{
{"tags", o.Tags, omitEmpty},
{"summary", o.Summary, omitEmpty},
{"description", o.Description, omitEmpty},
{"externalDocs", o.ExternalDocs, omitEmpty},
{"operationId", o.OperationID, omitEmpty},
{"parameters", o.Parameters, omitEmpty},
{"requestBody", o.RequestBody, omitEmpty},
{"responses", o.Responses, omitEmpty},
{"callbacks", o.Callbacks, omitEmpty},
{"deprecated", o.Deprecated, omitEmpty},
{"security", o.Security, omitEmpty},
{"servers", o.Servers, omitEmpty},
}, o.Extensions)
}
// PathItem describes the operations available on a single path. A Path Item MAY
// be empty, due to ACL constraints. The path itself is still exposed to the
// documentation viewer but they will not know which operations and parameters
// are available.
//
// get:
// description: Returns pets based on ID
// summary: Find pets by ID
// operationId: getPetsById
// responses:
// '200':
// description: pet response
// content:
// '*/*' :
// schema:
// type: array
// items:
// $ref: '#/components/schemas/Pet'
// default:
// description: error payload
// content:
// 'text/html':
// schema:
// $ref: '#/components/schemas/ErrorModel'
// parameters:
// - name: id
// in: path
// description: ID of pet to use
// required: true
// schema:
// type: array
// items:
// type: string
// style: simple
type PathItem struct {
// Ref is a reference to another example. This field is mutually exclusive
// with the other fields.