-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
109 lines (100 loc) · 3.2 KB
/
request.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
package chioas
import (
"github.com/go-andiamo/chioas/yaml"
)
// Request represents the OAS definition of a request
type Request struct {
// Ref is the OAS $ref name for the request
//
// If this is a non-empty string and the response is used by Method.Request, then a $ref to "#/components/requestBodies/" is used
//
// If the Request is used by Components.Requests this value is ignored
Ref string
// Description is the OAS description
Description string
// Required is the OAS flag determining if the request is required
Required bool
// ContentType is the OAS content type
//
// defaults to "application/json"
ContentType string
// AlternativeContentTypes is a map of alternative content types (where the key is the media type - e.g. "application/json")
AlternativeContentTypes ContentTypes
// Schema is the optional OAS Schema
//
// Only used if the value is non-nil - otherwise uses SchemaRef is used
//
// The value can be any of the following:
//
// * chioas.Schema (or *chioas.Schema)
//
// * a chioas.SchemaConverter
//
// * a chioas.SchemaWriter
//
// * a struct or ptr to struct (schema written is determined by examining struct fields)
//
// * a slice of structs (items schema written is determined by examining struct fields)
Schema any
// SchemaRef is the OAS schema reference
//
// Only used if value is a non-empty string - if both Schema is nil and SchemaRef is empty string, then an
// empty object schema is written to the spec yaml, e.g.
// schema:
// type: "object"
//
// If the value does not contain a path (i.e. does not contain any "/") then the ref
// path will be the value prefixed with components schemas path. For example, specifying "foo"
// will result in a schema ref:
// schema:
// $ref: "#/components/schemas/foo"
SchemaRef string
// IsArray indicates that request is an array of items
IsArray bool
// Examples is the ordered list of examples for the request
Examples Examples
// Extensions is extension OAS yaml properties
Extensions Extensions
// Additional is any additional OAS spec yaml to be written
Additional Additional
// Comment is any comment(s) to appear in the OAS spec yaml (not used with Ref)
Comment string
}
func (r *Request) isArray() bool {
return r.IsArray
}
func (r *Request) schema() any {
return r.Schema
}
func (r *Request) schemaRef() string {
return r.SchemaRef
}
func (r *Request) examples() Examples {
return r.Examples
}
func (r *Request) alternatives() ContentTypes {
return r.AlternativeContentTypes
}
func (r *Request) writeYaml(w yaml.Writer) {
w.WriteTagStart(tagNameRequestBody)
if r.Ref == "" {
w.WriteComments(r.Comment).
WriteTagValue(tagNameDescription, r.Description).
WriteTagValue(tagNameRequired, r.Required)
writeContent(r.ContentType, r, w)
writeExtensions(r.Extensions, w)
writeAdditional(r.Additional, r, w)
} else {
writeRef(tagNameRequestBodies, r.Ref, w)
}
w.WriteTagEnd()
}
func (r *Request) componentsWriteYaml(name string, w yaml.Writer) {
w.WriteTagStart(name).
WriteTagValue(tagNameDescription, r.Description).
WriteTagValue(tagNameRequired, r.Required)
writeContent(r.ContentType, r, w)
writeExtensions(r.Extensions, w)
writeAdditional(r.Additional, r, w)
w.WriteTagEnd()
}