From 44c3853e6bb7832cf3cd56fe08a20dd582a41704 Mon Sep 17 00:00:00 2001 From: Bogdan U Date: Thu, 3 Feb 2022 20:52:44 +0200 Subject: [PATCH] feat: add example parameter (#1122) --- operation.go | 13 ++++++++++ operation_test.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/operation.go b/operation.go index 54c19ba01..642df7777 100644 --- a/operation.go +++ b/operation.go @@ -391,6 +391,8 @@ var regexAttributes = map[string]*regexp.Regexp{ extensionsTag: regexp.MustCompile(`(?i)\s+extensions\(.*\)`), // for collectionFormat(csv) collectionFormatTag: regexp.MustCompile(`(?i)\s+collectionFormat\(.*\)`), + // example(0) + exampleTag: regexp.MustCompile(`(?i)\s+example\(.*\)`), } func (operation *Operation) parseAndExtractionParamAttribute(commentLine, objectType, schemaType string, param *spec.Parameter) error { @@ -411,6 +413,8 @@ func (operation *Operation) parseAndExtractionParamAttribute(commentLine, object err = setStringParam(param, attrKey, schemaType, attr, commentLine) case formatTag: param.Format = attr + case exampleTag: + err = setExample(param, schemaType, attr) case extensionsTag: _ = setExtensionParam(param, attr) case collectionFormatTag: @@ -526,6 +530,15 @@ func setDefault(param *spec.Parameter, schemaType string, value string) error { return nil } +func setExample(param *spec.Parameter, schemaType string, value string) error { + val, err := defineType(schemaType, value) + if err != nil { + return nil // Don't set a example value if it's not valid + } + param.Example = val + return nil +} + // defineType enum value define the type (object and array unsupported). func defineType(schemaType string, value string) (v interface{}, err error) { schemaType = TransToValidSchemeType(schemaType) diff --git a/operation_test.go b/operation_test.go index 3976896cf..8eb89b5f0 100644 --- a/operation_test.go +++ b/operation_test.go @@ -1821,6 +1821,67 @@ func TestParseParamCommentByDefault(t *testing.T) { assert.Equal(t, expected, string(b)) } +func TestParseParamCommentByExampleInt(t *testing.T) { + t.Parallel() + + comment := `@Param some_id query int true "Some ID" Example(10)` + operation := NewOperation(nil) + err := operation.ParseComment(comment, nil) + + assert.NoError(t, err) + b, _ := json.MarshalIndent(operation.Parameters, "", " ") + expected := `[ + { + "type": "integer", + "example": 10, + "description": "Some ID", + "name": "some_id", + "in": "query", + "required": true + } +]` + assert.Equal(t, expected, string(b)) +} + +func TestParseParamCommentByExampleString(t *testing.T) { + t.Parallel() + + comment := `@Param some_id query string true "Some ID" Example(True feelings)` + operation := NewOperation(nil) + err := operation.ParseComment(comment, nil) + + assert.NoError(t, err) + b, _ := json.MarshalIndent(operation.Parameters, "", " ") + expected := `[ + { + "type": "string", + "example": "True feelings", + "description": "Some ID", + "name": "some_id", + "in": "query", + "required": true + } +]` + assert.Equal(t, expected, string(b)) +} + +func TestParseParamCommentByExampleUnsupportedType(t *testing.T) { + t.Parallel() + var param spec.Parameter + + setExample(¶m, "something", "random value") + assert.Equal(t, param.Example, nil) + + setExample(¶m, STRING, "string value") + assert.Equal(t, param.Example, "string value") + + setExample(¶m, INTEGER, "10") + assert.Equal(t, param.Example, 10) + + setExample(¶m, NUMBER, "10") + assert.Equal(t, param.Example, float64(10)) +} + func TestParseParamArrayWithEnums(t *testing.T) { t.Parallel()