1// Copyright 2017 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package main
16
17import (
18	v2 "github.com/googleapis/gnostic/openapiv2"
19)
20
21func buildDocumentV2() *v2.Document {
22	d := &v2.Document{}
23	d.Swagger = "2.0"
24	d.Info = &v2.Info{
25		Title:   "Swagger Petstore",
26		Version: "1.0.0",
27		License: &v2.License{Name: "MIT"},
28	}
29	d.Host = "petstore.swagger.io"
30	d.BasePath = "/v1"
31	d.Schemes = []string{"http"}
32	d.Consumes = []string{"application/json"}
33	d.Produces = []string{"application/json"}
34	d.Paths = &v2.Paths{}
35	d.Paths.Path = append(d.Paths.Path,
36		&v2.NamedPathItem{
37			Name: "/pets",
38			Value: &v2.PathItem{
39				Get: &v2.Operation{
40					Summary:     "List all pets",
41					OperationId: "listPets",
42					Tags:        []string{"pets"},
43					Parameters: []*v2.ParametersItem{
44						&v2.ParametersItem{
45							Oneof: &v2.ParametersItem_Parameter{
46								Parameter: &v2.Parameter{
47									Oneof: &v2.Parameter_NonBodyParameter{
48										NonBodyParameter: &v2.NonBodyParameter{
49											Oneof: &v2.NonBodyParameter_QueryParameterSubSchema{
50												QueryParameterSubSchema: &v2.QueryParameterSubSchema{
51													Name:        "limit",
52													In:          "query",
53													Description: "How many items to return at one time (max 100)",
54													Required:    false,
55													Type:        "integer",
56													Format:      "int32",
57												},
58											},
59										},
60									},
61								},
62							},
63						},
64					},
65					Responses: &v2.Responses{
66						ResponseCode: []*v2.NamedResponseValue{
67							&v2.NamedResponseValue{
68								Name: "200",
69								Value: &v2.ResponseValue{
70									Oneof: &v2.ResponseValue_Response{
71										Response: &v2.Response{
72											Description: "An paged array of pets", // [sic] match other examples
73											Schema: &v2.SchemaItem{
74												Oneof: &v2.SchemaItem_Schema{
75													Schema: &v2.Schema{
76														XRef: "#/definitions/Pets",
77													},
78												},
79											},
80											Headers: &v2.Headers{
81												AdditionalProperties: []*v2.NamedHeader{
82													&v2.NamedHeader{
83														Name: "x-next",
84														Value: &v2.Header{
85															Type:        "string",
86															Description: "A link to the next page of responses",
87														},
88													},
89												},
90											},
91										},
92									},
93								},
94							},
95							&v2.NamedResponseValue{
96								Name: "default",
97								Value: &v2.ResponseValue{
98									Oneof: &v2.ResponseValue_Response{
99										Response: &v2.Response{
100											Description: "unexpected error",
101											Schema: &v2.SchemaItem{
102												Oneof: &v2.SchemaItem_Schema{
103													Schema: &v2.Schema{
104														XRef: "#/definitions/Error",
105													},
106												},
107											},
108										},
109									},
110								},
111							},
112						},
113					},
114				},
115				Post: &v2.Operation{
116					Summary:     "Create a pet",
117					OperationId: "createPets",
118					Tags:        []string{"pets"},
119					Parameters:  []*v2.ParametersItem{},
120					Responses: &v2.Responses{
121						ResponseCode: []*v2.NamedResponseValue{
122							&v2.NamedResponseValue{
123								Name: "201",
124								Value: &v2.ResponseValue{
125									Oneof: &v2.ResponseValue_Response{
126										Response: &v2.Response{
127											Description: "Null response",
128										},
129									},
130								},
131							},
132							&v2.NamedResponseValue{
133								Name: "default",
134								Value: &v2.ResponseValue{
135									Oneof: &v2.ResponseValue_Response{
136										Response: &v2.Response{
137											Description: "unexpected error",
138											Schema: &v2.SchemaItem{
139												Oneof: &v2.SchemaItem_Schema{
140													Schema: &v2.Schema{
141														XRef: "#/definitions/Error",
142													},
143												},
144											},
145										},
146									},
147								},
148							},
149						},
150					},
151				},
152			}})
153	d.Paths.Path = append(d.Paths.Path,
154		&v2.NamedPathItem{
155			Name: "/pets/{petId}",
156			Value: &v2.PathItem{
157				Get: &v2.Operation{
158					Summary:     "Info for a specific pet",
159					OperationId: "showPetById",
160					Tags:        []string{"pets"},
161					Parameters: []*v2.ParametersItem{
162						&v2.ParametersItem{
163							Oneof: &v2.ParametersItem_Parameter{
164								Parameter: &v2.Parameter{
165									Oneof: &v2.Parameter_NonBodyParameter{
166										NonBodyParameter: &v2.NonBodyParameter{
167											Oneof: &v2.NonBodyParameter_PathParameterSubSchema{
168												PathParameterSubSchema: &v2.PathParameterSubSchema{
169													Name:        "petId",
170													In:          "path",
171													Description: "The id of the pet to retrieve",
172													Required:    true,
173													Type:        "string",
174												},
175											},
176										},
177									},
178								},
179							},
180						},
181					},
182					Responses: &v2.Responses{
183						ResponseCode: []*v2.NamedResponseValue{
184							&v2.NamedResponseValue{
185								Name: "200",
186								Value: &v2.ResponseValue{
187									Oneof: &v2.ResponseValue_Response{
188										Response: &v2.Response{
189											Description: "Expected response to a valid request",
190											Schema: &v2.SchemaItem{
191												Oneof: &v2.SchemaItem_Schema{
192													Schema: &v2.Schema{
193														XRef: "#/definitions/Pets",
194													},
195												},
196											},
197										},
198									},
199								},
200							},
201							&v2.NamedResponseValue{
202								Name: "default",
203								Value: &v2.ResponseValue{
204									Oneof: &v2.ResponseValue_Response{
205										Response: &v2.Response{
206											Description: "unexpected error",
207											Schema: &v2.SchemaItem{
208												Oneof: &v2.SchemaItem_Schema{
209													Schema: &v2.Schema{
210														XRef: "#/definitions/Error",
211													},
212												},
213											},
214										},
215									},
216								},
217							},
218						},
219					},
220				},
221			}})
222	d.Definitions = &v2.Definitions{}
223	d.Definitions.AdditionalProperties = append(d.Definitions.AdditionalProperties,
224		&v2.NamedSchema{
225			Name: "Pet",
226			Value: &v2.Schema{
227				Required: []string{"id", "name"},
228				Properties: &v2.Properties{
229					AdditionalProperties: []*v2.NamedSchema{
230						&v2.NamedSchema{Name: "id", Value: &v2.Schema{
231							Type:   &v2.TypeItem{Value: []string{"integer"}},
232							Format: "int64"}},
233						&v2.NamedSchema{Name: "name", Value: &v2.Schema{Type: &v2.TypeItem{Value: []string{"string"}}}},
234						&v2.NamedSchema{Name: "tag", Value: &v2.Schema{Type: &v2.TypeItem{Value: []string{"string"}}}},
235					},
236				},
237			}})
238	d.Definitions.AdditionalProperties = append(d.Definitions.AdditionalProperties,
239		&v2.NamedSchema{
240			Name: "Pets",
241			Value: &v2.Schema{
242				Type:  &v2.TypeItem{Value: []string{"array"}},
243				Items: &v2.ItemsItem{Schema: []*v2.Schema{&v2.Schema{XRef: "#/definitions/Pet"}}},
244			}})
245	d.Definitions.AdditionalProperties = append(d.Definitions.AdditionalProperties,
246		&v2.NamedSchema{
247			Name: "Error",
248			Value: &v2.Schema{
249				Required: []string{"code", "message"},
250				Properties: &v2.Properties{
251					AdditionalProperties: []*v2.NamedSchema{
252						&v2.NamedSchema{Name: "code", Value: &v2.Schema{
253							Type:   &v2.TypeItem{Value: []string{"integer"}},
254							Format: "int32"}},
255						&v2.NamedSchema{Name: "message", Value: &v2.Schema{Type: &v2.TypeItem{Value:[]string{"string"}}}},
256					},
257				},
258			}})
259	return d
260}
261