Skip to content

Commit

Permalink
Merge pull request #34 from Arlet2/add-restrictions
Browse files Browse the repository at this point in the history
Add parsing for max and min length, nullable for response body, add example for parameters
  • Loading branch information
parvez3019 authored Nov 6, 2023
2 parents 48a4975 + dd2af8f commit fef3d30
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 439 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

# Test binary, built with `go test -c`
*.test
integration_test/test_data/spec/actual.json

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
Expand Down
47 changes: 44 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ type RequestHeaders struct {
// @Title Get user list of a group.
// @Description Get users related to a specific group.
// @Header model.RequestHeaders
// @Param groupID path int true "Id of a specific group."
// @Param groupID path int true "Id of a specific group." "120"
// @Success 200 object UsersResponse "UsersResponse JSON"
// @Failure 400 object ErrorResponse "ErrorResponse JSON"
// @Resource users
Expand Down Expand Up @@ -161,14 +161,15 @@ func PostUser() {
#### Parameter
```
@Param {name} {in} {goType} {required} {description}
@Param user body User true "Info of a user."
@Param {name} {in} {goType} {required} {description} {example}
@Param groupID path int true "Id of a specific group." "120"
```
- {name}: The parameter name.
- {in}: The parameter is in `path`, `query`, `form`, `header`, `cookie`, `body` or `file`.
- {goType}: The type in go code. This will be ignored when {in} is `file`.
- {required}: `true`, `false`, `required` or `optional`.
- {description}: The description of the parameter. Must be quoted.
- {example}: **Optional** example of this parameter. Must be quoted.
One can also override example for an object with `override-example` key in struct
eg -
Expand Down Expand Up @@ -252,6 +253,46 @@ type Request struct {
```
#### Response body
You need to provide swagger fields as reflect tags in your structure. In example:
```go
type Filter struct {
Rating int `json:"rating"`
Type string `json:"type"`
Distance int64 `json:"distance", minimum:"1", maximum:"100"`
DistrictCode string `json:"district_code", pattern:"[a-z]{2}\\d[a-z]\\s\\d[a-z]{2}"`
}
```
Available fields:
- type
- format
- required
- properties
- description
- items
- example
- deprecated (bool)
- ref
- enum
- title
- maximum (float64)
- exclusiveMaximum (bool)
- minimum (float64)
- exclusiveMinimum (bool)
- maxLength (uint)
- minLength (uint)
- pattern
- maxItems (uint)
- minItems (uint)
- uniqueItems (bool)
- maxProperties (uint)
- minProperties (uint)
- additionalProperties (bool)
- nullable (bool)
- readOnly (bool)
- writeOnly (bool)
### 4. Security
If authorization is required, you must define security schemes and then apply those to the API. A scheme is defined
Expand Down
2 changes: 1 addition & 1 deletion integration_test/test_data/handler/restaurants.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// @Description Returns a list of restaurants based on filter request
// @Header model.Headers
// @Param count query int32 false "count of restaurants"
// @Param offset query int32 false "offset limit count"
// @Param offset query int32 false "offset limit count" "100"
// @Param order_by query model.OrderByEnum false "order restaurants list"
// @Param filter query model.Filter false "In json format"
// @Param extra.field query string false "extra field"
Expand Down
14 changes: 7 additions & 7 deletions integration_test/test_data/model/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ type Menu struct {

// GetRestaurantsResponse represents the list of restaurants response
type GetRestaurantsResponse struct {
Restaurants []Restaurant `json:"restaurants"`
Restaurants []Restaurant `json:"restaurants" maxProperties:"100" minProperties:"2" additionalProperties:"true"`
}

// CreateUserRequest represents the model for creating user request
type CreateUserRequest struct {
FirstName string `json:"first_name"`
FirstName string `json:"first_name" readOnly:"true"`
LastName string `json:"last_name"`
Age int `json:"age"`
EmailID string `json:"email_id"`
UserName string `json:"user_name"`
Password string `json:"password"`
Roles []string `json:"roles"`
Age int `json:"age" minimum:"18" exclusiveMinimum:"true" maximum:"256" exclusiveMaximum:"true"`
EmailID string `json:"email_id" pattern:"[\\w.]+@[\\w.]"`
UserName string `json:"user_name" title:"login"`
Password string `json:"password" minLength:"6" maxLength:"200"`
Roles []string `json:"roles" writeOnly:"true" nullable:"true" uniqueItems:"true" minItems:"1" maxItems:"100"`
}

// CreateUserResponse represents the model for create user response
Expand Down
Loading

0 comments on commit fef3d30

Please sign in to comment.