Skip to content
Permalink
Browse files

feat: add support override swaggertype tag especially for self-define…

…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 0666acc8956457aa755727b935f1210cbb9a3c63
Showing with 55 additions and 2 deletions.
  1. +24 −0 README.md
  2. +7 −2 parser.go
  3. +3 −0 parser_test.go
  4. +21 −0 testdata/simple2/web/handler.go
@@ -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"`
@@ -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)
@@ -1104,6 +1104,9 @@ func TestParseSimpleApi_ForSnakecase(t *testing.T) {
"price"
],
"properties": {
"birthday": {
"type": "integer"
},
"category": {
"type": "object",
"properties": {
@@ -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 {
@@ -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

0 comments on commit 0666acc

Please sign in to comment.
You can’t perform that action at this time.