From fcdccb054bab97db6acf0793278382ceb7aaf80d Mon Sep 17 00:00:00 2001 From: Bojan Zivanovic Date: Fri, 11 Aug 2023 22:36:18 +0200 Subject: [PATCH] Start showing parameter defaults and examples in operation usage. --- cmd/broom/profile.go | 17 ++++++++++++++++- operation.go | 1 + spec.go | 2 ++ spec_test.go | 2 +- testdata/openapi3.yaml | 2 +- testdata/swagger.yaml | 2 +- 6 files changed, 22 insertions(+), 4 deletions(-) diff --git a/cmd/broom/profile.go b/cmd/broom/profile.go index f396d82..32012f3 100644 --- a/cmd/broom/profile.go +++ b/cmd/broom/profile.go @@ -160,9 +160,24 @@ func operationUsage(op broom.Operation, profile string) { // prepareParameterDescription prepares a parameter description for display. // +// Adds default and example values. // If a description has multiple lines, all lines are indented to match the first line's width. func prepareParameterDescription(p broom.Parameter) string { + values := make([]string, 0, 2) + if p.Default != nil { + values = append(values, fmt.Sprintf("%v %v", color.YellowString("Default:"), p.Default)) + } + if p.Example != nil { + values = append(values, fmt.Sprintf("%v %v", color.YellowString("Example:"), p.Example)) + } + + description := p.Description + if len(values) > 0 { + description = fmt.Sprintf("%s\n%s", description, strings.Join(values, " ")) + } // Since colors are used for the name column, tabwriter requires color codes to // be present even when that column is empty, for the tab width to be right. - return strings.ReplaceAll(p.Description, "\n", "\n\t"+color.GreenString("")+"\t") + description = strings.ReplaceAll(description, "\n", "\n\t"+color.GreenString("")+"\t") + + return description } diff --git a/operation.go b/operation.go index 663b72b..c6d9728 100644 --- a/operation.go +++ b/operation.go @@ -223,6 +223,7 @@ type Parameter struct { Style string Type string Enum []string + Example any Default any Deprecated bool Required bool diff --git a/spec.go b/spec.go index 1a26c10..ddc59cd 100644 --- a/spec.go +++ b/spec.go @@ -150,6 +150,7 @@ func newOperationFromSpec(method string, path string, params openapi3.Parameters Description: Sanitize(schema.Value.Description), Type: getSchemaType(*schema.Value), Enum: castEnum(schema.Value.Enum), + Example: schema.Value.Example, Default: schema.Value.Default, Deprecated: schema.Value.Deprecated, Required: required, @@ -171,6 +172,7 @@ func newParameterFromSpec(specParam openapi3.Parameter) Parameter { Style: specParam.Style, Type: getSchemaType(*specParam.Schema.Value), Enum: castEnum(specParam.Schema.Value.Enum), + Example: specParam.Schema.Value.Example, Default: specParam.Schema.Value.Default, Deprecated: specParam.Deprecated, Required: specParam.Required, diff --git a/spec_test.go b/spec_test.go index 7ca4e9c..f43c704 100644 --- a/spec_test.go +++ b/spec_test.go @@ -109,7 +109,7 @@ func TestLoadOperations(t *testing.T) { Name: "price", Description: "The product price, in cents.", Type: "integer", - Default: any(float64(1099)), + Example: any(float64(1099)), Required: true, }, broom.Parameter{ diff --git a/testdata/openapi3.yaml b/testdata/openapi3.yaml index 387a515..b384a41 100644 --- a/testdata/openapi3.yaml +++ b/testdata/openapi3.yaml @@ -100,7 +100,7 @@ paths: price: type: integer description: The product price, in cents. - default: 1099 + example: 1099 currency_code: type: string description: The currency code. diff --git a/testdata/swagger.yaml b/testdata/swagger.yaml index 1f3b36f..aa2a0a1 100644 --- a/testdata/swagger.yaml +++ b/testdata/swagger.yaml @@ -111,7 +111,7 @@ paths: price: type: integer description: The product price, in cents. - default: 1099 + example: 1099 currency_code: type: string description: The currency code.