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	v3 "github.com/googleapis/gnostic/OpenAPIv3"
19)
20
21func buildDocumentV3() *v3.Document {
22	d := &v3.Document{}
23	d.Openapi = "3.0"
24	d.Info = &v3.Info{
25		Title:   "OpenAPI Petstore",
26		Version: "1.0.0",
27		License: &v3.License{Name: "MIT"},
28	}
29	d.Servers = append(d.Servers, &v3.Server{
30		Url:         "https://petstore.openapis.org/v1",
31		Description: "Development server",
32	})
33	d.Paths = &v3.Paths{}
34	d.Paths.Path = append(d.Paths.Path,
35		&v3.NamedPathItem{
36			Name: "/pets",
37			Value: &v3.PathItem{
38				Get: &v3.Operation{
39					Summary:     "List all pets",
40					OperationId: "listPets",
41					Tags:        []string{"pets"},
42					Parameters: []*v3.ParameterOrReference{
43						&v3.ParameterOrReference{
44							Oneof: &v3.ParameterOrReference_Parameter{
45								Parameter: &v3.Parameter{
46									Name:        "limit",
47									In:          "query",
48									Description: "How many items to return at one time (max 100)",
49									Required:    false,
50									Schema: &v3.SchemaOrReference{
51										Oneof: &v3.SchemaOrReference_Schema{
52											Schema: &v3.Schema{
53												Type:   "integer",
54												Format: "int32",
55											},
56										},
57									},
58								},
59							},
60						},
61					},
62					Responses: &v3.Responses{
63						Default: &v3.ResponseOrReference{
64							Oneof: &v3.ResponseOrReference_Response{
65								Response: &v3.Response{
66									Description: "unexpected error",
67									Content: &v3.MediaTypes{
68										AdditionalProperties: []*v3.NamedMediaType{
69											&v3.NamedMediaType{
70												Name: "application/json",
71												Value: &v3.MediaType{
72													Schema: &v3.SchemaOrReference{
73														Oneof: &v3.SchemaOrReference_Reference{
74															Reference: &v3.Reference{
75																XRef: "#/components/schemas/Error",
76															},
77														},
78													},
79												},
80											},
81										},
82									},
83								},
84							},
85						},
86						ResponseOrReference: []*v3.NamedResponseOrReference{
87							&v3.NamedResponseOrReference{
88								Name: "200",
89								Value: &v3.ResponseOrReference{
90									Oneof: &v3.ResponseOrReference_Response{
91										Response: &v3.Response{
92											Description: "An paged array of pets", // [sic] match other examples
93											Content: &v3.MediaTypes{
94												AdditionalProperties: []*v3.NamedMediaType{
95													&v3.NamedMediaType{
96														Name: "application/json",
97														Value: &v3.MediaType{
98															Schema: &v3.SchemaOrReference{
99																Oneof: &v3.SchemaOrReference_Reference{
100																	Reference: &v3.Reference{
101																		XRef: "#/components/schemas/Pets",
102																	},
103																},
104															},
105														},
106													},
107												},
108											},
109											Headers: &v3.HeadersOrReferences{
110												AdditionalProperties: []*v3.NamedHeaderOrReference{
111													&v3.NamedHeaderOrReference{
112														Name: "x-next",
113														Value: &v3.HeaderOrReference{
114															Oneof: &v3.HeaderOrReference_Header{
115																Header: &v3.Header{
116																	Description: "A link to the next page of responses",
117																	Schema: &v3.SchemaOrReference{
118																		Oneof: &v3.SchemaOrReference_Schema{
119																			Schema: &v3.Schema{
120																				Type: "string",
121																			},
122																		},
123																	},
124																},
125															},
126														},
127													},
128												},
129											},
130										},
131									},
132								},
133							},
134						},
135					},
136				},
137				Post: &v3.Operation{
138					Summary:     "Create a pet",
139					OperationId: "createPets",
140					Tags:        []string{"pets"},
141					Responses: &v3.Responses{
142						Default: &v3.ResponseOrReference{
143							Oneof: &v3.ResponseOrReference_Response{
144								Response: &v3.Response{
145									Description: "unexpected error",
146									Content: &v3.MediaTypes{
147										AdditionalProperties: []*v3.NamedMediaType{
148											&v3.NamedMediaType{
149												Name: "application/json",
150												Value: &v3.MediaType{
151													Schema: &v3.SchemaOrReference{
152														Oneof: &v3.SchemaOrReference_Reference{
153															Reference: &v3.Reference{
154																XRef: "#/components/schemas/Error",
155															},
156														},
157													},
158												},
159											},
160										},
161									},
162								},
163							},
164						},
165						ResponseOrReference: []*v3.NamedResponseOrReference{
166							&v3.NamedResponseOrReference{
167								Name: "201",
168								Value: &v3.ResponseOrReference{
169									Oneof: &v3.ResponseOrReference_Response{
170										Response: &v3.Response{
171											Description: "Null response",
172										},
173									},
174								},
175							},
176						},
177					},
178				},
179			}},
180		&v3.NamedPathItem{
181			Name: "/pets/{petId}",
182			Value: &v3.PathItem{
183				Get: &v3.Operation{
184					Summary:     "Info for a specific pet",
185					OperationId: "showPetById",
186					Tags:        []string{"pets"},
187					Parameters: []*v3.ParameterOrReference{
188						&v3.ParameterOrReference{
189							Oneof: &v3.ParameterOrReference_Parameter{
190								Parameter: &v3.Parameter{
191									Name:        "petId",
192									In:          "path",
193									Description: "The id of the pet to retrieve",
194									Required:    true,
195									Schema: &v3.SchemaOrReference{
196										Oneof: &v3.SchemaOrReference_Schema{
197											Schema: &v3.Schema{
198												Type: "string",
199											},
200										},
201									},
202								},
203							},
204						},
205					},
206					Responses: &v3.Responses{
207						Default: &v3.ResponseOrReference{
208							Oneof: &v3.ResponseOrReference_Response{
209								Response: &v3.Response{
210									Description: "unexpected error",
211									Content: &v3.MediaTypes{
212										AdditionalProperties: []*v3.NamedMediaType{
213											&v3.NamedMediaType{
214												Name: "application/json",
215												Value: &v3.MediaType{
216													Schema: &v3.SchemaOrReference{
217														Oneof: &v3.SchemaOrReference_Reference{
218															Reference: &v3.Reference{
219																XRef: "#/components/schemas/Error",
220															},
221														},
222													},
223												},
224											},
225										},
226									},
227								},
228							},
229						},
230						ResponseOrReference: []*v3.NamedResponseOrReference{
231							&v3.NamedResponseOrReference{
232								Name: "200",
233								Value: &v3.ResponseOrReference{
234									Oneof: &v3.ResponseOrReference_Response{
235										Response: &v3.Response{
236											Description: "Expected response to a valid request",
237											Content: &v3.MediaTypes{
238												AdditionalProperties: []*v3.NamedMediaType{
239													&v3.NamedMediaType{
240														Name: "application/json",
241														Value: &v3.MediaType{
242															Schema: &v3.SchemaOrReference{
243																Oneof: &v3.SchemaOrReference_Reference{
244																	Reference: &v3.Reference{
245																		XRef: "#/components/schemas/Pets",
246																	},
247																},
248															},
249														},
250													},
251												},
252											},
253										},
254									},
255								},
256							},
257						},
258					},
259				},
260			}})
261	d.Components = &v3.Components{
262		Schemas: &v3.SchemasOrReferences{
263			AdditionalProperties: []*v3.NamedSchemaOrReference{
264				&v3.NamedSchemaOrReference{
265					Name: "Pet",
266					Value: &v3.SchemaOrReference{
267						Oneof: &v3.SchemaOrReference_Schema{
268							Schema: &v3.Schema{
269								Required: []string{"id", "name"},
270								Properties: &v3.Properties{
271									AdditionalProperties: []*v3.NamedSchemaOrReference{
272										&v3.NamedSchemaOrReference{
273											Name: "id",
274											Value: &v3.SchemaOrReference{
275												Oneof: &v3.SchemaOrReference_Schema{
276													Schema: &v3.Schema{
277														Type:   "integer",
278														Format: "int64",
279													},
280												},
281											},
282										},
283										&v3.NamedSchemaOrReference{
284											Name: "name",
285											Value: &v3.SchemaOrReference{
286												Oneof: &v3.SchemaOrReference_Schema{
287													Schema: &v3.Schema{
288														Type: "string",
289													},
290												},
291											},
292										},
293										&v3.NamedSchemaOrReference{
294											Name: "tag",
295											Value: &v3.SchemaOrReference{
296												Oneof: &v3.SchemaOrReference_Schema{
297													Schema: &v3.Schema{
298														Type: "string",
299													},
300												},
301											},
302										},
303									},
304								},
305							},
306						},
307					},
308				},
309				&v3.NamedSchemaOrReference{
310					Name: "Pets",
311					Value: &v3.SchemaOrReference{
312						Oneof: &v3.SchemaOrReference_Schema{
313							Schema: &v3.Schema{
314								Type: "array",
315								Items: &v3.ItemsItem{
316									SchemaOrReference: []*v3.SchemaOrReference{
317										&v3.SchemaOrReference{
318											Oneof: &v3.SchemaOrReference_Reference{
319												Reference: &v3.Reference{
320													XRef: "#/components/schemas/Pet",
321												},
322											},
323										},
324									},
325								},
326							},
327						},
328					},
329				},
330				&v3.NamedSchemaOrReference{
331					Name: "Error",
332					Value: &v3.SchemaOrReference{
333						Oneof: &v3.SchemaOrReference_Schema{
334							Schema: &v3.Schema{
335								Required: []string{"code", "message"},
336								Properties: &v3.Properties{
337									AdditionalProperties: []*v3.NamedSchemaOrReference{
338										&v3.NamedSchemaOrReference{
339											Name: "code",
340											Value: &v3.SchemaOrReference{
341												Oneof: &v3.SchemaOrReference_Schema{
342													Schema: &v3.Schema{
343														Type:   "integer",
344														Format: "int32",
345													},
346												},
347											},
348										},
349										&v3.NamedSchemaOrReference{
350											Name: "message",
351											Value: &v3.SchemaOrReference{
352												Oneof: &v3.SchemaOrReference_Schema{
353													Schema: &v3.Schema{
354														Type: "string",
355													},
356												},
357											},
358										},
359									},
360								},
361							},
362						},
363					},
364				},
365			},
366		},
367	}
368	return d
369}
370