Skip to content

Commit

Permalink
feat(openapi3gen): Customize json.RawMessage (#1050)
Browse files Browse the repository at this point in the history
* feat(openapi3gen): Customize json.RawMessage

* Add test case
  • Loading branch information
kyleconroy authored Feb 3, 2025
1 parent cea0a13 commit 72fb819
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
7 changes: 3 additions & 4 deletions openapi3gen/openapi3gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,10 @@ func (g *Generator) generateWithoutSaving(parents []*theTypeInfo, t reflect.Type

case reflect.Slice:
if t.Elem().Kind() == reflect.Uint8 {
if t == rawMessageType {
return &openapi3.SchemaRef{Value: schema}, nil
if t != rawMessageType {
schema.Type = &openapi3.Types{"string"}
schema.Format = "byte"
}
schema.Type = &openapi3.Types{"string"}
schema.Format = "byte"
} else {
schema.Type = &openapi3.Types{"array"}
items, err := g.generateSchemaRefFor(parents, t.Elem(), name, tag)
Expand Down
9 changes: 8 additions & 1 deletion openapi3gen/openapi3gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,8 @@ func ExampleSchemaCustomizer() {
InnerFieldWithTag int `mymintag:"-1" mymaxtag:"50"`
NestedInnerBla
}
Enum2Field string `json:"enum2" myenumtag:"c,d"`
Enum2Field string `json:"enum2" myenumtag:"c,d"`
JsonField json.RawMessage `json:"rawmsg" myjsontag:"raw"`
}

type Bla struct {
Expand Down Expand Up @@ -435,6 +436,9 @@ func ExampleSchemaCustomizer() {
schema.Enum = append(schema.Enum, s)
}
}
if tag.Get("myjsontag") != "" {
schema.Description = "description"
}
return nil
})

Expand Down Expand Up @@ -487,6 +491,9 @@ func ExampleSchemaCustomizer() {
// "f"
// ],
// "type": "string"
// },
// "rawmsg": {
// "description": "description"
// }
// },
// "type": "object"
Expand Down

0 comments on commit 72fb819

Please sign in to comment.