Skip to content

Commit

Permalink
feat: add support override swaggertype tag especially for self-define…
Browse files Browse the repository at this point in the history
…d st… (#290)

* add: surpport override swaggertype tag especially for self-defined struct

* add test and usage instruction

* import strconv

* alignment

* alignment

* alignment

* uncomment

* modify test
  • Loading branch information
sdghchj authored and easonlin404 committed Jan 24, 2019
1 parent 7cb5615 commit 0666acc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
24 changes: 24 additions & 0 deletions README.md
Expand Up @@ -529,9 +529,33 @@ type Account struct {
### Override swagger type of a struct field

```go
type TimestampTime struct {
time.Time
}
///implement encoding.JSON.Marshaler interface
func (t *TimestampTime) MarshalJSON() ([]byte, error) {
bin := make([]byte, 16)
bin = strconv.AppendInt(bin[:0], t.Time.Unix(), 10)
return bin, nil
}
func (t *TimestampTime) UnmarshalJSON(bin []byte) error {
v, err := strconv.ParseInt(string(bin), 10, 64)
if err != nil {
return err
}
t.Time = time.Unix(v, 0)
return nil
}
///
type Account struct {
// Override primitive type by simply specifying it via `swaggertype` tag
ID sql.NullInt64 `json:"id" swaggertype:"integer"`
// Override struct type to a primitive type 'integer' by specifying it via `swaggertype` tag
RegisterTime TimestampTime `json:"register_time" swaggertype:"primitive,integer"`
// Array types can be overridden using "array,<prim_type>" format
Coeffs []big.Float `json:"coeffs" swaggertype:"array,number"`
Expand Down
9 changes: 7 additions & 2 deletions parser.go
Expand Up @@ -856,8 +856,13 @@ func (parser *Parser) parseField(field *ast.Field) *structField {
if 0 < len(parts) && len(parts) <= 2 {
newSchemaType := parts[0]
newArrayType := structField.arrayType
if len(parts) >= 2 && newSchemaType == "array" {
newArrayType = parts[1]
if len(parts) >= 2 {
if (newSchemaType == "array"){
newArrayType = parts[1]
}else if (newSchemaType == "primitive"){
newSchemaType= parts[1]
newArrayType = parts[1]
}
}

CheckSchemaType(newSchemaType)
Expand Down
3 changes: 3 additions & 0 deletions parser_test.go
Expand Up @@ -1104,6 +1104,9 @@ func TestParseSimpleApi_ForSnakecase(t *testing.T) {
"price"
],
"properties": {
"birthday": {
"type": "integer"
},
"category": {
"type": "object",
"properties": {
Expand Down
21 changes: 21 additions & 0 deletions testdata/simple2/web/handler.go
Expand Up @@ -3,12 +3,32 @@ package web
import (
"database/sql"
"math/big"
"strconv"
"time"

uuid "github.com/satori/go.uuid"
"github.com/shopspring/decimal"
)

type TimestampTime struct {
time.Time
}

func (t *TimestampTime) MarshalJSON() ([]byte, error) {
bin := make([]byte, 16)
bin = strconv.AppendInt(bin[:0], t.Time.Unix(), 10)
return bin, nil
}

func (t *TimestampTime) UnmarshalJSON(bin []byte) error {
v, err := strconv.ParseInt(string(bin), 10, 64)
if err != nil {
return err
}
t.Time = time.Unix(v, 0)
return nil
}

type Pet struct {
ID int `example:"1" format:"int64"`
Category struct {
Expand Down Expand Up @@ -37,6 +57,7 @@ type Pet struct {
customStringArr []CustomString
NullInt sql.NullInt64 `swaggertype:"integer"`
Coeffs []big.Float `swaggertype:"array,number"`
Birthday TimestampTime `swaggertype:"primitive,integer"`
}

type CustomString string
Expand Down

0 comments on commit 0666acc

Please sign in to comment.