Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add openapi:json:indent Meta #3480

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions dsl/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,22 @@ import (
// Meta("openapi:generate", "false")
// })
//
// - "openapi:json:prefix" specifies the prefix used to format the OpenAPI
// specification encoded in JSON. It can be used with "openapi:json:indent".
// Applicable to API only.
//
// var _ = API("MyAPI", func() {
// Meta("openapi:json:prefix", " ")
// })
//
// - "openapi:json:indent" specifies the indent used to format the OpenAPI
// specification encoded in JSON. It can be used with "openapi:json:prefix".
// Applicable to API only.
//
// var _ = API("MyAPI", func() {
// Meta("openapi:json:indent", " ")
// })
//
// - "swagger:summary" DEPRECATED, use "openapi:summary" instead
//
// - "openapi:summary" sets the OpenAPI operation summary field. The special
Expand Down
22 changes: 16 additions & 6 deletions http/codegen/openapi/v2/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func Files(root *expr.RootExpr) ([]*codegen.File, error) {
}
jsonSection := &codegen.SectionTemplate{
Name: "openapi",
FuncMap: template.FuncMap{"toJSON": toJSON},
FuncMap: template.FuncMap{"toJSON": toJSON(root.API.Meta)},
Source: "{{ toJSON .}}",
Data: spec,
}
Expand All @@ -41,12 +41,22 @@ func Files(root *expr.RootExpr) ([]*codegen.File, error) {
}, nil
}

func toJSON(d any) string {
b, err := json.Marshal(d)
if err != nil {
panic("openapi: " + err.Error()) // bug
func toJSON(meta expr.MetaExpr) func(any) string {
prefix, p := meta.Last("openapi:json:prefix")
indent, i := meta.Last("openapi:json:indent")
marshal := json.Marshal
if p || i {
marshal = func(v any) ([]byte, error) {
return json.MarshalIndent(v, prefix, indent)
}
}
return func(d any) string {
b, err := marshal(d)
if err != nil {
panic("openapi: " + err.Error()) // bug
}
return string(b)
}
return string(b)
}

func toYAML(d any) string {
Expand Down
3 changes: 3 additions & 0 deletions http/codegen/openapi/v2/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ func TestSections(t *testing.T) {
{"not-generate-server", testdata.NotGenerateServerDSL},
{"not-generate-host", testdata.NotGenerateHostDSL},
{"not-generate-attribute", testdata.NotGenerateAttributeDSL},
{"json-prefix", testdata.JSONPrefixDSL},
{"json-indent", testdata.JSONIndentDSL},
{"json-prefix-indent", testdata.JSONPrefixIndentDSL},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"swagger": "2.0",
"info": {
"title": "",
"version": "0.0.1"
},
"host": "goa.design",
"consumes": [
"application/json",
"application/xml",
"application/gob"
],
"produces": [
"application/json",
"application/xml",
"application/gob"
],
"paths": {
"/path1": {
"get": {
"tags": [
"service-name"
],
"summary": "Download filename",
"operationId": "service-name#/path1",
"responses": {
"200": {
"description": "File downloaded",
"schema": {
"type": "file"
}
}
},
"schemes": [
"https"
]
}
},
"/path2": {
"get": {
"tags": [
"user-tag"
],
"summary": "Download filename",
"operationId": "service-name#/path2",
"responses": {
"200": {
"description": "File downloaded",
"schema": {
"type": "file"
}
}
},
"schemes": [
"https"
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
swagger: "2.0"
info:
title: ""
version: 0.0.1
host: goa.design
consumes:
- application/json
- application/xml
- application/gob
produces:
- application/json
- application/xml
- application/gob
paths:
/path1:
get:
tags:
- service-name
summary: Download filename
operationId: service-name#/path1
responses:
"200":
description: File downloaded
schema:
type: file
schemes:
- https
/path2:
get:
tags:
- user-tag
summary: Download filename
operationId: service-name#/path2
responses:
"200":
description: File downloaded
schema:
type: file
schemes:
- https
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"swagger": "2.0",
"info": {
"title": "",
"version": "0.0.1"
},
"host": "goa.design",
"consumes": [
"application/json",
"application/xml",
"application/gob"
],
"produces": [
"application/json",
"application/xml",
"application/gob"
],
"paths": {
"/path1": {
"get": {
"tags": [
"service-name"
],
"summary": "Download filename",
"operationId": "service-name#/path1",
"responses": {
"200": {
"description": "File downloaded",
"schema": {
"type": "file"
}
}
},
"schemes": [
"https"
]
}
},
"/path2": {
"get": {
"tags": [
"user-tag"
],
"summary": "Download filename",
"operationId": "service-name#/path2",
"responses": {
"200": {
"description": "File downloaded",
"schema": {
"type": "file"
}
}
},
"schemes": [
"https"
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
swagger: "2.0"
info:
title: ""
version: 0.0.1
host: goa.design
consumes:
- application/json
- application/xml
- application/gob
produces:
- application/json
- application/xml
- application/gob
paths:
/path1:
get:
tags:
- service-name
summary: Download filename
operationId: service-name#/path1
responses:
"200":
description: File downloaded
schema:
type: file
schemes:
- https
/path2:
get:
tags:
- user-tag
summary: Download filename
operationId: service-name#/path2
responses:
"200":
description: File downloaded
schema:
type: file
schemes:
- https
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"swagger": "2.0",
"info": {
"title": "",
"version": "0.0.1"
},
"host": "goa.design",
"consumes": [
"application/json",
"application/xml",
"application/gob"
],
"produces": [
"application/json",
"application/xml",
"application/gob"
],
"paths": {
"/path1": {
"get": {
"tags": [
"service-name"
],
"summary": "Download filename",
"operationId": "service-name#/path1",
"responses": {
"200": {
"description": "File downloaded",
"schema": {
"type": "file"
}
}
},
"schemes": [
"https"
]
}
},
"/path2": {
"get": {
"tags": [
"user-tag"
],
"summary": "Download filename",
"operationId": "service-name#/path2",
"responses": {
"200": {
"description": "File downloaded",
"schema": {
"type": "file"
}
}
},
"schemes": [
"https"
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
swagger: "2.0"
info:
title: ""
version: 0.0.1
host: goa.design
consumes:
- application/json
- application/xml
- application/gob
produces:
- application/json
- application/xml
- application/gob
paths:
/path1:
get:
tags:
- service-name
summary: Download filename
operationId: service-name#/path1
responses:
"200":
description: File downloaded
schema:
type: file
schemes:
- https
/path2:
get:
tags:
- user-tag
summary: Download filename
operationId: service-name#/path2
responses:
"200":
description: File downloaded
schema:
type: file
schemes:
- https
Loading
Loading