File: openapi2.go

package info (click to toggle)
golang-github-getkin-kin-openapi 0.124.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,288 kB
  • sloc: sh: 344; makefile: 4
file content (120 lines) | stat: -rw-r--r-- 3,994 bytes parent folder | download | duplicates (3)
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
package openapi2

import (
	"encoding/json"

	"github.com/getkin/kin-openapi/openapi3"
)

// T is the root of an OpenAPI v2 document
type T struct {
	Extensions map[string]interface{} `json:"-" yaml:"-"`

	Swagger             string                         `json:"swagger" yaml:"swagger"` // required
	Info                openapi3.Info                  `json:"info" yaml:"info"`       // required
	ExternalDocs        *openapi3.ExternalDocs         `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`
	Schemes             []string                       `json:"schemes,omitempty" yaml:"schemes,omitempty"`
	Consumes            []string                       `json:"consumes,omitempty" yaml:"consumes,omitempty"`
	Produces            []string                       `json:"produces,omitempty" yaml:"produces,omitempty"`
	Host                string                         `json:"host,omitempty" yaml:"host,omitempty"`
	BasePath            string                         `json:"basePath,omitempty" yaml:"basePath,omitempty"`
	Paths               map[string]*PathItem           `json:"paths,omitempty" yaml:"paths,omitempty"`
	Definitions         map[string]*openapi3.SchemaRef `json:"definitions,omitempty" yaml:"definitions,omitempty"`
	Parameters          map[string]*Parameter          `json:"parameters,omitempty" yaml:"parameters,omitempty"`
	Responses           map[string]*Response           `json:"responses,omitempty" yaml:"responses,omitempty"`
	SecurityDefinitions map[string]*SecurityScheme     `json:"securityDefinitions,omitempty" yaml:"securityDefinitions,omitempty"`
	Security            SecurityRequirements           `json:"security,omitempty" yaml:"security,omitempty"`
	Tags                openapi3.Tags                  `json:"tags,omitempty" yaml:"tags,omitempty"`
}

// MarshalJSON returns the JSON encoding of T.
func (doc T) MarshalJSON() ([]byte, error) {
	m := make(map[string]interface{}, 15+len(doc.Extensions))
	for k, v := range doc.Extensions {
		m[k] = v
	}
	m["swagger"] = doc.Swagger
	m["info"] = doc.Info
	if x := doc.ExternalDocs; x != nil {
		m["externalDocs"] = x
	}
	if x := doc.Schemes; len(x) != 0 {
		m["schemes"] = x
	}
	if x := doc.Consumes; len(x) != 0 {
		m["consumes"] = x
	}
	if x := doc.Produces; len(x) != 0 {
		m["produces"] = x
	}
	if x := doc.Host; x != "" {
		m["host"] = x
	}
	if x := doc.BasePath; x != "" {
		m["basePath"] = x
	}
	if x := doc.Paths; len(x) != 0 {
		m["paths"] = x
	}
	if x := doc.Definitions; len(x) != 0 {
		m["definitions"] = x
	}
	if x := doc.Parameters; len(x) != 0 {
		m["parameters"] = x
	}
	if x := doc.Responses; len(x) != 0 {
		m["responses"] = x
	}
	if x := doc.SecurityDefinitions; len(x) != 0 {
		m["securityDefinitions"] = x
	}
	if x := doc.Security; len(x) != 0 {
		m["security"] = x
	}
	if x := doc.Tags; len(x) != 0 {
		m["tags"] = x
	}
	return json.Marshal(m)
}

// UnmarshalJSON sets T to a copy of data.
func (doc *T) UnmarshalJSON(data []byte) error {
	type TBis T
	var x TBis
	if err := json.Unmarshal(data, &x); err != nil {
		return unmarshalError(err)
	}
	_ = json.Unmarshal(data, &x.Extensions)
	delete(x.Extensions, "swagger")
	delete(x.Extensions, "info")
	delete(x.Extensions, "externalDocs")
	delete(x.Extensions, "schemes")
	delete(x.Extensions, "consumes")
	delete(x.Extensions, "produces")
	delete(x.Extensions, "host")
	delete(x.Extensions, "basePath")
	delete(x.Extensions, "paths")
	delete(x.Extensions, "definitions")
	delete(x.Extensions, "parameters")
	delete(x.Extensions, "responses")
	delete(x.Extensions, "securityDefinitions")
	delete(x.Extensions, "security")
	delete(x.Extensions, "tags")
	if len(x.Extensions) == 0 {
		x.Extensions = nil
	}
	*doc = T(x)
	return nil
}

func (doc *T) AddOperation(path string, method string, operation *Operation) {
	if doc.Paths == nil {
		doc.Paths = make(map[string]*PathItem)
	}
	pathItem := doc.Paths[path]
	if pathItem == nil {
		pathItem = &PathItem{}
		doc.Paths[path] = pathItem
	}
	pathItem.SetOperation(method, operation)
}