Files returns the OpenAPI v3 specification files in JSON and YAML formats.
(root *expr.RootExpr)
| 13 | |
| 14 | // Files returns the OpenAPI v3 specification files in JSON and YAML formats. |
| 15 | func Files(root *expr.RootExpr) ([]*codegen.File, error) { |
| 16 | spec := New(root) |
| 17 | jsonSection := &codegen.SectionTemplate{ |
| 18 | Name: "openapi_v3", |
| 19 | FuncMap: template.FuncMap{"toJSON": toJSON(root.API.Meta)}, |
| 20 | Source: "{{ toJSON .}}", |
| 21 | Data: spec, |
| 22 | } |
| 23 | yamlSection := &codegen.SectionTemplate{ |
| 24 | Name: "openapi_v3", |
| 25 | FuncMap: template.FuncMap{"toYAML": toYAML}, |
| 26 | Source: "{{ toYAML .}}", |
| 27 | Data: spec, |
| 28 | } |
| 29 | |
| 30 | return []*codegen.File{ |
| 31 | { |
| 32 | Path: filepath.Join(codegen.Gendir, "http", "openapi3.json"), |
| 33 | SectionTemplates: []*codegen.SectionTemplate{jsonSection}, |
| 34 | }, |
| 35 | { |
| 36 | Path: filepath.Join(codegen.Gendir, "http", "openapi3.yaml"), |
| 37 | SectionTemplates: []*codegen.SectionTemplate{yamlSection}, |
| 38 | }, |
| 39 | }, nil |
| 40 | } |
| 41 | |
| 42 | func toJSON(meta expr.MetaExpr) func(any) string { |
| 43 | prefix, p := meta.Last("openapi:json:prefix") |