buildOperation builds the OpenAPI Operation object for the given path.
(key string, r *expr.RouteExpr, bodies *EndpointBodies, rand *expr.ExampleGenerator, meta expr.MetaExpr)
| 190 | |
| 191 | // buildOperation builds the OpenAPI Operation object for the given path. |
| 192 | func buildOperation(key string, r *expr.RouteExpr, bodies *EndpointBodies, rand *expr.ExampleGenerator, meta expr.MetaExpr) *Operation { |
| 193 | e := r.Endpoint |
| 194 | m := e.MethodExpr |
| 195 | svc := e.Service |
| 196 | |
| 197 | // OpenAPI summary |
| 198 | var summary string |
| 199 | setSummary := func(meta expr.MetaExpr) { |
| 200 | for n, mdata := range meta { |
| 201 | if (n == "openapi:summary" || n == "swagger:summary") && len(mdata) > 0 { |
| 202 | if mdata[0] == "{path}" { |
| 203 | summary = r.Path |
| 204 | } else { |
| 205 | summary = mdata[0] |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | summary = fmt.Sprintf("%s %s", e.Name(), svc.Name()) |
| 212 | setSummary(meta) |
| 213 | setSummary(svc.ServiceExpr.Meta) |
| 214 | setSummary(e.Meta) |
| 215 | setSummary(m.Meta) |
| 216 | |
| 217 | // OpenAPI operationId |
| 218 | var operationIDFormat string |
| 219 | setOperationIDFormat := func(meta expr.MetaExpr) { |
| 220 | for n, mdata := range meta { |
| 221 | if (n == "openapi:operationId") && len(mdata) > 0 { |
| 222 | operationIDFormat = mdata[0] |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | operationIDFormat = defaultOperationIDFormat |
| 228 | setOperationIDFormat(meta) |
| 229 | setOperationIDFormat(m.Service.Meta) |
| 230 | setOperationIDFormat(e.Meta) |
| 231 | setOperationIDFormat(m.Meta) |
| 232 | |
| 233 | // request body |
| 234 | var requestBody *RequestBodyRef |
| 235 | if e.Body.Type != expr.Empty { |
| 236 | ct := "application/json" // TBD: need a way to specify method media type in design... |
| 237 | if e.MultipartRequest { |
| 238 | ct = "multipart/form-data" |
| 239 | } |
| 240 | mt := &MediaType{Schema: bodies.RequestBody} |
| 241 | initExamples(mt, e.Body, rand) |
| 242 | requestBody = &RequestBodyRef{Value: &RequestBody{ |
| 243 | Description: e.Body.Description, |
| 244 | Required: e.Body.Type != expr.Empty, |
| 245 | Content: map[string]*MediaType{ct: mt}, |
| 246 | Extensions: openapi.ExtensionsFromExpr(e.Body.Meta), |
| 247 | }} |
| 248 | } |
| 249 |