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
2 changes: 1 addition & 1 deletion pkg/codegen/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ type AliasType struct {
}

func (p AliasType) Generate(out *Emitter) error {
out.Printf("type %s = %s", p.Alias, p.Name)
out.Printlnf("type %s = %s", p.Alias, p.Name)

return nil
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/generator/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ func (g *Generator) AddFile(fileName string, schema *schemas.Schema) error {
return err
}

if schema.ID != "" {
if _, processed := o.processedSchemas[schema.ID]; processed {
return nil
}

o.processedSchemas[schema.ID] = true
}

return newSchemaGenerator(g, schema, fileName, o).generateRootType()
}

Expand Down Expand Up @@ -213,6 +221,7 @@ func (g *Generator) beginOutput(
declsBySchema: map[*schemas.Type]*codegen.TypeDecl{},
declsByName: map[string]*codegen.TypeDecl{},
unmarshallersByTypeDecl: map[*codegen.TypeDecl]bool{},
processedSchemas: map[string]bool{},
}
g.outputs[id] = output

Expand Down
1 change: 1 addition & 0 deletions pkg/generator/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type output struct {
declsByName map[string]*codegen.TypeDecl
declsBySchema map[*schemas.Type]*codegen.TypeDecl
unmarshallersByTypeDecl map[*codegen.TypeDecl]bool
processedSchemas map[string]bool
warner func(string)
}

Expand Down
24 changes: 24 additions & 0 deletions pkg/schemas/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ func AnyOf(types []*Type, baseType *Type) (*Type, error) {
return nil, err
}

typ.Required = mergeRequiredUnion(types, baseType)
typ.subSchemaType = SubSchemaTypeAnyOf
typ.subSchemasCount = len(types)

Expand Down Expand Up @@ -339,6 +340,29 @@ func MergeTypes(types []*Type, baseType *Type) (*Type, error) {
return result, nil
}

func mergeRequiredUnion(types []*Type, baseType *Type) []string {
required := make([]string, len(baseType.Required))
copy(required, baseType.Required)

for _, r := range types[0].Required {
valid := true

for _, t := range types {
if !slices.Contains(t.Required, r) {
valid = false

break
}
}

if valid && !slices.Contains(required, r) {
required = append(required, r) //nolint:makezero
}
}

return required
}

func updateAllRefsValues(structValue *reflect.Value, refPath string) error {
switch structValue.Kind() { //nolint:exhaustive
case reflect.Struct:
Expand Down
4 changes: 2 additions & 2 deletions tests/data/core/anyOf/anyOf.1.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/data/core/anyOf/anyOf.2.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/data/core/anyOf/anyOf.3.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions tests/data/core/anyOf/anyOf.4.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tests/data/core/anyOf/anyOf.6.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tests/data/core/anyOf/anyOf.7.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions tests/data/core/anyOf/anyOfMultipleRequired.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/data/core/anyOf/anyOfWithDirectProperties.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions tests/unmarshal_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func TestJsonUmarshalAnyOf(t *testing.T) {
t,
&testAnyOf.AnyOf1{
Configurations: []testAnyOf.AnyOf1ConfigurationsElem{
{Foo: "hello"},
{Bar: 2.2},
{Foo: ptr("hello")},
{Bar: ptr(2.2)},
{Baz: ptr(true)},
},
Flags: "hello",
Expand Down Expand Up @@ -115,8 +115,8 @@ func TestJsonUmarshalAnyOf(t *testing.T) {
t,
&testAnyOf.AnyOf1{
Configurations: []testAnyOf.AnyOf1ConfigurationsElem{
{Foo: "ciao"},
{Bar: 200.0},
{Foo: ptr("ciao")},
{Bar: ptr(200.0)},
},
Flags: true,
},
Expand Down Expand Up @@ -145,8 +145,8 @@ func TestJsonUmarshalAnyOf(t *testing.T) {
t,
&testAnyOf.AnyOf2{
Configurations: []testAnyOf.AnyOf2ConfigurationsElem{
{Foo: "ciao"},
{Bar: 2.0},
{Foo: ptr("ciao")},
{Bar: ptr(2.0)},
{Baz: ptr(false)},
},
},
Expand All @@ -162,7 +162,7 @@ func TestJsonUmarshalAnyOf(t *testing.T) {
assert.Equal(
t,
&testAnyOf.AnyOf3{
Foo: "ciao",
Foo: ptr("ciao"),
},
target,
)
Expand All @@ -176,7 +176,7 @@ func TestJsonUmarshalAnyOf(t *testing.T) {
assert.Equal(
t,
&testAnyOf.AnyOf3{
Bar: 2.0,
Bar: ptr(2.0),
},
target,
)
Expand Down