Skip to content

Commit

Permalink
feat: Example/SchemaExample: allow \r \n \t
Browse files Browse the repository at this point in the history
  • Loading branch information
mschneider82 committed Mar 10, 2022
1 parent 334111b commit be03834
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
12 changes: 11 additions & 1 deletion operation.go
Expand Up @@ -535,6 +535,9 @@ func setDefault(param *spec.Parameter, schemaType string, value string) error {
return nil
}

// controlCharReplacer replaces \r \n \t in example string values
var controlCharReplacer = strings.NewReplacer(`\r`, "\r", `\n`, "\n", `\t`, "\t")

func setSchemaExample(param *spec.Parameter, schemaType string, value string) error {
val, err := defineType(schemaType, value)
if err != nil {
Expand All @@ -544,7 +547,14 @@ func setSchemaExample(param *spec.Parameter, schemaType string, value string) er
if param.Schema == nil {
return nil
}
param.Schema.Example = val

switch v := val.(type) {
case string:
param.Schema.Example = controlCharReplacer.Replace(v)
default:
param.Schema.Example = val
}

return nil
}

Expand Down
9 changes: 6 additions & 3 deletions operation_test.go
Expand Up @@ -1918,13 +1918,16 @@ func TestParseParamCommentBySchemaExampleUnsupportedType(t *testing.T) {

param.Schema = &spec.Schema{}
setSchemaExample(&param, STRING, "string value")
assert.Equal(t, param.Schema.Example, "string value")
assert.Equal(t, "string value", param.Schema.Example)

setSchemaExample(&param, INTEGER, "10")
assert.Equal(t, param.Schema.Example, 10)
assert.Equal(t, 10, param.Schema.Example)

setSchemaExample(&param, NUMBER, "10")
assert.Equal(t, param.Schema.Example, float64(10))
assert.Equal(t, float64(10), param.Schema.Example)

setSchemaExample(&param, STRING, "string \\r\\nvalue")
assert.Equal(t, "string \r\nvalue", param.Schema.Example)
}

func TestParseParamArrayWithEnums(t *testing.T) {
Expand Down

0 comments on commit be03834

Please sign in to comment.