Skip to content

Commit

Permalink
Add multipleOf field to parser. (#1008)
Browse files Browse the repository at this point in the history
* Add multipleOf field to parser.

* Extend multipleOf tests
  • Loading branch information
jimmycannlfs committed Sep 27, 2021
1 parent 0aac069 commit bcbae0f
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ Field Name | Type | Description
<a name="parameterDefault"></a>default | * | Declares the value of the parameter that the server will use if none is provided, for example a "count" to control the number of results per page might default to 100 if not supplied by the client in the request. (Note: "default" has no meaning for required parameters.) See https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-6.2. Unlike JSON Schema this value MUST conform to the defined [`type`](#parameterType) for this parameter.
<a name="parameterMaximum"></a>maximum | `number` | See https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.1.2.
<a name="parameterMinimum"></a>minimum | `number` | See https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.1.3.
<a name="parameterMultipleOf"></a>multipleOf | `number` | See https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.1.1.
<a name="parameterMaxLength"></a>maxLength | `integer` | See https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.2.1.
<a name="parameterMinLength"></a>minLength | `integer` | See https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.2.2.
<a name="parameterEnums"></a>enums | [\*] | See https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.5.1.
Expand All @@ -488,7 +489,6 @@ Field Name | Type | Description

Field Name | Type | Description
---|:---:|---
<a name="parameterMultipleOf"></a>multipleOf | `number` | See https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.1.1.
<a name="parameterPattern"></a>pattern | `string` | See https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.2.3.
<a name="parameterMaxItems"></a>maxItems | `integer` | See https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.3.2.
<a name="parameterMinItems"></a>minItems | `integer` | See https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.3.3.
Expand Down
8 changes: 8 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,7 @@ type structField struct {
exampleValue interface{}
maximum *float64
minimum *float64
multipleOf *float64
maxLength *int64
minLength *int64
enums []interface{}
Expand Down Expand Up @@ -1077,6 +1078,7 @@ func (parser *Parser) parseStructField(file *ast.File, field *ast.Field) (map[st
}
eleSchema.Maximum = structField.maximum
eleSchema.Minimum = structField.minimum
eleSchema.MultipleOf = structField.multipleOf
eleSchema.MaxLength = structField.maxLength
eleSchema.MinLength = structField.minLength
eleSchema.Enum = structField.enums
Expand Down Expand Up @@ -1269,6 +1271,12 @@ func (parser *Parser) parseFieldTag(field *ast.Field, types []string) (*structFi
return nil, err
}
structField.minimum = minimum

multipleOf, err := getFloatTag(structTag, "multipleOf")
if err != nil {
return nil, err
}
structField.multipleOf = multipleOf
}
if structField.schemaType == STRING || structField.arrayType == STRING {
maxLength, err := getIntTag(structTag, "maxLength")
Expand Down
25 changes: 25 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,7 @@ func TestParseSimpleApi_ForSnakecase(t *testing.T) {
},
"price": {
"type": "number",
"multipleOf": 0.01,
"example": 3.25
},
"status": {
Expand Down Expand Up @@ -1435,6 +1436,7 @@ func TestParseSimpleApi_ForLowerCamelcase(t *testing.T) {
},
"price": {
"type": "number",
"multipleOf": 0.01,
"example": 3.25
},
"status": {
Expand Down Expand Up @@ -3259,6 +3261,29 @@ func TestParseFieldTag(t *testing.T) {
[]string{"number"})
assert.Error(t, err)

field, err = parser.parseFieldTag(
&ast.Field{
Tag: &ast.BasicLit{
Value: `json:"test" multipleOf:"1"`,
},
},
[]string{"number"})
assert.NoError(t, err)
multipleOf := float64(1)
assert.Equal(t, &structField{
schemaType: "number",
multipleOf: &multipleOf,
}, field)

_, err = parser.parseFieldTag(
&ast.Field{
Tag: &ast.BasicLit{
Value: `json:"test" multipleOf:"one"`,
},
},
[]string{"number"})
assert.Error(t, err)

field, err = parser.parseFieldTag(
&ast.Field{
Tag: &ast.BasicLit{
Expand Down
2 changes: 1 addition & 1 deletion testdata/pet/web/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Pet struct {
PhotoUrls []string `json:"photo_urls" example:"http://test/image/1.jpg,http://test/image/2.jpg"`
Tags []Tag `json:"tags"`
Status string `json:"status"`
Price float32 `json:"price" example:"3.25"`
Price float32 `json:"price" example:"3.25" multipleOf:"0.01"`
IsAlive bool `json:"is_alive" example:"true"`
}

Expand Down
1 change: 1 addition & 0 deletions testdata/simple/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@
"type": "number",
"maximum": 1000,
"minimum": 1,
"multipleOf": 0.01,
"example": 3.25
},
"status": {
Expand Down
2 changes: 1 addition & 1 deletion testdata/simple/web/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Pet struct {
Pets *[]Pet2 `json:"pets"`
Pets2 []*Pet2 `json:"pets2"`
Status string `json:"status" enums:"healthy,ill"`
Price float32 `json:"price" example:"3.25" minimum:"1.0" maximum:"1000"`
Price float32 `json:"price" example:"3.25" minimum:"1.0" maximum:"1000" multipleOf:"0.01"`
IsAlive bool `json:"is_alive" example:"true" default:"true"`
Data interface{} `json:"data"`
Hidden string `json:"-"`
Expand Down
2 changes: 1 addition & 1 deletion testdata/simple2/web/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Pet struct {
Pets *[]Pet2
Pets2 []*Pet2
Status string
Price float32 `example:"3.25" validate:"required,gte=0,lte=130"`
Price float32 `example:"3.25" validate:"required,gte=0,lte=130" multipleOf:"0.01"`
IsAlive bool `example:"true"`
Data interface{}
Hidden string `json:"-"`
Expand Down
2 changes: 1 addition & 1 deletion testdata/simple3/web/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Pet struct {
Pets *[]Pet2
Pets2 []*Pet2
Status string
Price float32 `example:"3.25"`
Price float32 `example:"3.25" multipleOf:"0.01"`
IsAlive bool `example:"true"`
Data interface{}
Hidden string `json:"-"`
Expand Down

0 comments on commit bcbae0f

Please sign in to comment.