Skip to content
Open
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
10 changes: 8 additions & 2 deletions dsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,13 @@ func (s *Schema) SetDescription(d string) *Schema {

// SetType sets the Type of the Schema.
func (s *Schema) SetType(t string) *Schema {
s.Type = t
s.Type = jsonschema.StringArray{t}
return s
}

// AddType adds the Type to the Type array of the Schema.
func (s *Schema) AddType(t string) *Schema {
s.Type = append(s.Type, t)
return s
}

Expand Down Expand Up @@ -1066,7 +1072,7 @@ func schema(t, f string) *Schema {
// AsArray returns a new "array" Schema wrapping the receiver.
func (s *Schema) AsArray() *Schema {
return &Schema{
Type: jsonschema.Array.String(),
Type: []string{jsonschema.Array.String()},
Items: &Items{
Item: s,
},
Expand Down
16 changes: 8 additions & 8 deletions dsl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestBuilder(t *testing.T) {
In: "path",
Description: "ID Parameter in path",
Required: true,
Schema: &ogen.Schema{Type: "integer", Format: "int32"},
Schema: &ogen.Schema{Type: []string{"integer"}, Format: "int32"},
},
},
Responses: ogen.Responses{
Expand All @@ -138,11 +138,11 @@ func TestBuilder(t *testing.T) {
Description: "Success",
Content: map[string]ogen.Media{
ir.EncodingJSON.String(): {Schema: &ogen.Schema{
Type: "object",
Type: []string{"object"},
Description: "Success",
Properties: []ogen.Property{
{Name: "prop1", Schema: &ogen.Schema{Type: "integer", Format: "int32"}},
{Name: "prop2", Schema: &ogen.Schema{Type: "string"}},
{Name: "prop1", Schema: &ogen.Schema{Type: []string{"integer"}, Format: "int32"}},
{Name: "prop2", Schema: &ogen.Schema{Type: []string{"string"}}},
},
}},
},
Expand Down Expand Up @@ -178,11 +178,11 @@ func TestBuilder(t *testing.T) {
Description: "An Error Response",
Content: map[string]ogen.Media{
ir.EncodingJSON.String(): {Schema: &ogen.Schema{
Type: "object",
Type: []string{"object"},
Description: "Error Response Schema",
Properties: []ogen.Property{
{Name: "code", Schema: &ogen.Schema{Type: "integer", Format: "int32"}},
{Name: "status", Schema: &ogen.Schema{Type: "string"}},
{Name: "code", Schema: &ogen.Schema{Type: []string{"integer"}, Format: "int32"}},
{Name: "status", Schema: &ogen.Schema{Type: []string{"string"}}},
},
}},
},
Expand Down Expand Up @@ -368,7 +368,7 @@ func TestBuilder(t *testing.T) {
assert.Equal(t, &ogen.Schema{
Ref: "ref",
Description: "desc",
Type: "object",
Type: []string{"object"},
Format: "",
Properties: []ogen.Property{{Name: "prop"}},
Required: []string{"prop"},
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/go-faster/yaml v0.4.6
github.com/google/uuid v1.6.0
github.com/mattn/go-isatty v0.0.20
github.com/samber/lo v1.52.0
github.com/shopspring/decimal v1.4.0
github.com/stretchr/testify v1.11.1
github.com/valyala/fasthttp v1.69.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
github.com/samber/lo v1.52.0 h1:Rvi+3BFHES3A8meP33VPAxiBZX/Aws5RxrschYGjomw=
github.com/samber/lo v1.52.0/go.mod h1:4+MXEGsJzbKGaUEQFKBq2xtfuznW9oz/WrgyzMzRoM0=
github.com/segmentio/asm v1.2.1 h1:DTNbBqs57ioxAD4PrArqftgypG4/qNpXoJx8TVXxPR0=
github.com/segmentio/asm v1.2.1/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
Expand Down
28 changes: 14 additions & 14 deletions jsonschema/cross_type_constraints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestCrossTypeConstraints(t *testing.T) {
{
name: "maximum on string type - strict mode",
schema: &RawSchema{
Type: "string",
Type: StringArray{"string"},
Maximum: Num(`1000`),
},
allowCrossType: false,
Expand All @@ -28,7 +28,7 @@ func TestCrossTypeConstraints(t *testing.T) {
{
name: "maximum on string type - default mode interprets as numeric constraint",
schema: &RawSchema{
Type: "string",
Type: StringArray{"string"},
Maximum: Num(`1000`),
},
allowCrossType: true,
Expand All @@ -41,7 +41,7 @@ func TestCrossTypeConstraints(t *testing.T) {
{
name: "minimum on string type - strict mode",
schema: &RawSchema{
Type: "string",
Type: StringArray{"string"},
Minimum: Num(`0.001`),
},
allowCrossType: false,
Expand All @@ -51,7 +51,7 @@ func TestCrossTypeConstraints(t *testing.T) {
{
name: "minimum on string type - default mode",
schema: &RawSchema{
Type: "string",
Type: StringArray{"string"},
Minimum: Num(`0.001`),
},
allowCrossType: true,
Expand All @@ -60,7 +60,7 @@ func TestCrossTypeConstraints(t *testing.T) {
{
name: "pattern on number type - strict mode",
schema: &RawSchema{
Type: "number",
Type: StringArray{"number"},
Pattern: `^\d+(\.\d{1,2})?$`,
},
allowCrossType: false,
Expand All @@ -70,7 +70,7 @@ func TestCrossTypeConstraints(t *testing.T) {
{
name: "pattern on number type - default mode",
schema: &RawSchema{
Type: "number",
Type: StringArray{"number"},
Pattern: `^\d+(\.\d{1,2})?$`,
},
allowCrossType: true,
Expand All @@ -79,7 +79,7 @@ func TestCrossTypeConstraints(t *testing.T) {
{
name: "pattern on integer type - strict mode",
schema: &RawSchema{
Type: "integer",
Type: StringArray{"integer"},
Pattern: `^\d+$`,
},
allowCrossType: false,
Expand All @@ -89,7 +89,7 @@ func TestCrossTypeConstraints(t *testing.T) {
{
name: "pattern on integer type - default mode",
schema: &RawSchema{
Type: "integer",
Type: StringArray{"integer"},
Pattern: `^\d+$`,
},
allowCrossType: true,
Expand All @@ -98,7 +98,7 @@ func TestCrossTypeConstraints(t *testing.T) {
{
name: "maxLength on number type - strict mode",
schema: &RawSchema{
Type: "number",
Type: StringArray{"number"},
MaxLength: uint64Ptr(10),
},
allowCrossType: false,
Expand All @@ -108,7 +108,7 @@ func TestCrossTypeConstraints(t *testing.T) {
{
name: "maxLength on number type - default mode",
schema: &RawSchema{
Type: "number",
Type: StringArray{"number"},
MaxLength: uint64Ptr(10),
},
allowCrossType: true,
Expand All @@ -117,7 +117,7 @@ func TestCrossTypeConstraints(t *testing.T) {
{
name: "valid schema - both modes",
schema: &RawSchema{
Type: "string",
Type: StringArray{"string"},
MaxLength: uint64Ptr(100),
Pattern: `^\w+$`,
},
Expand Down Expand Up @@ -151,20 +151,20 @@ func TestCrossTypeConstraints(t *testing.T) {
func TestCrossTypeConstraintsComplex(t *testing.T) {
// Test with a more complex schema like the one in the issue
schema := &RawSchema{
Type: "object",
Type: StringArray{"object"},
Properties: []RawProperty{
{
Name: "weight",
Schema: &RawSchema{
Type: "string",
Type: StringArray{"string"},
Maximum: Num(`1000`),
Minimum: Num(`0.001`),
},
},
{
Name: "quantity",
Schema: &RawSchema{
Type: "number",
Type: StringArray{"number"},
Pattern: `^\d+(\.\d{1,2})?$`,
},
},
Expand Down
26 changes: 15 additions & 11 deletions jsonschema/infer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"slices"
"strings"

"github.com/samber/lo"

"github.com/go-faster/errors"
"github.com/go-faster/jx"
)
Expand All @@ -23,16 +25,16 @@ func (i *Infer) Apply(data []byte) error {
return apply(&i.target, jx.DecodeBytes(data))
}

func applyType(s *RawSchema, tt string) {
if hasType(s, tt) {
func applyType(s *RawSchema, tt ...string) {
if hasType(s, tt...) {
return
}
if len(s.OneOf) > 0 {
s.OneOf = append(s.OneOf, &RawSchema{Type: tt})
return
}

if s.Type == "" {
if s.Type == nil {
s.Type = tt
return
}
Expand All @@ -46,26 +48,28 @@ func applyType(s *RawSchema, tt string) {
}
}

func hasType(s *RawSchema, tt string) bool {
if s.Type == tt {
func hasType(s *RawSchema, tt ...string) bool {
if len(lo.Intersect(s.Type, tt)) > 0 {
return true
}

for _, v := range s.OneOf {
if v.Type == tt {
if len(lo.Intersect(v.Type, tt)) > 0 {
return true
}
}
return false
}

func replaceType(s *RawSchema, from, to string) bool {
if s.Type == from {
func replaceType(s *RawSchema, from, to []string) bool {
if lo.ElementsMatch(s.Type, from) {
s.Type = to
return true
}

for _, v := range s.OneOf {
if v.Type == from {
v.Type = to
if lo.ElementsMatch(v.Type, from) {
s.Type = to
return true
}
}
Expand All @@ -86,7 +90,7 @@ func apply(s *RawSchema, d *jx.Decoder) error {
applyType(s, "integer")
return nil
}
if replaceType(s, "integer", "number") {
if replaceType(s, []string{"integer"}, []string{"number"}) {
return nil
}
applyType(s, "number")
Expand Down
Loading
Loading