From baa51fd2d4deab6217ed2c60f10ebec85ad9b66e Mon Sep 17 00:00:00 2001 From: Bogdan Ungureanu Date: Thu, 3 Feb 2022 20:36:22 +0200 Subject: [PATCH 1/2] feat: add example parameter --- operation.go | 13 +++++++++++++ operation_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/operation.go b/operation.go index 54c19ba01..e4e06c21d 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 default 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..cac6653e5 100644 --- a/operation_test.go +++ b/operation_test.go @@ -1821,6 +1821,50 @@ 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 TestParseParamArrayWithEnums(t *testing.T) { t.Parallel() From 00b8b7862441985ccc175a55f2d7d86bc3dde703 Mon Sep 17 00:00:00 2001 From: Bogdan Ungureanu Date: Thu, 3 Feb 2022 20:49:09 +0200 Subject: [PATCH 2/2] chore: fix unit tests --- operation.go | 2 +- operation_test.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/operation.go b/operation.go index e4e06c21d..642df7777 100644 --- a/operation.go +++ b/operation.go @@ -533,7 +533,7 @@ func setDefault(param *spec.Parameter, schemaType string, value string) error { func setExample(param *spec.Parameter, schemaType string, value string) error { val, err := defineType(schemaType, value) if err != nil { - return nil // Don't set a default value if it's not valid + return nil // Don't set a example value if it's not valid } param.Example = val return nil diff --git a/operation_test.go b/operation_test.go index cac6653e5..8eb89b5f0 100644 --- a/operation_test.go +++ b/operation_test.go @@ -1865,6 +1865,23 @@ func TestParseParamCommentByExampleString(t *testing.T) { 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()