MCPcopy Create free account
hub / github.com/a-h/rest / createOpenAPI

Method createOpenAPI

schema.go:62–169  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

60}
61
62func (api *API) createOpenAPI() (spec *openapi3.T, err error) {
63 spec = newSpec(api.Name)
64 // Add all the routes.
65 for pattern, methodToRoute := range api.Routes {
66 path := &openapi3.PathItem{}
67 for method, route := range methodToRoute {
68 op := &openapi3.Operation{}
69
70 // Add the query params.
71 for _, k := range getSortedKeys(route.Params.Query) {
72 v := route.Params.Query[k]
73
74 ps := newPrimitiveSchema(v.Type).
75 WithPattern(v.Regexp)
76 queryParam := openapi3.NewQueryParameter(k).
77 WithDescription(v.Description).
78 WithSchema(ps)
79 queryParam.Required = v.Required
80 queryParam.AllowEmptyValue = v.AllowEmpty
81
82 // Apply schema customisation.
83 if v.ApplyCustomSchema != nil {
84 v.ApplyCustomSchema(queryParam)
85 }
86
87 op.AddParameter(queryParam)
88 }
89
90 // Add the route params.
91 for _, k := range getSortedKeys(route.Params.Path) {
92 v := route.Params.Path[k]
93
94 ps := newPrimitiveSchema(v.Type).
95 WithPattern(v.Regexp)
96 pathParam := openapi3.NewPathParameter(k).
97 WithDescription(v.Description).
98 WithSchema(ps)
99
100 // Apply schema customisation.
101 if v.ApplyCustomSchema != nil {
102 v.ApplyCustomSchema(pathParam)
103 }
104
105 op.AddParameter(pathParam)
106 }
107
108 // Handle request types.
109 if route.Models.Request.Type != nil {
110 name, schema, err := api.RegisterModel(route.Models.Request)
111 if err != nil {
112 return spec, err
113 }
114 op.RequestBody = &openapi3.RequestBodyRef{
115 Value: openapi3.NewRequestBody().WithContent(map[string]*openapi3.MediaType{
116 "application/json": {
117 Schema: getSchemaReferenceOrValue(name, schema),
118 },
119 }),

Callers 1

SpecMethod · 0.95

Calls 6

RegisterModelMethod · 0.95
newSpecFunction · 0.85
getSortedKeysFunction · 0.85
newPrimitiveSchemaFunction · 0.85
ApplyCustomSchemaMethod · 0.65

Tested by

no test coverage detected