Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ type docable interface {
Docs() map[string]string
}

type exemplar interface {
Example() any
}

type attrable interface {
Attributes() map[string]string
}
Expand All @@ -376,6 +380,10 @@ func customizer(name string, t reflect.Type, _ reflect.StructTag, schema *kin.Sc
applyDocs(schema, obj)
}

if obj, ok := v.(exemplar); ok {
applyExemplar(schema, obj)
}

if obj, ok := v.(attrable); ok {
applyAttrs(schema, obj)
}
Expand Down Expand Up @@ -421,6 +429,14 @@ func applyDocs(schema *kin.Schema, obj docable) {
}
}

func applyExemplar(schema *kin.Schema, obj exemplar) {
example := obj.Example()
if example == nil {
return
}
schema.Example = example
}

func applyAttrs(schema *kin.Schema, obj attrable) {
attrs := obj.Attributes()
var required []string
Expand Down
10 changes: 9 additions & 1 deletion gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ type TestObject struct {
Test1 string `json:"test1"`
Test2 string `json:"test2"`
Test3 string `json:"test3"`
Test4 string `json:"test4"`
Test4 string `json:"test4,omitempty"`
}

func (TestObject) Docs() map[string]string {
Expand All @@ -195,6 +195,14 @@ func (TestObject) Attributes() map[string]string {
}
}

func (TestObject) Example() any {
return TestObject{
Test1: "a",
Test2: "b",
Test3: "c",
}
}

func (TestObject) Formats() map[string]string {
return map[string]string{
"test4": "ipv4",
Expand Down
5 changes: 5 additions & 0 deletions testdata/spec-pkgseg0.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
"type": "object"
},
"TestObject": {
"example": {
"test1": "a",
"test2": "b",
"test3": "c"
},
"properties": {
"test1": {
"description": "Some test docs",
Expand Down
5 changes: 5 additions & 0 deletions testdata/spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
"type": "object"
},
"openapi_test.TestObject": {
"example": {
"test1": "a",
"test2": "b",
"test3": "c"
},
"properties": {
"test1": {
"description": "Some test docs",
Expand Down