Skip to content

Commit

Permalink
prepare
Browse files Browse the repository at this point in the history
  • Loading branch information
emicklei committed Oct 18, 2023
1 parent 0a3beeb commit c4931e5
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ clean:
rm coverage.out

test:
go test -cover -race -count=1 ./...
go test -cover ./...

coverage:
go test -coverprofile=coverage.out ./...
Expand Down
4 changes: 2 additions & 2 deletions build_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func TestReadArrayObjectInBody(t *testing.T) {
Writes([]Sample{}))

p := buildPaths(ws, Config{})
t.Log(asJSON(p))
//t.Log(asJSON(p))

postInfo := p.Paths["/tests/a/a/b"].Post

Expand Down Expand Up @@ -349,7 +349,7 @@ func TestWritesPrimitive(t *testing.T) {
Writes(Sample{}))

p := buildPaths(ws, Config{})
t.Log(asJSON(p))
//t.Log(asJSON(p))

// Make sure that the operation that returns a primitive type is correct.
if pathInfo, okay := p.Paths["/tests/returns/primitive"]; !okay {
Expand Down
24 changes: 12 additions & 12 deletions definition_builder.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package restfulspec

import (
"encoding/json"
"reflect"
"strings"

Expand Down Expand Up @@ -172,17 +171,17 @@ func (b definitionBuilder) buildProperty(field reflect.StructField, model *spec.
fieldType := field.Type

// check if type is doing its own marshalling
marshalerType := reflect.TypeOf((*json.Marshaler)(nil)).Elem()
if fieldType.Implements(marshalerType) {
var pType = "string"
if prop.Type == nil {
prop.Type = []string{pType}
}
if prop.Format == "" {
prop.Format = b.jsonSchemaFormat(keyFrom(fieldType, b.Config), fieldType.Kind())
}
return jsonName, modelDescription, prop
}
// marshalerType := reflect.TypeOf((*json.Marshaler)(nil)).Elem()
// if fieldType.Implements(marshalerType) {
// var pType = "string"
// if prop.Type == nil {
// prop.Type = []string{pType}
// }
// if prop.Format == "" {
// prop.Format = b.jsonSchemaFormat(keyFrom(fieldType, b.Config), fieldType.Kind())
// }
// return jsonName, modelDescription, prop
// }

// check if annotation says it is a string
if jsonTag := field.Tag.Get("json"); jsonTag != "" {
Expand Down Expand Up @@ -317,6 +316,7 @@ func (b definitionBuilder) buildArrayTypeProperty(field reflect.StructField, jso
isArray = b.isSliceOrArrayType(itemType.Kind())
if itemType.Kind() == reflect.Uint8 {
stringt := "string"
prop.Format = "binary"
itemSchema.Type = []string{stringt}
return jsonName, prop
}
Expand Down
17 changes: 8 additions & 9 deletions definition_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package restfulspec

import (
"encoding/json"
"fmt"
"testing"

"github.com/go-openapi/spec"
Expand Down Expand Up @@ -433,8 +434,8 @@ type Embed struct {

func TestRecursiveFieldStructure(t *testing.T) {
db := definitionBuilder{Definitions: spec.Definitions{}, Config: Config{}}
// should not panic
db.addModelFrom(Foo{})
t.Log(db)
}

type email struct {
Expand All @@ -453,7 +454,6 @@ func TestDoubleByteArray(t *testing.T) {
if !ok {
t.Fail()
}
t.Log(sc)
if got, want := sc.Type[0], "array"; got != want {
t.Errorf("got [%v:%T] want [%v:%T]", got, got, want, want)
}
Expand All @@ -476,12 +476,13 @@ func TestDoubleStringArray(t *testing.T) {
t.Log(db.Definitions)
t.Fail()
}
t.Logf("%+v", sc)
if got, want := sc.Type[0], "array"; got != want {
t.Errorf("got [%v:%T] want [%v:%T]", got, got, want, want)
}
tp := sc.Items.Schema.Type
t.Logf("%+v", tp)
if got, want := fmt.Sprint(tp), "[array]"; got != want {
t.Errorf("got [%v:%T] want [%v:%T]", got, got, want, want)
}
}

type Cell struct {
Expand All @@ -501,7 +502,7 @@ func TestDoubleStructArray(t *testing.T) {
t.Logf("definitions: %v", db.Definitions)
t.Fail()
}
t.Logf("%+v", schema)
//t.Logf("%+v", schema)
if want, got := schema.Properties["Cells"].Type[0], "array"; got != want {
t.Errorf("got [%v:%T] want [%v:%T]", got, got, want, want)
}
Expand Down Expand Up @@ -541,22 +542,20 @@ func TestPostBuildSwaggerSchema(t *testing.T) {
t.Logf("definitions: %#v", db.Definitions)
t.Fail()
}
t.Logf("sc: %#v", sc)
// t.Logf("sc: %#v", sc)
if got, want := sc.Description, "parent's description"; got != want {
t.Errorf("got [%v:%T] want [%v:%T]", got, got, want, want)
}
t.Log(sc.Description)

sc, ok = db.Definitions["restfulspec.childPostBuildSwaggerSchema"]
if !ok {
t.Logf("definitions: %#v", db.Definitions)
t.Fail()
}
t.Logf("sc: %#v", sc)
//t.Logf("sc: %#v", sc)
if got, want := sc.Description, "child's description"; got != want {
t.Errorf("got [%v:%T] want [%v:%T]", got, got, want, want)
}
t.Log(sc.Description)
}

func TestEmbedStruct(t *testing.T) {
Expand Down
32 changes: 32 additions & 0 deletions issue79_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package restfulspec

import (
"encoding/json"
"testing"

"github.com/go-openapi/spec"
)

type Parent struct {
FieldA string
}

func (v Parent) MarshalJSON() ([]byte, error) { return nil, nil }

type Child struct {
FieldA Parent
FieldB int
}

func TestParentChildArray(t *testing.T) {
t.Skip()
db := definitionBuilder{Definitions: spec.Definitions{}, Config: Config{}}
db.addModelFrom(Child{})
s := spec.Schema{
SchemaProps: spec.SchemaProps{
Definitions: db.Definitions,
},
}
data, _ := json.MarshalIndent(s, "", " ")
t.Log(string(data))
}
1 change: 1 addition & 0 deletions property_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func TestThatExtraTagsAreReadIntoModel(t *testing.T) {
NotNullableField string `x-nullable:"false"`
UUID string `type:"string" format:"UUID"`
XGoName string `x-go-name:"specgoname"`
ByteArray []byte `format:"binary"`
}
d := definitionsFromStruct(Anything{})
props, _ := d["restfulspec.Anything"]
Expand Down

0 comments on commit c4931e5

Please sign in to comment.