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

feat: document args/options schemas, fixes #157 #212

Merged
merged 1 commit into from
Aug 4, 2023
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
35 changes: 35 additions & 0 deletions openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,19 @@ func getRequestInfo(op *v3.Operation) (string, *base.Schema, []interface{}) {
return "", nil, nil
}

// paramSchema returns a rendered schema line for a given parameter, falling
// back to the param type info if no schema is available.
func paramSchema(p *cli.Param, s *base.Schema) string {
schemaDesc := fmt.Sprintf("(%s): %s", p.Type, p.Description)
if s != nil {
schemaDesc = renderSchema(s, " ", modeWrite)
}
return schemaDesc
}

func openapiOperation(cmd *cobra.Command, method string, uriTemplate *url.URL, path *v3.PathItem, op *v3.Operation) cli.Operation {
var pathParams, queryParams, headerParams []*cli.Param
var pathSchemas, querySchemas, headerSchemas []*base.Schema = []*base.Schema{}, []*base.Schema{}, []*base.Schema{}

// Combine path and operation parameters, with operation params having
// precedence when there are name conflicts.
Expand All @@ -225,8 +236,10 @@ func openapiOperation(cmd *cobra.Command, method string, uriTemplate *url.URL, p
var example interface{}

typ := "string"
var schema *base.Schema
if p.Schema != nil && p.Schema.Schema() != nil {
s := p.Schema.Schema()
schema = s
if len(s.Type) > 0 {
// TODO: support params of multiple types?
typ = s.Type[0]
Expand Down Expand Up @@ -277,16 +290,19 @@ func openapiOperation(cmd *cobra.Command, method string, uriTemplate *url.URL, p
pathParams = []*cli.Param{}
}
pathParams = append(pathParams, param)
pathSchemas = append(pathSchemas, schema)
case "query":
if queryParams == nil {
queryParams = []*cli.Param{}
}
queryParams = append(queryParams, param)
querySchemas = append(querySchemas, schema)
case "header":
if headerParams == nil {
headerParams = []*cli.Param{}
}
headerParams = append(headerParams, param)
headerSchemas = append(headerSchemas, schema)
}
}

Expand All @@ -305,6 +321,25 @@ func openapiOperation(cmd *cobra.Command, method string, uriTemplate *url.URL, p
desc := getExt(op.Extensions, ExtDescription, op.Description)
hidden := getExt(op.Extensions, ExtHidden, false)

if len(pathParams) > 0 {
desc += "\n## Argument Schema:\n```schema\n{\n"
for i, p := range pathParams {
desc += " " + p.OptionName() + ": " + paramSchema(p, pathSchemas[i]) + "\n"
}
desc += "}\n```\n"
}

if len(queryParams) > 0 || len(headerParams) > 0 {
desc += "\n## Option Schema:\n```schema\n{\n"
for i, p := range queryParams {
desc += " --" + p.OptionName() + ": " + paramSchema(p, querySchemas[i]) + "\n"
}
for i, p := range headerParams {
desc += " --" + p.OptionName() + ": " + paramSchema(p, headerSchemas[i]) + "\n"
}
desc += "}\n```\n"
}

mediaType := ""
var examples []string
if op.RequestBody != nil {
Expand Down
16 changes: 16 additions & 0 deletions openapi/testdata/extensions/output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ operations:
- gi
short: ""
long: |
## Argument Schema:
```schema
{
item-id: (string)
}
```
## Option Schema:
```schema
{
--query: [
(string)
]
}
```
## Response 200 (application/json)
CLI-specific description override
Expand Down
7 changes: 7 additions & 0 deletions openapi/testdata/long_example/output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ operations:
aliases: []
short: ""
long: |
## Argument Schema:
```schema
{
item-id: (string)
}
```
## Input Example
```json
Expand Down
14 changes: 14 additions & 0 deletions openapi/testdata/petstore/output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ operations:
- listpets
short: List all pets
long: |
## Option Schema:
```schema
{
--limit: (integer format:int32)
}
```
## Response 200 (application/json)
A paged array of pets
Expand Down Expand Up @@ -66,6 +73,13 @@ operations:
- showpetbyid
short: Info for a specific pet
long: |
## Argument Schema:
```schema
{
pet-id: (string)
}
```
## Response 200 (application/json)
Expected response to a valid request
Expand Down
21 changes: 21 additions & 0 deletions openapi/testdata/request/output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ operations:
aliases: []
short: ""
long: |
## Argument Schema:
```schema
{
item-id: (string)
}
```
## Response 204
Response has no body
Expand All @@ -16,6 +23,20 @@ operations:
aliases: []
short: ""
long: |
## Argument Schema:
```schema
{
item-id: (string)
}
```
## Option Schema:
```schema
{
--my-header: (string)
}
```
## Input Example
```json
Expand Down
Loading