MCPcopy Create free account
hub / github.com/goadesign/goa / GenerateServiceDefinition

Function GenerateServiceDefinition

http/codegen/openapi/json_schema.go:168–224  ·  view source on GitHub ↗

GenerateServiceDefinition produces the JSON schema corresponding to the given service. It stores the results in Definitions.

(api *expr.APIExpr, res *expr.HTTPServiceExpr)

Source from the content-addressed store, hash-verified

166// GenerateServiceDefinition produces the JSON schema corresponding to the given
167// service. It stores the results in Definitions.
168func GenerateServiceDefinition(api *expr.APIExpr, res *expr.HTTPServiceExpr) {
169 s := NewSchema()
170 s.Description = res.Description()
171 s.Type = Object
172 s.Title = res.Name()
173 Definitions[res.Name()] = s
174 for _, a := range res.HTTPEndpoints {
175 var requestSchema *Schema
176 if a.MethodExpr.Payload.Type != expr.Empty {
177 requestSchema = AttributeTypeSchema(api, a.MethodExpr.Payload)
178 requestSchema.Description = a.Name() + " payload"
179 }
180 var targetSchema *Schema
181 var identifier string
182 for _, resp := range a.Responses {
183 dt := resp.Body.Type
184 if mt := dt.(*expr.ResultTypeExpr); mt != nil {
185 if identifier == "" {
186 identifier = mt.Identifier
187 } else {
188 identifier = ""
189 }
190 switch {
191 case targetSchema == nil:
192 targetSchema = TypeSchemaWithPrefix(api, mt, a.Name())
193 case targetSchema.AnyOf == nil:
194 firstSchema := targetSchema
195 targetSchema = NewSchema()
196 targetSchema.AnyOf = []*Schema{firstSchema, TypeSchemaWithPrefix(api, mt, a.Name())}
197 default:
198 targetSchema.AnyOf = append(targetSchema.AnyOf, TypeSchemaWithPrefix(api, mt, a.Name()))
199 }
200 }
201 }
202 for i, r := range a.Routes {
203 for j, href := range toSchemaHrefs(r) {
204 link := Link{
205 Title: a.Name(),
206 Rel: a.Name(),
207 Href: href,
208 Method: r.Method,
209 Schema: requestSchema,
210 TargetSchema: targetSchema,
211 ResultType: identifier,
212 }
213 if i == 0 && j == 0 {
214 if ca := a.Service.CanonicalEndpoint(); ca != nil {
215 if ca.Name() == a.Name() {
216 link.Rel = "self"
217 }
218 }
219 }
220 s.Links = append(s.Links, &link)
221 }
222 }
223 }
224}
225

Callers 1

APISchemaFunction · 0.85

Calls 7

NewSchemaFunction · 0.85
AttributeTypeSchemaFunction · 0.85
TypeSchemaWithPrefixFunction · 0.85
toSchemaHrefsFunction · 0.85
CanonicalEndpointMethod · 0.80
NameMethod · 0.65
DescriptionMethod · 0.45

Tested by

no test coverage detected