This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
/
default_openapi_spec.go
100 lines (94 loc) · 2.7 KB
/
default_openapi_spec.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
package converter
import (
"github.com/getkin/kin-openapi/openapi3"
"github.com/golang/protobuf/proto"
)
// defaultSpec creates a new OpenAPI3 document to fill in with schemas and paths:
func (c *Converter) defaultSpec() {
// Spec ready to take schemas:
c.openAPISpec = &openapi3.T{
Components: openapi3.Components{
RequestBodies: make(map[string]*openapi3.RequestBodyRef),
Responses: make(map[string]*openapi3.ResponseRef),
SecuritySchemes: make(map[string]*openapi3.SecuritySchemeRef),
Schemas: make(map[string]*openapi3.SchemaRef),
},
Info: &openapi3.Info{
Title: "M3O API",
Description: "Generated by protoc-gen-openapi",
Version: "1",
ExtensionProps: openapi3.ExtensionProps{
Extensions: map[string]interface{}{
"x-logo": map[string]string{
"url": "https://raw.githubusercontent.com/m3o/m3o/main/images/logo.png",
"backgroundColor": "#000000",
"altText": "M3O logo",
},
},
},
},
OpenAPI: "3.0.0",
Paths: make(openapi3.Paths),
}
// Add the LIVE platform server:
c.openAPISpec.AddServer(
&openapi3.Server{
URL: "https://api.m3o.com/v1",
Description: "M3O platform",
},
)
// Add the Micro auth mechanism:
c.openAPISpec.Components.SecuritySchemes["M3OAPIToken"] = &openapi3.SecuritySchemeRef{
Value: &openapi3.SecurityScheme{
BearerFormat: "token",
Description: "M3O API token",
Type: "http",
Scheme: "bearer",
},
}
// Add a default Micro error schema:
c.openAPISpec.Components.Responses["MicroAPIError"] = &openapi3.ResponseRef{
Value: &openapi3.Response{
Content: openapi3.Content{
"application/json": &openapi3.MediaType{
Schema: &openapi3.SchemaRef{
Value: &openapi3.Schema{
Type: openAPITypeObject,
Title: "MicroAPIError",
Properties: map[string]*openapi3.SchemaRef{
"Id": {
Value: &openapi3.Schema{
Description: "Error ID",
Type: openAPITypeString,
},
},
"Code": {
Value: &openapi3.Schema{
Description: "Error code",
Example: 500,
Type: openAPITypeNumber,
},
},
"Detail": {
Value: &openapi3.Schema{
Description: "Error detail",
Example: "service not found",
Type: openAPITypeString,
},
},
"Status": {
Value: &openapi3.Schema{
Description: "Error status message",
Example: "Internal Server Error",
Type: openAPITypeString,
},
},
},
},
},
},
},
Description: proto.String("Error from the Micro API"),
},
}
}