-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_body.go
More file actions
53 lines (47 loc) · 1.43 KB
/
request_body.go
File metadata and controls
53 lines (47 loc) · 1.43 KB
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
package openapi
// RequestBody represents a request body in OpenAPI
type RequestBody struct {
Ref string `json:"$ref,omitempty"`
Description string `json:"description,omitempty"`
Content map[string]MediaType `json:"content"`
Required bool `json:"required,omitempty"`
}
// NewRequestBody creates a new request body
func NewRequestBody(description string, required bool) RequestBody {
return RequestBody{
Description: description,
Required: required,
Content: make(map[string]MediaType),
}
}
// NewJSONRequestBody creates a JSON request body
func NewJSONRequestBody(description string, required bool, schema *Schema) RequestBody {
return RequestBody{
Description: description,
Required: required,
Content: map[string]MediaType{
"application/json": {
Schema: schema,
},
},
}
}
// WithContent adds content to the request body
func (r RequestBody) WithContent(mediaType string, content MediaType) RequestBody {
if r.Content == nil {
r.Content = make(map[string]MediaType)
}
r.Content[mediaType] = content
return r
}
// WithJSONContent adds JSON content
func (r RequestBody) WithJSONContent(schema *Schema) RequestBody {
return r.WithContent("application/json", MediaType{
Schema: schema,
})
}
// WithRequired sets whether the request body is required
func (r RequestBody) WithRequired(required bool) RequestBody {
r.Required = required
return r
}