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: SchemaExample: allow \r \n \t #1156

Merged
merged 1 commit into from Mar 15, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -33,6 +33,7 @@ Swag converts Go annotations to Swagger Documentation 2.0. We've created a varie
- [Add a headers in response](#add-a-headers-in-response)
- [Use multiple path params](#use-multiple-path-params)
- [Example value of struct](#example-value-of-struct)
- [SchemaExample of body](#schemaexample-of-body)
- [Description of struct](#description-of-struct)
- [Use swaggertype tag to supported custom type](#use-swaggertype-tag-to-supported-custom-type)
- [Use global overrides to support a custom type](#use-global-overrides-to-support-a-custom-type)
Expand Down Expand Up @@ -613,6 +614,12 @@ type Account struct {
}
```

### SchemaExample of body

```go
// @Param email body string true "message/rfc822" SchemaExample(Subject: Testmail\r\n\r\nBody Message\r\n)
```

### Description of struct

```go
Expand Down
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