Skip to content

Commit

Permalink
Merge 62d66dc into add313f
Browse files Browse the repository at this point in the history
  • Loading branch information
mschneider82 committed Mar 11, 2022
2 parents add313f + 62d66dc commit 2854f34
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
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

0 comments on commit 2854f34

Please sign in to comment.