From 3d5e820beac3e0b1bdf25899ac42489e7d2ac5ab Mon Sep 17 00:00:00 2001 From: tdakkota Date: Mon, 24 Jul 2023 12:11:05 +0300 Subject: [PATCH 1/3] feat(gen): inline `oneOf` into object schema --- gen/_template/json/encoders_struct.tmpl | 12 ++++ gen/_template/json/encoders_sum.tmpl | 37 ++++++++--- gen/ir/field.go | 1 + gen/ir/json.go | 10 +++ gen/schema_gen.go | 84 +++++++++++++++++++++++-- gen/schema_gen_sum.go | 38 +++++++++-- 6 files changed, 164 insertions(+), 18 deletions(-) diff --git a/gen/_template/json/encoders_struct.tmpl b/gen/_template/json/encoders_struct.tmpl index 65ca8a256..995f5470c 100644 --- a/gen/_template/json/encoders_struct.tmpl +++ b/gen/_template/json/encoders_struct.tmpl @@ -13,6 +13,7 @@ {{- $fields := $.JSON.Fields }} {{- $additional := $.JSON.AdditionalProps }} {{- $pattern := $.JSON.PatternProps }} +{{- $sum := $.JSON.SumProps }} {{- $va := $.Validators }} // Encode implements json.Marshaler. func (s {{ $.ReadOnlyReceiver }}) Encode(e *jx.Encoder) { @@ -79,6 +80,9 @@ func (s {{ $.ReadOnlyReceiver }}) encodeFields(e *jx.Encoder) { {{ template "json/enc" map_elem $p.Type.Item }} } {{- end }} + {{- range $s := $sum }} + s.{{ $s.Name }}.encodeFields(e) + {{- end }} } var jsonFieldsNameOf{{ $.Name }} = [{{ len $fields }}]string{ @@ -93,6 +97,14 @@ func (s *{{ $.Name }}) Decode(d *jx.Decoder) error { return errors.New({{ printf "invalid: unable to decode %s to nil" $.Name | quote }}) } + {{- range $s := $sum }} + if err := d.Capture(func(d *jx.Decoder) error { + return s.{{ $s.Name }}.Decode(d) + }); err != nil { + return errors.Wrap(err, {{ printf "decode field %s" $s.Name | quote }}) + } + {{- end }} + {{- $requiredMask := $fields.RequiredMask }} {{- if $fields.HasRequired }} var requiredBitSet [{{ len $requiredMask }}]uint8 diff --git a/gen/_template/json/encoders_sum.tmpl b/gen/_template/json/encoders_sum.tmpl index d30851dc6..22c031286 100644 --- a/gen/_template/json/encoders_sum.tmpl +++ b/gen/_template/json/encoders_sum.tmpl @@ -3,16 +3,9 @@ // Encode encodes {{ $.Name }} as json. func (s {{ $.ReadOnlyReceiver }}) Encode(e *jx.Encoder) { {{- if $d.Discriminator -}} - switch s.Type { - {{- range $m := $.SumSpec.Mapping }} - case {{ $m.Type.Name }}{{ $.Name }}: - e.ObjStart() - e.FieldStart({{ quote $.SumSpec.Discriminator }}) - e.Str({{ quote $m.Key }}) - s.{{ $m.Type.Name }}.encodeFields(e) - e.ObjEnd() - {{- end }} - } + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() {{- else }} switch s.Type { {{- range $s := $.SumOf }} @@ -23,6 +16,30 @@ func (s {{ $.ReadOnlyReceiver }}) Encode(e *jx.Encoder) { {{- end }} } +{{ if or $d.Discriminator $d.Fields -}} +func (s {{ $.ReadOnlyReceiver }}) encodeFields(e *jx.Encoder) { + {{- if $d.Discriminator -}} + switch s.Type { + {{- range $m := $.SumSpec.Mapping }} + case {{ $m.Type.Name }}{{ $.Name }}: + e.FieldStart({{ quote $.SumSpec.Discriminator }}) + e.Str({{ quote $m.Key }}) + s.{{ $m.Type.Name }}.encodeFields(e) + {{- end }} + } + {{- else if $d.Fields }} + switch s.Type { + {{- range $s := $.SumOf }} + case {{ $s.Name }}{{ $.Name }}: + s.{{ $s.Name }}.encodeFields(e) + {{- end }} + } + {{- else }} + {{ errorf "unexpected sum type %s" $d }} + {{- end }} +} +{{- end }} + // Decode decodes {{ $.Name }} from json. func (s *{{ $.Name }}) Decode(d *jx.Decoder) error { if s == nil { diff --git a/gen/ir/field.go b/gen/ir/field.go index cc87bbe19..9be597f47 100644 --- a/gen/ir/field.go +++ b/gen/ir/field.go @@ -46,6 +46,7 @@ const ( InlineNone InlineField = iota InlineAdditional InlinePattern + InlineSum ) // Field of structure. diff --git a/gen/ir/json.go b/gen/ir/json.go index eb57fed40..ce8a572bc 100644 --- a/gen/ir/json.go +++ b/gen/ir/json.go @@ -96,6 +96,16 @@ func (j JSON) PatternProps() (fields []*Field) { return fields } +// SumProps return field of Type that should be encoded as inlined sum. +func (j JSON) SumProps() (fields []*Field) { + for _, f := range j.t.Fields { + if f.Inline == InlineSum { + fields = append(fields, f) + } + } + return fields +} + // Format returns format name for handling json encoding or decoding. // // Mostly used for encoding or decoding of string formats, like `json.EncodeUUID`, diff --git a/gen/schema_gen.go b/gen/schema_gen.go index 271db2733..507e89c3d 100644 --- a/gen/schema_gen.go +++ b/gen/schema_gen.go @@ -185,13 +185,51 @@ func (g *schemaGen) generate2(name string, schema *jsonschema.Schema) (ret *ir.T name = "R" + name } + var ( + oneOf *ir.Type + anyOf *ir.Type + checkOnlyObjectVariants = func(sum []*jsonschema.Schema) error { + for _, s := range sum { + if s.Type == jsonschema.Object { + continue + } + + ptr := s.Pointer + err := errors.Errorf("can't merge object with %s", s.Type) + + pos, ok := ptr.Position() + if !ok { + return err + } + + return &location.Error{ + File: ptr.File(), + Pos: pos, + Err: err, + } + } + return nil + } + ) switch { case len(schema.AnyOf) > 0: - t, err := g.anyOf(name, schema) + side := schema.Type == jsonschema.Object + sumName := name + if side { + sumName += "Sum" + } + t, err := g.anyOf(sumName, schema, side) if err != nil { return nil, errors.Wrap(err, "anyOf") } - return t, nil + + if !side { + return t, nil + } + if err := checkOnlyObjectVariants(schema.AnyOf); err != nil { + return nil, err + } + anyOf = t case len(schema.AllOf) > 0: t, err := g.allOf(name, schema) if err != nil { @@ -199,11 +237,23 @@ func (g *schemaGen) generate2(name string, schema *jsonschema.Schema) (ret *ir.T } return t, nil case len(schema.OneOf) > 0: - t, err := g.oneOf(name, schema) + side := schema.Type == jsonschema.Object + sumName := name + if side { + sumName += "Sum" + } + t, err := g.oneOf(sumName, schema, side) if err != nil { return nil, errors.Wrap(err, "oneOf") } - return t, nil + + if !side { + return t, nil + } + if err := checkOnlyObjectVariants(schema.OneOf); err != nil { + return nil, err + } + oneOf = t } switch schema.Type { @@ -399,6 +449,32 @@ func (g *schemaGen) generate2(name string, schema *jsonschema.Schema) (ret *ir.T } } } + if anyOf != nil { + slot := fieldSlot{ + original: "anyOf", + nameDefinedAt: schema.Pointer.Key("anyOf"), + } + if err := addField(&ir.Field{ + Name: "AnyOf", + Type: anyOf, + Inline: ir.InlineSum, + }, slot); err != nil { + return nil, err + } + } + if oneOf != nil { + slot := fieldSlot{ + original: "oneOf", + nameDefinedAt: schema.Pointer.Key("oneOf"), + } + if err := addField(&ir.Field{ + Name: "OneOf", + Type: oneOf, + Inline: ir.InlineSum, + }, slot); err != nil { + return nil, err + } + } return s, nil case jsonschema.Array: diff --git a/gen/schema_gen_sum.go b/gen/schema_gen_sum.go index 6eb80030f..4364e92da 100644 --- a/gen/schema_gen_sum.go +++ b/gen/schema_gen_sum.go @@ -175,15 +175,19 @@ func schemaName(k jsonschema.Ref) (string, bool) { return path.Base(after), true } -func (g *schemaGen) anyOf(name string, schema *jsonschema.Schema) (*ir.Type, error) { +func (g *schemaGen) anyOf(name string, schema *jsonschema.Schema, side bool) (*ir.Type, error) { if err := ensureNoInfiniteRecursion(schema); err != nil { return nil, err } + var regSchema *jsonschema.Schema + if !side { + regSchema = schema + } sum := g.regtype(name, &ir.Type{ Name: name, Kind: ir.KindSum, - Schema: schema, + Schema: regSchema, }) { variants, err := g.collectSumVariants(name, schema.AnyOf) @@ -233,15 +237,19 @@ func (g *schemaGen) anyOf(name string, schema *jsonschema.Schema) (*ir.Type, err return nil, &ErrNotImplemented{"complex anyOf"} } -func (g *schemaGen) oneOf(name string, schema *jsonschema.Schema) (*ir.Type, error) { +func (g *schemaGen) oneOf(name string, schema *jsonschema.Schema, side bool) (*ir.Type, error) { if err := ensureNoInfiniteRecursion(schema); err != nil { return nil, err } + var regSchema *jsonschema.Schema + if !side { + regSchema = schema + } sum := g.regtype(name, &ir.Type{ Name: name, Kind: ir.KindSum, - Schema: schema, + Schema: regSchema, }) { variants, err := g.collectSumVariants(name, schema.OneOf) @@ -812,6 +820,28 @@ func mergeSchemes(s1, s2 *jsonschema.Schema) (_ *jsonschema.Schema, err error) { } } + // oneOf, anyOf + mergeSum := func(name string, s1, s2 []*jsonschema.Schema) ([]*jsonschema.Schema, error) { + switch { + case len(s1) > 0 && len(s2) > 0: + return nil, &ErrNotImplemented{Name: fmt.Sprintf("allOf with %s", name)} + case len(s1) > 0: + return s1, nil + case len(s2) > 0: + return s2, nil + default: + return nil, nil + } + } + r.OneOf, err = mergeSum("oneOf", s1.OneOf, s2.OneOf) + if err != nil { + return nil, errors.Wrap(err, "merge oneOf") + } + r.AnyOf, err = mergeSum("anyOf", s1.AnyOf, s2.AnyOf) + if err != nil { + return nil, errors.Wrap(err, "merge anyOf") + } + return r, nil } From f171f4879a4354088103fb7156af4d8932962e9a Mon Sep 17 00:00:00 2001 From: tdakkota Date: Mon, 24 Jul 2023 12:11:27 +0300 Subject: [PATCH 2/3] test: add tests for inlined `oneOf` --- _testdata/positive/sample.json | 161 +++++++++++++++++++- internal/integration/json_test.go | 241 ++++++++++++++++++++++++++++++ 2 files changed, 400 insertions(+), 2 deletions(-) diff --git a/_testdata/positive/sample.json b/_testdata/positive/sample.json index 0b717955c..4b387a066 100644 --- a/_testdata/positive/sample.json +++ b/_testdata/positive/sample.json @@ -781,6 +781,23 @@ } } } + }, + "/testInlineOneof": { + "get": { + "operationId": "testInlineOneof", + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/TestInlineOneOf" + } + } + } + } + } + } } }, "components": { @@ -1423,7 +1440,6 @@ ] }, "OneOfMappingReference": { - "type": "object", "oneOf": [ { "$ref": "#/components/schemas/OneOfMappingReferenceA" @@ -1816,6 +1832,147 @@ } } ] + }, + "TestInlineOneOf": { + "type": "object", + "properties": { + "inline_discriminator": { + "$ref": "#/components/schemas/InlineDiscriminatorOneOf" + }, + "merge_discriminator": { + "$ref": "#/components/schemas/MergeDiscriminatorOneOf" + }, + "inline_unique_fields": { + "$ref": "#/components/schemas/InlineUniqueFieldsOneOf" + }, + "merge_unique_fields": { + "$ref": "#/components/schemas/MergeUniqueFieldsOneOf" + } + } + }, + "InlineDiscriminatorOneOf": { + "type": "object", + "required": [ + "common" + ], + "properties": { + "common": { + "type": "string" + } + }, + "oneOf": [ + { + "$ref": "#/components/schemas/InlineOneOfFoo" + }, + { + "$ref": "#/components/schemas/InlineOneOfBar" + } + ], + "discriminator": { + "propertyName": "kind", + "mapping": { + "foo": "#/components/schemas/InlineOneOfFoo", + "bar": "#/components/schemas/InlineOneOfBar" + } + } + }, + "MergeDiscriminatorOneOf": { + "allOf": [ + { + "type": "object", + "required": [ + "common" + ], + "properties": { + "common": { + "type": "string" + } + } + }, + { + "oneOf": [ + { + "$ref": "#/components/schemas/InlineOneOfFoo" + }, + { + "$ref": "#/components/schemas/InlineOneOfBar" + } + ], + "discriminator": { + "propertyName": "kind", + "mapping": { + "foo": "#/components/schemas/InlineOneOfFoo", + "bar": "#/components/schemas/InlineOneOfBar" + } + } + } + ] + }, + "InlineUniqueFieldsOneOf": { + "type": "object", + "required": [ + "common" + ], + "properties": { + "common": { + "type": "string" + } + }, + "oneOf": [ + { + "$ref": "#/components/schemas/InlineOneOfFoo" + }, + { + "$ref": "#/components/schemas/InlineOneOfBar" + } + ] + }, + "MergeUniqueFieldsOneOf": { + "allOf": [ + { + "type": "object", + "required": [ + "common" + ], + "properties": { + "common": { + "type": "string" + } + } + }, + { + "oneOf": [ + { + "$ref": "#/components/schemas/InlineOneOfFoo" + }, + { + "$ref": "#/components/schemas/InlineOneOfBar" + } + ] + } + ] + }, + "InlineOneOfFoo": { + "type": "object", + "properties": { + "kind": { + "type": "string" + }, + "foo": { + "type": "string" + } + } + }, + "InlineOneOfBar": { + "type": "object", + "properties": { + "kind": { + "type": "string" + }, + "bar": { + "type": "string" + } + } } }, "parameters": { @@ -1922,4 +2079,4 @@ } } } -} +} \ No newline at end of file diff --git a/internal/integration/json_test.go b/internal/integration/json_test.go index 818b63d93..221b4a048 100644 --- a/internal/integration/json_test.go +++ b/internal/integration/json_test.go @@ -1223,3 +1223,244 @@ func TestTupleJSON(t *testing.T) { }) } } + +func TestInlineOneOf(t *testing.T) { + t.Run("InlineDiscriminator", func(t *testing.T) { + for i, tc := range []struct { + Input string + Expected api.InlineDiscriminatorOneOf + ExpectErr bool + }{ + { + `{"common": "object_field", "kind": "foo"}`, + api.InlineDiscriminatorOneOf{ + Common: "object_field", + OneOf: api.InlineDiscriminatorOneOfSum{ + Type: api.InlineOneOfFooInlineDiscriminatorOneOfSum, + InlineOneOfFoo: api.InlineOneOfFoo{}, + }, + }, + false, + }, + { + `{"common": "object_field", "kind": "foo", "foo": "sum_field"}`, + api.InlineDiscriminatorOneOf{ + Common: "object_field", + OneOf: api.InlineDiscriminatorOneOfSum{ + Type: api.InlineOneOfFooInlineDiscriminatorOneOfSum, + InlineOneOfFoo: api.InlineOneOfFoo{ + Foo: api.NewOptString(`sum_field`), + }, + }, + }, + false, + }, + { + `{"common": "object_field", "kind": "bar"}`, + api.InlineDiscriminatorOneOf{ + Common: "object_field", + OneOf: api.InlineDiscriminatorOneOfSum{ + Type: api.InlineOneOfBarInlineDiscriminatorOneOfSum, + InlineOneOfBar: api.InlineOneOfBar{}, + }, + }, + false, + }, + { + `{"common": "object_field", "kind": "bar", "bar": "sum_field"}`, + api.InlineDiscriminatorOneOf{ + Common: "object_field", + OneOf: api.InlineDiscriminatorOneOfSum{ + Type: api.InlineOneOfBarInlineDiscriminatorOneOfSum, + InlineOneOfBar: api.InlineOneOfBar{ + Bar: api.NewOptString(`sum_field`), + }, + }, + }, + false, + }, + {`{"common": "foo"}`, api.InlineDiscriminatorOneOf{}, true}, + } { + // Make range value copy to prevent data races. + tc := tc + t.Run(fmt.Sprintf("Test%d", i+1), func(t *testing.T) { + r := &api.InlineDiscriminatorOneOf{} + err := r.Decode(jx.DecodeStr(tc.Input)) + if tc.ExpectErr { + require.Error(t, err) + return + } + require.NoError(t, err) + testEncode(t, r, tc.Input) + }) + } + }) + t.Run("MergeDiscriminator", func(t *testing.T) { + for i, tc := range []struct { + Input string + Expected api.MergeDiscriminatorOneOf + ExpectErr bool + }{ + { + `{"common": "object_field", "kind": "foo"}`, + api.MergeDiscriminatorOneOf{ + Common: "object_field", + OneOf: api.MergeDiscriminatorOneOfSum{ + Type: api.InlineOneOfFooMergeDiscriminatorOneOfSum, + InlineOneOfFoo: api.InlineOneOfFoo{}, + }, + }, + false, + }, + { + `{"common": "object_field", "kind": "foo", "foo": "sum_field"}`, + api.MergeDiscriminatorOneOf{ + Common: "object_field", + OneOf: api.MergeDiscriminatorOneOfSum{ + Type: api.InlineOneOfFooMergeDiscriminatorOneOfSum, + InlineOneOfFoo: api.InlineOneOfFoo{ + Foo: api.NewOptString(`sum_field`), + }, + }, + }, + false, + }, + { + `{"common": "object_field", "kind": "bar"}`, + api.MergeDiscriminatorOneOf{ + Common: "object_field", + OneOf: api.MergeDiscriminatorOneOfSum{ + Type: api.InlineOneOfBarMergeDiscriminatorOneOfSum, + InlineOneOfBar: api.InlineOneOfBar{}, + }, + }, + false, + }, + { + `{"common": "object_field", "kind": "bar", "bar": "sum_field"}`, + api.MergeDiscriminatorOneOf{ + Common: "object_field", + OneOf: api.MergeDiscriminatorOneOfSum{ + Type: api.InlineOneOfBarMergeDiscriminatorOneOfSum, + InlineOneOfBar: api.InlineOneOfBar{ + Bar: api.NewOptString(`sum_field`), + }, + }, + }, + false, + }, + {`{"common": "foo"}`, api.MergeDiscriminatorOneOf{}, true}, + } { + // Make range value copy to prevent data races. + tc := tc + t.Run(fmt.Sprintf("Test%d", i+1), func(t *testing.T) { + r := &api.MergeDiscriminatorOneOf{} + err := r.Decode(jx.DecodeStr(tc.Input)) + if tc.ExpectErr { + require.Error(t, err) + return + } + require.NoError(t, err) + testEncode(t, r, tc.Input) + }) + } + }) + t.Run("InlineUniqueFields", func(t *testing.T) { + for i, tc := range []struct { + Input string + Expected api.InlineUniqueFieldsOneOf + ExpectErr bool + }{ + { + `{"common": "object_field", "foo": "sum_field"}`, + api.InlineUniqueFieldsOneOf{ + Common: "object_field", + OneOf: api.InlineUniqueFieldsOneOfSum{ + Type: api.InlineOneOfFooInlineUniqueFieldsOneOfSum, + InlineOneOfFoo: api.InlineOneOfFoo{ + Foo: api.NewOptString(`sum_field`), + }, + }, + }, + false, + }, + { + `{"common": "object_field", "bar": "sum_field"}`, + api.InlineUniqueFieldsOneOf{ + Common: "object_field", + OneOf: api.InlineUniqueFieldsOneOfSum{ + Type: api.InlineOneOfBarInlineUniqueFieldsOneOfSum, + InlineOneOfBar: api.InlineOneOfBar{ + Bar: api.NewOptString(`sum_field`), + }, + }, + }, + false, + }, + {`{"common": "foo"}`, api.InlineUniqueFieldsOneOf{}, true}, + {`{"common": "foo", "kind": "foo"}`, api.InlineUniqueFieldsOneOf{}, true}, + } { + // Make range value copy to prevent data races. + tc := tc + t.Run(fmt.Sprintf("Test%d", i+1), func(t *testing.T) { + r := &api.InlineUniqueFieldsOneOf{} + err := r.Decode(jx.DecodeStr(tc.Input)) + if tc.ExpectErr { + require.Error(t, err) + return + } + require.NoError(t, err) + testEncode(t, r, tc.Input) + }) + } + }) + t.Run("MergeUniqueFields", func(t *testing.T) { + for i, tc := range []struct { + Input string + Expected api.MergeUniqueFieldsOneOf + ExpectErr bool + }{ + { + `{"common": "object_field", "foo": "sum_field"}`, + api.MergeUniqueFieldsOneOf{ + Common: "object_field", + OneOf: api.MergeUniqueFieldsOneOfSum{ + Type: api.InlineOneOfFooMergeUniqueFieldsOneOfSum, + InlineOneOfFoo: api.InlineOneOfFoo{ + Foo: api.NewOptString(`sum_field`), + }, + }, + }, + false, + }, + { + `{"common": "object_field", "bar": "sum_field"}`, + api.MergeUniqueFieldsOneOf{ + Common: "object_field", + OneOf: api.MergeUniqueFieldsOneOfSum{ + Type: api.InlineOneOfBarMergeUniqueFieldsOneOfSum, + InlineOneOfBar: api.InlineOneOfBar{ + Bar: api.NewOptString(`sum_field`), + }, + }, + }, + false, + }, + {`{"common": "foo"}`, api.MergeUniqueFieldsOneOf{}, true}, + {`{"common": "foo", "kind": "foo"}`, api.MergeUniqueFieldsOneOf{}, true}, + } { + // Make range value copy to prevent data races. + tc := tc + t.Run(fmt.Sprintf("Test%d", i+1), func(t *testing.T) { + r := &api.MergeUniqueFieldsOneOf{} + err := r.Decode(jx.DecodeStr(tc.Input)) + if tc.ExpectErr { + require.Error(t, err) + return + } + require.NoError(t, err) + testEncode(t, r, tc.Input) + }) + } + }) +} From e3ad021a5ad37790c977bc04652c88499872dd0d Mon Sep 17 00:00:00 2001 From: tdakkota Date: Mon, 24 Jul 2023 12:11:37 +0300 Subject: [PATCH 3/3] chore: commit generated files --- examples/ex_2ch/oas_json_gen.go | 9 + examples/ex_github/oas_json_gen.go | 18 + examples/ex_gotd/oas_json_gen.go | 164 +-- examples/ex_openapi/output.gen.go | 81 ++ examples/ex_telegram/oas_json_gen.go | 334 +++-- .../integration/sample_api/oas_client_gen.go | 69 + .../integration/sample_api/oas_faker_gen.go | 174 +++ .../sample_api/oas_handlers_gen.go | 82 ++ .../integration/sample_api/oas_json_gen.go | 1215 ++++++++++++++++- .../sample_api/oas_response_decoders_gen.go | 41 + .../sample_api/oas_response_encoders_gen.go | 13 + .../integration/sample_api/oas_router_gen.go | 39 + .../integration/sample_api/oas_schemas_gen.go | 640 +++++++++ .../integration/sample_api/oas_server_gen.go | 4 + .../sample_api/oas_test_examples_gen_test.go | 132 ++ .../sample_api/oas_unimplemented_gen.go | 7 + .../sample_api_nc/oas_handlers_gen.go | 82 ++ .../integration/sample_api_nc/oas_json_gen.go | 1215 ++++++++++++++++- .../oas_response_encoders_gen.go | 13 + .../sample_api_nc/oas_router_gen.go | 39 + .../sample_api_nc/oas_schemas_gen.go | 640 +++++++++ .../sample_api_nc/oas_server_gen.go | 4 + .../sample_api_nc/oas_unimplemented_gen.go | 7 + .../sample_api_ns/oas_client_gen.go | 69 + .../integration/sample_api_ns/oas_json_gen.go | 1215 ++++++++++++++++- .../oas_response_decoders_gen.go | 41 + .../sample_api_ns/oas_schemas_gen.go | 640 +++++++++ .../sample_api_nsnc/oas_json_gen.go | 1215 ++++++++++++++++- .../sample_api_nsnc/oas_schemas_gen.go | 640 +++++++++ 29 files changed, 8568 insertions(+), 274 deletions(-) diff --git a/examples/ex_2ch/oas_json_gen.go b/examples/ex_2ch/oas_json_gen.go index 6ef50cfd0..f7db0056b 100644 --- a/examples/ex_2ch/oas_json_gen.go +++ b/examples/ex_2ch/oas_json_gen.go @@ -3152,6 +3152,15 @@ func (s UserPostingPostOK) Encode(e *jx.Encoder) { } } +func (s UserPostingPostOK) encodeFields(e *jx.Encoder) { + switch s.Type { + case PostingNewThreadUserPostingPostOK: + s.PostingNewThread.encodeFields(e) + case PostingNewPostUserPostingPostOK: + s.PostingNewPost.encodeFields(e) + } +} + // Decode decodes UserPostingPostOK from json. func (s *UserPostingPostOK) Decode(d *jx.Decoder) error { if s == nil { diff --git a/examples/ex_github/oas_json_gen.go b/examples/ex_github/oas_json_gen.go index 63b50acad..c05327b37 100644 --- a/examples/ex_github/oas_json_gen.go +++ b/examples/ex_github/oas_json_gen.go @@ -167976,6 +167976,15 @@ func (s UsersGetAuthenticatedOK) Encode(e *jx.Encoder) { } } +func (s UsersGetAuthenticatedOK) encodeFields(e *jx.Encoder) { + switch s.Type { + case PrivateUserUsersGetAuthenticatedOK: + s.PrivateUser.encodeFields(e) + case PublicUserUsersGetAuthenticatedOK: + s.PublicUser.encodeFields(e) + } +} + // Decode decodes UsersGetAuthenticatedOK from json. func (s *UsersGetAuthenticatedOK) Decode(d *jx.Decoder) error { if s == nil { @@ -168099,6 +168108,15 @@ func (s UsersGetByUsernameOK) Encode(e *jx.Encoder) { } } +func (s UsersGetByUsernameOK) encodeFields(e *jx.Encoder) { + switch s.Type { + case PrivateUserUsersGetByUsernameOK: + s.PrivateUser.encodeFields(e) + case PublicUserUsersGetByUsernameOK: + s.PublicUser.encodeFields(e) + } +} + // Decode decodes UsersGetByUsernameOK from json. func (s *UsersGetByUsernameOK) Decode(d *jx.Decoder) error { if s == nil { diff --git a/examples/ex_gotd/oas_json_gen.go b/examples/ex_gotd/oas_json_gen.go index b060106c7..522f2ea3f 100644 --- a/examples/ex_gotd/oas_json_gen.go +++ b/examples/ex_gotd/oas_json_gen.go @@ -1929,49 +1929,41 @@ func (s *BotCommand) UnmarshalJSON(data []byte) error { // Encode encodes BotCommandScope as json. func (s BotCommandScope) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s BotCommandScope) encodeFields(e *jx.Encoder) { switch s.Type { case BotCommandScopeAllChatAdministratorsBotCommandScope: - e.ObjStart() e.FieldStart("type") e.Str("all_chat_administrators") s.BotCommandScopeAllChatAdministrators.encodeFields(e) - e.ObjEnd() case BotCommandScopeAllGroupChatsBotCommandScope: - e.ObjStart() e.FieldStart("type") e.Str("all_group_chats") s.BotCommandScopeAllGroupChats.encodeFields(e) - e.ObjEnd() case BotCommandScopeAllPrivateChatsBotCommandScope: - e.ObjStart() e.FieldStart("type") e.Str("all_private_chats") s.BotCommandScopeAllPrivateChats.encodeFields(e) - e.ObjEnd() case BotCommandScopeChatBotCommandScope: - e.ObjStart() e.FieldStart("type") e.Str("chat") s.BotCommandScopeChat.encodeFields(e) - e.ObjEnd() case BotCommandScopeChatAdministratorsBotCommandScope: - e.ObjStart() e.FieldStart("type") e.Str("chat_administrators") s.BotCommandScopeChatAdministrators.encodeFields(e) - e.ObjEnd() case BotCommandScopeChatMemberBotCommandScope: - e.ObjStart() e.FieldStart("type") e.Str("chat_member") s.BotCommandScopeChatMember.encodeFields(e) - e.ObjEnd() case BotCommandScopeDefaultBotCommandScope: - e.ObjStart() e.FieldStart("type") e.Str("default") s.BotCommandScopeDefault.encodeFields(e) - e.ObjEnd() } } @@ -4018,43 +4010,37 @@ func (s *ChatLocation) UnmarshalJSON(data []byte) error { // Encode encodes ChatMember as json. func (s ChatMember) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s ChatMember) encodeFields(e *jx.Encoder) { switch s.Type { case ChatMemberAdministratorChatMember: - e.ObjStart() e.FieldStart("status") e.Str("ChatMemberAdministrator") s.ChatMemberAdministrator.encodeFields(e) - e.ObjEnd() case ChatMemberBannedChatMember: - e.ObjStart() e.FieldStart("status") e.Str("ChatMemberBanned") s.ChatMemberBanned.encodeFields(e) - e.ObjEnd() case ChatMemberLeftChatMember: - e.ObjStart() e.FieldStart("status") e.Str("ChatMemberLeft") s.ChatMemberLeft.encodeFields(e) - e.ObjEnd() case ChatMemberMemberChatMember: - e.ObjStart() e.FieldStart("status") e.Str("ChatMemberMember") s.ChatMemberMember.encodeFields(e) - e.ObjEnd() case ChatMemberOwnerChatMember: - e.ObjStart() e.FieldStart("status") e.Str("ChatMemberOwner") s.ChatMemberOwner.encodeFields(e) - e.ObjEnd() case ChatMemberRestrictedChatMember: - e.ObjStart() e.FieldStart("status") e.Str("ChatMemberRestricted") s.ChatMemberRestricted.encodeFields(e) - e.ObjEnd() } } @@ -12138,85 +12124,65 @@ func (s *InlineQueryChatType) UnmarshalJSON(data []byte) error { // Encode encodes InlineQueryResult as json. func (s InlineQueryResult) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s InlineQueryResult) encodeFields(e *jx.Encoder) { switch s.Type { case InlineQueryResultArticleInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("article") s.InlineQueryResultArticle.encodeFields(e) - e.ObjEnd() case InlineQueryResultAudioInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("audio") s.InlineQueryResultAudio.encodeFields(e) - e.ObjEnd() case InlineQueryResultContactInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("contact") s.InlineQueryResultContact.encodeFields(e) - e.ObjEnd() case InlineQueryResultDocumentInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("document") s.InlineQueryResultDocument.encodeFields(e) - e.ObjEnd() case InlineQueryResultGameInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("game") s.InlineQueryResultGame.encodeFields(e) - e.ObjEnd() case InlineQueryResultGifInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("gif") s.InlineQueryResultGif.encodeFields(e) - e.ObjEnd() case InlineQueryResultLocationInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("location") s.InlineQueryResultLocation.encodeFields(e) - e.ObjEnd() case InlineQueryResultMpeg4GifInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("mpeg4_gif") s.InlineQueryResultMpeg4Gif.encodeFields(e) - e.ObjEnd() case InlineQueryResultPhotoInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("photo") s.InlineQueryResultPhoto.encodeFields(e) - e.ObjEnd() case InlineQueryResultCachedStickerInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("sticker") s.InlineQueryResultCachedSticker.encodeFields(e) - e.ObjEnd() case InlineQueryResultVenueInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("venue") s.InlineQueryResultVenue.encodeFields(e) - e.ObjEnd() case InlineQueryResultVideoInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("video") s.InlineQueryResultVideo.encodeFields(e) - e.ObjEnd() case InlineQueryResultVoiceInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("voice") s.InlineQueryResultVoice.encodeFields(e) - e.ObjEnd() } } @@ -18402,37 +18368,33 @@ func (s *InputLocationMessageContent) UnmarshalJSON(data []byte) error { // Encode encodes InputMedia as json. func (s InputMedia) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s InputMedia) encodeFields(e *jx.Encoder) { switch s.Type { case InputMediaAnimationInputMedia: - e.ObjStart() e.FieldStart("type") e.Str("animation") s.InputMediaAnimation.encodeFields(e) - e.ObjEnd() case InputMediaAudioInputMedia: - e.ObjStart() e.FieldStart("type") e.Str("audio") s.InputMediaAudio.encodeFields(e) - e.ObjEnd() case InputMediaDocumentInputMedia: - e.ObjStart() e.FieldStart("type") e.Str("document") s.InputMediaDocument.encodeFields(e) - e.ObjEnd() case InputMediaPhotoInputMedia: - e.ObjStart() e.FieldStart("type") e.Str("photo") s.InputMediaPhoto.encodeFields(e) - e.ObjEnd() case InputMediaVideoInputMedia: - e.ObjStart() e.FieldStart("type") e.Str("video") s.InputMediaVideo.encodeFields(e) - e.ObjEnd() } } @@ -19599,6 +19561,21 @@ func (s InputMessageContent) Encode(e *jx.Encoder) { } } +func (s InputMessageContent) encodeFields(e *jx.Encoder) { + switch s.Type { + case InputTextMessageContentInputMessageContent: + s.InputTextMessageContent.encodeFields(e) + case InputLocationMessageContentInputMessageContent: + s.InputLocationMessageContent.encodeFields(e) + case InputVenueMessageContentInputMessageContent: + s.InputVenueMessageContent.encodeFields(e) + case InputContactMessageContentInputMessageContent: + s.InputContactMessageContent.encodeFields(e) + case InputInvoiceMessageContentInputMessageContent: + s.InputInvoiceMessageContent.encodeFields(e) + } +} + // Decode decodes InputMessageContent from json. func (s *InputMessageContent) Decode(d *jx.Decoder) error { if s == nil { @@ -21446,25 +21423,25 @@ func (s *MaskPosition) UnmarshalJSON(data []byte) error { // Encode encodes MenuButton as json. func (s MenuButton) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s MenuButton) encodeFields(e *jx.Encoder) { switch s.Type { case MenuButtonCommandsMenuButton: - e.ObjStart() e.FieldStart("type") e.Str("commands") s.MenuButtonCommands.encodeFields(e) - e.ObjEnd() case MenuButtonDefaultMenuButton: - e.ObjStart() e.FieldStart("type") e.Str("default") s.MenuButtonDefault.encodeFields(e) - e.ObjEnd() case MenuButtonWebAppMenuButton: - e.ObjStart() e.FieldStart("type") e.Str("web_app") s.MenuButtonWebApp.encodeFields(e) - e.ObjEnd() } } @@ -26078,61 +26055,49 @@ func (s *PassportData) UnmarshalJSON(data []byte) error { // Encode encodes PassportElementError as json. func (s PassportElementError) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s PassportElementError) encodeFields(e *jx.Encoder) { switch s.Type { case PassportElementErrorDataFieldPassportElementError: - e.ObjStart() e.FieldStart("type") e.Str("data") s.PassportElementErrorDataField.encodeFields(e) - e.ObjEnd() case PassportElementErrorFilePassportElementError: - e.ObjStart() e.FieldStart("type") e.Str("file") s.PassportElementErrorFile.encodeFields(e) - e.ObjEnd() case PassportElementErrorFilesPassportElementError: - e.ObjStart() e.FieldStart("type") e.Str("files") s.PassportElementErrorFiles.encodeFields(e) - e.ObjEnd() case PassportElementErrorFrontSidePassportElementError: - e.ObjStart() e.FieldStart("type") e.Str("front_side") s.PassportElementErrorFrontSide.encodeFields(e) - e.ObjEnd() case PassportElementErrorReverseSidePassportElementError: - e.ObjStart() e.FieldStart("type") e.Str("reverse_side") s.PassportElementErrorReverseSide.encodeFields(e) - e.ObjEnd() case PassportElementErrorSelfiePassportElementError: - e.ObjStart() e.FieldStart("type") e.Str("selfie") s.PassportElementErrorSelfie.encodeFields(e) - e.ObjEnd() case PassportElementErrorTranslationFilePassportElementError: - e.ObjStart() e.FieldStart("type") e.Str("translation_file") s.PassportElementErrorTranslationFile.encodeFields(e) - e.ObjEnd() case PassportElementErrorTranslationFilesPassportElementError: - e.ObjStart() e.FieldStart("type") e.Str("translation_files") s.PassportElementErrorTranslationFiles.encodeFields(e) - e.ObjEnd() case PassportElementErrorUnspecifiedPassportElementError: - e.ObjStart() e.FieldStart("type") e.Str("unspecified") s.PassportElementErrorUnspecified.encodeFields(e) - e.ObjEnd() } } @@ -34917,31 +34882,29 @@ func (s *SendMediaGroup) UnmarshalJSON(data []byte) error { // Encode encodes SendMediaGroupMediaItem as json. func (s SendMediaGroupMediaItem) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s SendMediaGroupMediaItem) encodeFields(e *jx.Encoder) { switch s.Type { case InputMediaAudioSendMediaGroupMediaItem: - e.ObjStart() e.FieldStart("type") e.Str("audio") s.InputMediaAudio.encodeFields(e) - e.ObjEnd() case InputMediaDocumentSendMediaGroupMediaItem: - e.ObjStart() e.FieldStart("type") e.Str("document") s.InputMediaDocument.encodeFields(e) - e.ObjEnd() case InputMediaPhotoSendMediaGroupMediaItem: - e.ObjStart() e.FieldStart("type") e.Str("photo") s.InputMediaPhoto.encodeFields(e) - e.ObjEnd() case InputMediaVideoSendMediaGroupMediaItem: - e.ObjStart() e.FieldStart("type") e.Str("video") s.InputMediaVideo.encodeFields(e) - e.ObjEnd() } } @@ -35969,6 +35932,19 @@ func (s SendReplyMarkup) Encode(e *jx.Encoder) { } } +func (s SendReplyMarkup) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineKeyboardMarkupSendReplyMarkup: + s.InlineKeyboardMarkup.encodeFields(e) + case ReplyKeyboardMarkupSendReplyMarkup: + s.ReplyKeyboardMarkup.encodeFields(e) + case ReplyKeyboardRemoveSendReplyMarkup: + s.ReplyKeyboardRemove.encodeFields(e) + case ForceReplySendReplyMarkup: + s.ForceReply.encodeFields(e) + } +} + // Decode decodes SendReplyMarkup from json. func (s *SendReplyMarkup) Decode(d *jx.Decoder) error { if s == nil { diff --git a/examples/ex_openapi/output.gen.go b/examples/ex_openapi/output.gen.go index f20bc5762..f373ff9c5 100644 --- a/examples/ex_openapi/output.gen.go +++ b/examples/ex_openapi/output.gen.go @@ -6065,6 +6065,15 @@ func (s CallbackOrReference) Encode(e *jx.Encoder) { } } +func (s CallbackOrReference) encodeFields(e *jx.Encoder) { + switch s.Type { + case CallbackCallbackOrReference: + s.Callback.encodeFields(e) + case ReferenceCallbackOrReference: + s.Reference.encodeFields(e) + } +} + // Decode decodes CallbackOrReference from json. func (s *CallbackOrReference) Decode(d *jx.Decoder) error { if s == nil { @@ -7398,6 +7407,15 @@ func (s ExampleOrReference) Encode(e *jx.Encoder) { } } +func (s ExampleOrReference) encodeFields(e *jx.Encoder) { + switch s.Type { + case ExampleExampleOrReference: + s.Example.encodeFields(e) + case ReferenceExampleOrReference: + s.Reference.encodeFields(e) + } +} + // Decode decodes ExampleOrReference from json. func (s *ExampleOrReference) Decode(d *jx.Decoder) error { if s == nil { @@ -8150,6 +8168,15 @@ func (s HeaderOrReference) Encode(e *jx.Encoder) { } } +func (s HeaderOrReference) encodeFields(e *jx.Encoder) { + switch s.Type { + case HeaderHeaderOrReference: + s.Header.encodeFields(e) + case ReferenceHeaderOrReference: + s.Reference.encodeFields(e) + } +} + // Decode decodes HeaderOrReference from json. func (s *HeaderOrReference) Decode(d *jx.Decoder) error { if s == nil { @@ -9061,6 +9088,15 @@ func (s LinkOrReference) Encode(e *jx.Encoder) { } } +func (s LinkOrReference) encodeFields(e *jx.Encoder) { + switch s.Type { + case LinkLinkOrReference: + s.Link.encodeFields(e) + case ReferenceLinkOrReference: + s.Reference.encodeFields(e) + } +} + // Decode decodes LinkOrReference from json. func (s *LinkOrReference) Decode(d *jx.Decoder) error { if s == nil { @@ -12286,6 +12322,15 @@ func (s ParameterOrReference) Encode(e *jx.Encoder) { } } +func (s ParameterOrReference) encodeFields(e *jx.Encoder) { + switch s.Type { + case ParameterParameterOrReference: + s.Parameter.encodeFields(e) + case ReferenceParameterOrReference: + s.Reference.encodeFields(e) + } +} + // Decode decodes ParameterOrReference from json. func (s *ParameterOrReference) Decode(d *jx.Decoder) error { if s == nil { @@ -13598,6 +13643,15 @@ func (s RequestBodyOrReference) Encode(e *jx.Encoder) { } } +func (s RequestBodyOrReference) encodeFields(e *jx.Encoder) { + switch s.Type { + case RequestBodyRequestBodyOrReference: + s.RequestBody.encodeFields(e) + case ReferenceRequestBodyOrReference: + s.Reference.encodeFields(e) + } +} + // Decode decodes RequestBodyOrReference from json. func (s *RequestBodyOrReference) Decode(d *jx.Decoder) error { if s == nil { @@ -13925,6 +13979,15 @@ func (s ResponseOrReference) Encode(e *jx.Encoder) { } } +func (s ResponseOrReference) encodeFields(e *jx.Encoder) { + switch s.Type { + case ResponseResponseOrReference: + s.Response.encodeFields(e) + case ReferenceResponseOrReference: + s.Reference.encodeFields(e) + } +} + // Decode decodes ResponseOrReference from json. func (s *ResponseOrReference) Decode(d *jx.Decoder) error { if s == nil { @@ -15121,6 +15184,15 @@ func (s SchemaOrReference) Encode(e *jx.Encoder) { } } +func (s SchemaOrReference) encodeFields(e *jx.Encoder) { + switch s.Type { + case SchemaSchemaOrReference: + s.Schema.encodeFields(e) + case ReferenceSchemaOrReference: + s.Reference.encodeFields(e) + } +} + // Decode decodes SchemaOrReference from json. func (s *SchemaOrReference) Decode(d *jx.Decoder) error { if s == nil { @@ -16008,6 +16080,15 @@ func (s SecuritySchemeOrReference) Encode(e *jx.Encoder) { } } +func (s SecuritySchemeOrReference) encodeFields(e *jx.Encoder) { + switch s.Type { + case SecuritySchemeSecuritySchemeOrReference: + s.SecurityScheme.encodeFields(e) + case ReferenceSecuritySchemeOrReference: + s.Reference.encodeFields(e) + } +} + // Decode decodes SecuritySchemeOrReference from json. func (s *SecuritySchemeOrReference) Decode(d *jx.Decoder) error { if s == nil { diff --git a/examples/ex_telegram/oas_json_gen.go b/examples/ex_telegram/oas_json_gen.go index 033c58a73..b50d99e1d 100644 --- a/examples/ex_telegram/oas_json_gen.go +++ b/examples/ex_telegram/oas_json_gen.go @@ -1802,49 +1802,41 @@ func (s *BotCommand) UnmarshalJSON(data []byte) error { // Encode encodes BotCommandScope as json. func (s BotCommandScope) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s BotCommandScope) encodeFields(e *jx.Encoder) { switch s.Type { case BotCommandScopeAllChatAdministratorsBotCommandScope: - e.ObjStart() e.FieldStart("type") e.Str("all_chat_administrators") s.BotCommandScopeAllChatAdministrators.encodeFields(e) - e.ObjEnd() case BotCommandScopeAllGroupChatsBotCommandScope: - e.ObjStart() e.FieldStart("type") e.Str("all_group_chats") s.BotCommandScopeAllGroupChats.encodeFields(e) - e.ObjEnd() case BotCommandScopeAllPrivateChatsBotCommandScope: - e.ObjStart() e.FieldStart("type") e.Str("all_private_chats") s.BotCommandScopeAllPrivateChats.encodeFields(e) - e.ObjEnd() case BotCommandScopeChatBotCommandScope: - e.ObjStart() e.FieldStart("type") e.Str("chat") s.BotCommandScopeChat.encodeFields(e) - e.ObjEnd() case BotCommandScopeChatAdministratorsBotCommandScope: - e.ObjStart() e.FieldStart("type") e.Str("chat_administrators") s.BotCommandScopeChatAdministrators.encodeFields(e) - e.ObjEnd() case BotCommandScopeChatMemberBotCommandScope: - e.ObjStart() e.FieldStart("type") e.Str("chat_member") s.BotCommandScopeChatMember.encodeFields(e) - e.ObjEnd() case BotCommandScopeDefaultBotCommandScope: - e.ObjStart() e.FieldStart("type") e.Str("default") s.BotCommandScopeDefault.encodeFields(e) - e.ObjEnd() } } @@ -3607,43 +3599,37 @@ func (s *ChatLocation) UnmarshalJSON(data []byte) error { // Encode encodes ChatMember as json. func (s ChatMember) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s ChatMember) encodeFields(e *jx.Encoder) { switch s.Type { case ChatMemberAdministratorChatMember: - e.ObjStart() e.FieldStart("status") e.Str("ChatMemberAdministrator") s.ChatMemberAdministrator.encodeFields(e) - e.ObjEnd() case ChatMemberBannedChatMember: - e.ObjStart() e.FieldStart("status") e.Str("ChatMemberBanned") s.ChatMemberBanned.encodeFields(e) - e.ObjEnd() case ChatMemberLeftChatMember: - e.ObjStart() e.FieldStart("status") e.Str("ChatMemberLeft") s.ChatMemberLeft.encodeFields(e) - e.ObjEnd() case ChatMemberMemberChatMember: - e.ObjStart() e.FieldStart("status") e.Str("ChatMemberMember") s.ChatMemberMember.encodeFields(e) - e.ObjEnd() case ChatMemberOwnerChatMember: - e.ObjStart() e.FieldStart("status") e.Str("ChatMemberOwner") s.ChatMemberOwner.encodeFields(e) - e.ObjEnd() case ChatMemberRestrictedChatMember: - e.ObjStart() e.FieldStart("status") e.Str("ChatMemberRestricted") s.ChatMemberRestricted.encodeFields(e) - e.ObjEnd() } } @@ -5998,6 +5984,19 @@ func (s CopyMessageReplyMarkup) Encode(e *jx.Encoder) { } } +func (s CopyMessageReplyMarkup) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineKeyboardMarkupCopyMessageReplyMarkup: + s.InlineKeyboardMarkup.encodeFields(e) + case ReplyKeyboardMarkupCopyMessageReplyMarkup: + s.ReplyKeyboardMarkup.encodeFields(e) + case ReplyKeyboardRemoveCopyMessageReplyMarkup: + s.ReplyKeyboardRemove.encodeFields(e) + case ForceReplyCopyMessageReplyMarkup: + s.ForceReply.encodeFields(e) + } +} + // Decode decodes CopyMessageReplyMarkup from json. func (s *CopyMessageReplyMarkup) Decode(d *jx.Decoder) error { if s == nil { @@ -11652,85 +11651,65 @@ func (s *InlineQueryChatType) UnmarshalJSON(data []byte) error { // Encode encodes InlineQueryResult as json. func (s InlineQueryResult) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s InlineQueryResult) encodeFields(e *jx.Encoder) { switch s.Type { case InlineQueryResultArticleInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("article") s.InlineQueryResultArticle.encodeFields(e) - e.ObjEnd() case InlineQueryResultAudioInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("audio") s.InlineQueryResultAudio.encodeFields(e) - e.ObjEnd() case InlineQueryResultContactInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("contact") s.InlineQueryResultContact.encodeFields(e) - e.ObjEnd() case InlineQueryResultDocumentInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("document") s.InlineQueryResultDocument.encodeFields(e) - e.ObjEnd() case InlineQueryResultGameInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("game") s.InlineQueryResultGame.encodeFields(e) - e.ObjEnd() case InlineQueryResultGifInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("gif") s.InlineQueryResultGif.encodeFields(e) - e.ObjEnd() case InlineQueryResultLocationInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("location") s.InlineQueryResultLocation.encodeFields(e) - e.ObjEnd() case InlineQueryResultMpeg4GifInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("mpeg4_gif") s.InlineQueryResultMpeg4Gif.encodeFields(e) - e.ObjEnd() case InlineQueryResultPhotoInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("photo") s.InlineQueryResultPhoto.encodeFields(e) - e.ObjEnd() case InlineQueryResultCachedStickerInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("sticker") s.InlineQueryResultCachedSticker.encodeFields(e) - e.ObjEnd() case InlineQueryResultVenueInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("venue") s.InlineQueryResultVenue.encodeFields(e) - e.ObjEnd() case InlineQueryResultVideoInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("video") s.InlineQueryResultVideo.encodeFields(e) - e.ObjEnd() case InlineQueryResultVoiceInlineQueryResult: - e.ObjStart() e.FieldStart("type") e.Str("voice") s.InlineQueryResultVoice.encodeFields(e) - e.ObjEnd() } } @@ -17916,37 +17895,33 @@ func (s *InputLocationMessageContent) UnmarshalJSON(data []byte) error { // Encode encodes InputMedia as json. func (s InputMedia) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s InputMedia) encodeFields(e *jx.Encoder) { switch s.Type { case InputMediaAnimationInputMedia: - e.ObjStart() e.FieldStart("type") e.Str("animation") s.InputMediaAnimation.encodeFields(e) - e.ObjEnd() case InputMediaAudioInputMedia: - e.ObjStart() e.FieldStart("type") e.Str("audio") s.InputMediaAudio.encodeFields(e) - e.ObjEnd() case InputMediaDocumentInputMedia: - e.ObjStart() e.FieldStart("type") e.Str("document") s.InputMediaDocument.encodeFields(e) - e.ObjEnd() case InputMediaPhotoInputMedia: - e.ObjStart() e.FieldStart("type") e.Str("photo") s.InputMediaPhoto.encodeFields(e) - e.ObjEnd() case InputMediaVideoInputMedia: - e.ObjStart() e.FieldStart("type") e.Str("video") s.InputMediaVideo.encodeFields(e) - e.ObjEnd() } } @@ -19113,6 +19088,21 @@ func (s InputMessageContent) Encode(e *jx.Encoder) { } } +func (s InputMessageContent) encodeFields(e *jx.Encoder) { + switch s.Type { + case InputTextMessageContentInputMessageContent: + s.InputTextMessageContent.encodeFields(e) + case InputLocationMessageContentInputMessageContent: + s.InputLocationMessageContent.encodeFields(e) + case InputVenueMessageContentInputMessageContent: + s.InputVenueMessageContent.encodeFields(e) + case InputContactMessageContentInputMessageContent: + s.InputContactMessageContent.encodeFields(e) + case InputInvoiceMessageContentInputMessageContent: + s.InputInvoiceMessageContent.encodeFields(e) + } +} + // Decode decodes InputMessageContent from json. func (s *InputMessageContent) Decode(d *jx.Decoder) error { if s == nil { @@ -25366,61 +25356,49 @@ func (s *PassportData) UnmarshalJSON(data []byte) error { // Encode encodes PassportElementError as json. func (s PassportElementError) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s PassportElementError) encodeFields(e *jx.Encoder) { switch s.Type { case PassportElementErrorDataFieldPassportElementError: - e.ObjStart() e.FieldStart("type") e.Str("data") s.PassportElementErrorDataField.encodeFields(e) - e.ObjEnd() case PassportElementErrorFilePassportElementError: - e.ObjStart() e.FieldStart("type") e.Str("file") s.PassportElementErrorFile.encodeFields(e) - e.ObjEnd() case PassportElementErrorFilesPassportElementError: - e.ObjStart() e.FieldStart("type") e.Str("files") s.PassportElementErrorFiles.encodeFields(e) - e.ObjEnd() case PassportElementErrorFrontSidePassportElementError: - e.ObjStart() e.FieldStart("type") e.Str("front_side") s.PassportElementErrorFrontSide.encodeFields(e) - e.ObjEnd() case PassportElementErrorReverseSidePassportElementError: - e.ObjStart() e.FieldStart("type") e.Str("reverse_side") s.PassportElementErrorReverseSide.encodeFields(e) - e.ObjEnd() case PassportElementErrorSelfiePassportElementError: - e.ObjStart() e.FieldStart("type") e.Str("selfie") s.PassportElementErrorSelfie.encodeFields(e) - e.ObjEnd() case PassportElementErrorTranslationFilePassportElementError: - e.ObjStart() e.FieldStart("type") e.Str("translation_file") s.PassportElementErrorTranslationFile.encodeFields(e) - e.ObjEnd() case PassportElementErrorTranslationFilesPassportElementError: - e.ObjStart() e.FieldStart("type") e.Str("translation_files") s.PassportElementErrorTranslationFiles.encodeFields(e) - e.ObjEnd() case PassportElementErrorUnspecifiedPassportElementError: - e.ObjStart() e.FieldStart("type") e.Str("unspecified") s.PassportElementErrorUnspecified.encodeFields(e) - e.ObjEnd() } } @@ -31518,6 +31496,19 @@ func (s SendAnimationReplyMarkup) Encode(e *jx.Encoder) { } } +func (s SendAnimationReplyMarkup) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineKeyboardMarkupSendAnimationReplyMarkup: + s.InlineKeyboardMarkup.encodeFields(e) + case ReplyKeyboardMarkupSendAnimationReplyMarkup: + s.ReplyKeyboardMarkup.encodeFields(e) + case ReplyKeyboardRemoveSendAnimationReplyMarkup: + s.ReplyKeyboardRemove.encodeFields(e) + case ForceReplySendAnimationReplyMarkup: + s.ForceReply.encodeFields(e) + } +} + // Decode decodes SendAnimationReplyMarkup from json. func (s *SendAnimationReplyMarkup) Decode(d *jx.Decoder) error { if s == nil { @@ -31949,6 +31940,19 @@ func (s SendAudioReplyMarkup) Encode(e *jx.Encoder) { } } +func (s SendAudioReplyMarkup) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineKeyboardMarkupSendAudioReplyMarkup: + s.InlineKeyboardMarkup.encodeFields(e) + case ReplyKeyboardMarkupSendAudioReplyMarkup: + s.ReplyKeyboardMarkup.encodeFields(e) + case ReplyKeyboardRemoveSendAudioReplyMarkup: + s.ReplyKeyboardRemove.encodeFields(e) + case ForceReplySendAudioReplyMarkup: + s.ForceReply.encodeFields(e) + } +} + // Decode decodes SendAudioReplyMarkup from json. func (s *SendAudioReplyMarkup) Decode(d *jx.Decoder) error { if s == nil { @@ -32412,6 +32416,19 @@ func (s SendContactReplyMarkup) Encode(e *jx.Encoder) { } } +func (s SendContactReplyMarkup) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineKeyboardMarkupSendContactReplyMarkup: + s.InlineKeyboardMarkup.encodeFields(e) + case ReplyKeyboardMarkupSendContactReplyMarkup: + s.ReplyKeyboardMarkup.encodeFields(e) + case ReplyKeyboardRemoveSendContactReplyMarkup: + s.ReplyKeyboardRemove.encodeFields(e) + case ForceReplySendContactReplyMarkup: + s.ForceReply.encodeFields(e) + } +} + // Decode decodes SendContactReplyMarkup from json. func (s *SendContactReplyMarkup) Decode(d *jx.Decoder) error { if s == nil { @@ -32712,6 +32729,19 @@ func (s SendDiceReplyMarkup) Encode(e *jx.Encoder) { } } +func (s SendDiceReplyMarkup) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineKeyboardMarkupSendDiceReplyMarkup: + s.InlineKeyboardMarkup.encodeFields(e) + case ReplyKeyboardMarkupSendDiceReplyMarkup: + s.ReplyKeyboardMarkup.encodeFields(e) + case ReplyKeyboardRemoveSendDiceReplyMarkup: + s.ReplyKeyboardRemove.encodeFields(e) + case ForceReplySendDiceReplyMarkup: + s.ForceReply.encodeFields(e) + } +} + // Decode decodes SendDiceReplyMarkup from json. func (s *SendDiceReplyMarkup) Decode(d *jx.Decoder) error { if s == nil { @@ -33109,6 +33139,19 @@ func (s SendDocumentReplyMarkup) Encode(e *jx.Encoder) { } } +func (s SendDocumentReplyMarkup) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineKeyboardMarkupSendDocumentReplyMarkup: + s.InlineKeyboardMarkup.encodeFields(e) + case ReplyKeyboardMarkupSendDocumentReplyMarkup: + s.ReplyKeyboardMarkup.encodeFields(e) + case ReplyKeyboardRemoveSendDocumentReplyMarkup: + s.ReplyKeyboardRemove.encodeFields(e) + case ForceReplySendDocumentReplyMarkup: + s.ForceReply.encodeFields(e) + } +} + // Decode decodes SendDocumentReplyMarkup from json. func (s *SendDocumentReplyMarkup) Decode(d *jx.Decoder) error { if s == nil { @@ -34221,6 +34264,19 @@ func (s SendLocationReplyMarkup) Encode(e *jx.Encoder) { } } +func (s SendLocationReplyMarkup) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineKeyboardMarkupSendLocationReplyMarkup: + s.InlineKeyboardMarkup.encodeFields(e) + case ReplyKeyboardMarkupSendLocationReplyMarkup: + s.ReplyKeyboardMarkup.encodeFields(e) + case ReplyKeyboardRemoveSendLocationReplyMarkup: + s.ReplyKeyboardRemove.encodeFields(e) + case ForceReplySendLocationReplyMarkup: + s.ForceReply.encodeFields(e) + } +} + // Decode decodes SendLocationReplyMarkup from json. func (s *SendLocationReplyMarkup) Decode(d *jx.Decoder) error { if s == nil { @@ -34502,31 +34558,29 @@ func (s *SendMediaGroup) UnmarshalJSON(data []byte) error { // Encode encodes SendMediaGroupMediaItem as json. func (s SendMediaGroupMediaItem) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s SendMediaGroupMediaItem) encodeFields(e *jx.Encoder) { switch s.Type { case InputMediaAudioSendMediaGroupMediaItem: - e.ObjStart() e.FieldStart("type") e.Str("audio") s.InputMediaAudio.encodeFields(e) - e.ObjEnd() case InputMediaDocumentSendMediaGroupMediaItem: - e.ObjStart() e.FieldStart("type") e.Str("document") s.InputMediaDocument.encodeFields(e) - e.ObjEnd() case InputMediaPhotoSendMediaGroupMediaItem: - e.ObjStart() e.FieldStart("type") e.Str("photo") s.InputMediaPhoto.encodeFields(e) - e.ObjEnd() case InputMediaVideoSendMediaGroupMediaItem: - e.ObjStart() e.FieldStart("type") e.Str("video") s.InputMediaVideo.encodeFields(e) - e.ObjEnd() } } @@ -34870,6 +34924,19 @@ func (s SendMessageReplyMarkup) Encode(e *jx.Encoder) { } } +func (s SendMessageReplyMarkup) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineKeyboardMarkupSendMessageReplyMarkup: + s.InlineKeyboardMarkup.encodeFields(e) + case ReplyKeyboardMarkupSendMessageReplyMarkup: + s.ReplyKeyboardMarkup.encodeFields(e) + case ReplyKeyboardRemoveSendMessageReplyMarkup: + s.ReplyKeyboardRemove.encodeFields(e) + case ForceReplySendMessageReplyMarkup: + s.ForceReply.encodeFields(e) + } +} + // Decode decodes SendMessageReplyMarkup from json. func (s *SendMessageReplyMarkup) Decode(d *jx.Decoder) error { if s == nil { @@ -35233,6 +35300,19 @@ func (s SendPhotoReplyMarkup) Encode(e *jx.Encoder) { } } +func (s SendPhotoReplyMarkup) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineKeyboardMarkupSendPhotoReplyMarkup: + s.InlineKeyboardMarkup.encodeFields(e) + case ReplyKeyboardMarkupSendPhotoReplyMarkup: + s.ReplyKeyboardMarkup.encodeFields(e) + case ReplyKeyboardRemoveSendPhotoReplyMarkup: + s.ReplyKeyboardRemove.encodeFields(e) + case ForceReplySendPhotoReplyMarkup: + s.ForceReply.encodeFields(e) + } +} + // Decode decodes SendPhotoReplyMarkup from json. func (s *SendPhotoReplyMarkup) Decode(d *jx.Decoder) error { if s == nil { @@ -35745,6 +35825,19 @@ func (s SendPollReplyMarkup) Encode(e *jx.Encoder) { } } +func (s SendPollReplyMarkup) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineKeyboardMarkupSendPollReplyMarkup: + s.InlineKeyboardMarkup.encodeFields(e) + case ReplyKeyboardMarkupSendPollReplyMarkup: + s.ReplyKeyboardMarkup.encodeFields(e) + case ReplyKeyboardRemoveSendPollReplyMarkup: + s.ReplyKeyboardRemove.encodeFields(e) + case ForceReplySendPollReplyMarkup: + s.ForceReply.encodeFields(e) + } +} + // Decode decodes SendPollReplyMarkup from json. func (s *SendPollReplyMarkup) Decode(d *jx.Decoder) error { if s == nil { @@ -36045,6 +36138,19 @@ func (s SendStickerReplyMarkup) Encode(e *jx.Encoder) { } } +func (s SendStickerReplyMarkup) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineKeyboardMarkupSendStickerReplyMarkup: + s.InlineKeyboardMarkup.encodeFields(e) + case ReplyKeyboardMarkupSendStickerReplyMarkup: + s.ReplyKeyboardMarkup.encodeFields(e) + case ReplyKeyboardRemoveSendStickerReplyMarkup: + s.ReplyKeyboardRemove.encodeFields(e) + case ForceReplySendStickerReplyMarkup: + s.ForceReply.encodeFields(e) + } +} + // Decode decodes SendStickerReplyMarkup from json. func (s *SendStickerReplyMarkup) Decode(d *jx.Decoder) error { if s == nil { @@ -36465,6 +36571,19 @@ func (s SendVenueReplyMarkup) Encode(e *jx.Encoder) { } } +func (s SendVenueReplyMarkup) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineKeyboardMarkupSendVenueReplyMarkup: + s.InlineKeyboardMarkup.encodeFields(e) + case ReplyKeyboardMarkupSendVenueReplyMarkup: + s.ReplyKeyboardMarkup.encodeFields(e) + case ReplyKeyboardRemoveSendVenueReplyMarkup: + s.ReplyKeyboardRemove.encodeFields(e) + case ForceReplySendVenueReplyMarkup: + s.ForceReply.encodeFields(e) + } +} + // Decode decodes SendVenueReplyMarkup from json. func (s *SendVenueReplyMarkup) Decode(d *jx.Decoder) error { if s == nil { @@ -37144,6 +37263,19 @@ func (s SendVideoNoteReplyMarkup) Encode(e *jx.Encoder) { } } +func (s SendVideoNoteReplyMarkup) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineKeyboardMarkupSendVideoNoteReplyMarkup: + s.InlineKeyboardMarkup.encodeFields(e) + case ReplyKeyboardMarkupSendVideoNoteReplyMarkup: + s.ReplyKeyboardMarkup.encodeFields(e) + case ReplyKeyboardRemoveSendVideoNoteReplyMarkup: + s.ReplyKeyboardRemove.encodeFields(e) + case ForceReplySendVideoNoteReplyMarkup: + s.ForceReply.encodeFields(e) + } +} + // Decode decodes SendVideoNoteReplyMarkup from json. func (s *SendVideoNoteReplyMarkup) Decode(d *jx.Decoder) error { if s == nil { @@ -37265,6 +37397,19 @@ func (s SendVideoReplyMarkup) Encode(e *jx.Encoder) { } } +func (s SendVideoReplyMarkup) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineKeyboardMarkupSendVideoReplyMarkup: + s.InlineKeyboardMarkup.encodeFields(e) + case ReplyKeyboardMarkupSendVideoReplyMarkup: + s.ReplyKeyboardMarkup.encodeFields(e) + case ReplyKeyboardRemoveSendVideoReplyMarkup: + s.ReplyKeyboardRemove.encodeFields(e) + case ForceReplySendVideoReplyMarkup: + s.ForceReply.encodeFields(e) + } +} + // Decode decodes SendVideoReplyMarkup from json. func (s *SendVideoReplyMarkup) Decode(d *jx.Decoder) error { if s == nil { @@ -37645,6 +37790,19 @@ func (s SendVoiceReplyMarkup) Encode(e *jx.Encoder) { } } +func (s SendVoiceReplyMarkup) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineKeyboardMarkupSendVoiceReplyMarkup: + s.InlineKeyboardMarkup.encodeFields(e) + case ReplyKeyboardMarkupSendVoiceReplyMarkup: + s.ReplyKeyboardMarkup.encodeFields(e) + case ReplyKeyboardRemoveSendVoiceReplyMarkup: + s.ReplyKeyboardRemove.encodeFields(e) + case ForceReplySendVoiceReplyMarkup: + s.ForceReply.encodeFields(e) + } +} + // Decode decodes SendVoiceReplyMarkup from json. func (s *SendVoiceReplyMarkup) Decode(d *jx.Decoder) error { if s == nil { diff --git a/internal/integration/sample_api/oas_client_gen.go b/internal/integration/sample_api/oas_client_gen.go index 995aceda4..47967a6a2 100644 --- a/internal/integration/sample_api/oas_client_gen.go +++ b/internal/integration/sample_api/oas_client_gen.go @@ -2287,6 +2287,75 @@ func (c *Client) sendTestFloatValidation(ctx context.Context, request *TestFloat return result, nil } +// TestInlineOneof invokes testInlineOneof operation. +// +// GET /testInlineOneof +func (c *Client) TestInlineOneof(ctx context.Context) (*TestInlineOneOf, error) { + res, err := c.sendTestInlineOneof(ctx) + _ = res + return res, err +} + +func (c *Client) sendTestInlineOneof(ctx context.Context) (res *TestInlineOneOf, err error) { + otelAttrs := []attribute.KeyValue{ + otelogen.OperationID("testInlineOneof"), + } + + // Run stopwatch. + startTime := time.Now() + defer func() { + // Use floating point division here for higher precision (instead of Millisecond method). + elapsedDuration := time.Since(startTime) + c.duration.Record(ctx, float64(float64(elapsedDuration)/float64(time.Millisecond)), metric.WithAttributes(otelAttrs...)) + }() + + // Increment request counter. + c.requests.Add(ctx, 1, metric.WithAttributes(otelAttrs...)) + + // Start a span for this request. + ctx, span := c.cfg.Tracer.Start(ctx, "TestInlineOneof", + trace.WithAttributes(otelAttrs...), + clientSpanKind, + ) + // Track stage for error reporting. + var stage string + defer func() { + if err != nil { + span.RecordError(err) + span.SetStatus(codes.Error, stage) + c.errors.Add(ctx, 1, metric.WithAttributes(otelAttrs...)) + } + span.End() + }() + + stage = "BuildURL" + u := uri.Clone(c.requestURL(ctx)) + var pathParts [1]string + pathParts[0] = "/testInlineOneof" + uri.AddPathParts(u, pathParts[:]...) + + stage = "EncodeRequest" + r, err := ht.NewRequest(ctx, "GET", u) + if err != nil { + return res, errors.Wrap(err, "create request") + } + + stage = "SendRequest" + resp, err := c.cfg.Client.Do(r) + if err != nil { + return res, errors.Wrap(err, "do request") + } + defer resp.Body.Close() + + stage = "DecodeResponse" + result, err := decodeTestInlineOneofResponse(resp) + if err != nil { + return res, errors.Wrap(err, "decode response") + } + + return result, nil +} + // TestNullableOneofs invokes testNullableOneofs operation. // // GET /testNullableOneofs diff --git a/internal/integration/sample_api/oas_faker_gen.go b/internal/integration/sample_api/oas_faker_gen.go index 589d1d904..0a8a347af 100644 --- a/internal/integration/sample_api/oas_faker_gen.go +++ b/internal/integration/sample_api/oas_faker_gen.go @@ -279,6 +279,72 @@ func (s *ID) SetFake() { s.SetString(variant) } +// SetFake set fake values. +func (s *InlineDiscriminatorOneOf) SetFake() { + { + { + s.Common = "string" + } + } + { + { + s.OneOf.SetFake() + } + } +} + +// SetFake set fake values. +func (s *InlineDiscriminatorOneOfSum) SetFake() { + var variant InlineOneOfBar + + { + variant.SetFake() + } + s.SetInlineOneOfBar(variant) +} + +// SetFake set fake values. +func (s *InlineOneOfBar) SetFake() { + { + { + s.Bar.SetFake() + } + } +} + +// SetFake set fake values. +func (s *InlineOneOfFoo) SetFake() { + { + { + s.Foo.SetFake() + } + } +} + +// SetFake set fake values. +func (s *InlineUniqueFieldsOneOf) SetFake() { + { + { + s.Common = "string" + } + } + { + { + s.OneOf.SetFake() + } + } +} + +// SetFake set fake values. +func (s *InlineUniqueFieldsOneOfSum) SetFake() { + var variant InlineOneOfFoo + + { + variant.SetFake() + } + s.SetInlineOneOfFoo(variant) +} + // SetFake set fake values. func (s *Issue143) SetFake() { var variant Issue1430 @@ -478,6 +544,54 @@ func (s *MaxPropertiesTest) SetFake() { } } +// SetFake set fake values. +func (s *MergeDiscriminatorOneOf) SetFake() { + { + { + s.Common = "string" + } + } + { + { + s.OneOf.SetFake() + } + } +} + +// SetFake set fake values. +func (s *MergeDiscriminatorOneOfSum) SetFake() { + var variant InlineOneOfBar + + { + variant.SetFake() + } + s.SetInlineOneOfBar(variant) +} + +// SetFake set fake values. +func (s *MergeUniqueFieldsOneOf) SetFake() { + { + { + s.Common = "string" + } + } + { + { + s.OneOf.SetFake() + } + } +} + +// SetFake set fake values. +func (s *MergeUniqueFieldsOneOfSum) SetFake() { + var variant InlineOneOfFoo + + { + variant.SetFake() + } + s.SetInlineOneOfFoo(variant) +} + // SetFake set fake values. func (s *NilInt) SetFake() { s.Null = true @@ -875,6 +989,24 @@ func (s *OptIPv6) SetFake() { s.SetTo(elem) } +// SetFake set fake values. +func (s *OptInlineDiscriminatorOneOf) SetFake() { + var elem InlineDiscriminatorOneOf + { + elem.SetFake() + } + s.SetTo(elem) +} + +// SetFake set fake values. +func (s *OptInlineUniqueFieldsOneOf) SetFake() { + var elem InlineUniqueFieldsOneOf + { + elem.SetFake() + } + s.SetTo(elem) +} + // SetFake set fake values. func (s *OptInt) SetFake() { var elem int @@ -929,6 +1061,24 @@ func (s *OptMaxPropertiesTest) SetFake() { s.SetTo(elem) } +// SetFake set fake values. +func (s *OptMergeDiscriminatorOneOf) SetFake() { + var elem MergeDiscriminatorOneOf + { + elem.SetFake() + } + s.SetTo(elem) +} + +// SetFake set fake values. +func (s *OptMergeUniqueFieldsOneOf) SetFake() { + var elem MergeUniqueFieldsOneOf + { + elem.SetFake() + } + s.SetTo(elem) +} + // SetFake set fake values. func (s *OptNilString) SetFake() { s.Null = true @@ -1417,6 +1567,30 @@ func (s *TestFloatValidation) SetFake() { } } +// SetFake set fake values. +func (s *TestInlineOneOf) SetFake() { + { + { + s.InlineDiscriminator.SetFake() + } + } + { + { + s.MergeDiscriminator.SetFake() + } + } + { + { + s.InlineUniqueFields.SetFake() + } + } + { + { + s.MergeUniqueFields.SetFake() + } + } +} + // SetFake set fake values. func (s *TestNullableOneofsCreated) SetFake() { var unwrapped OneOfWithNullable diff --git a/internal/integration/sample_api/oas_handlers_gen.go b/internal/integration/sample_api/oas_handlers_gen.go index cec788cce..db5b3d99f 100644 --- a/internal/integration/sample_api/oas_handlers_gen.go +++ b/internal/integration/sample_api/oas_handlers_gen.go @@ -2471,6 +2471,88 @@ func (s *Server) handleTestFloatValidationRequest(args [0]string, argsEscaped bo } } +// handleTestInlineOneofRequest handles testInlineOneof operation. +// +// GET /testInlineOneof +func (s *Server) handleTestInlineOneofRequest(args [0]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) { + otelAttrs := []attribute.KeyValue{ + otelogen.OperationID("testInlineOneof"), + semconv.HTTPMethodKey.String("GET"), + semconv.HTTPRouteKey.String("/testInlineOneof"), + } + + // Start a span for this request. + ctx, span := s.cfg.Tracer.Start(r.Context(), "TestInlineOneof", + trace.WithAttributes(otelAttrs...), + serverSpanKind, + ) + defer span.End() + + // Run stopwatch. + startTime := time.Now() + defer func() { + elapsedDuration := time.Since(startTime) + // Use floating point division here for higher precision (instead of Millisecond method). + s.duration.Record(ctx, float64(float64(elapsedDuration)/float64(time.Millisecond)), metric.WithAttributes(otelAttrs...)) + }() + + // Increment request counter. + s.requests.Add(ctx, 1, metric.WithAttributes(otelAttrs...)) + + var ( + recordError = func(stage string, err error) { + span.RecordError(err) + span.SetStatus(codes.Error, stage) + s.errors.Add(ctx, 1, metric.WithAttributes(otelAttrs...)) + } + err error + ) + + var response *TestInlineOneOf + if m := s.cfg.Middleware; m != nil { + mreq := middleware.Request{ + Context: ctx, + OperationName: "TestInlineOneof", + OperationID: "testInlineOneof", + Body: nil, + Params: middleware.Parameters{}, + Raw: r, + } + + type ( + Request = struct{} + Params = struct{} + Response = *TestInlineOneOf + ) + response, err = middleware.HookMiddleware[ + Request, + Params, + Response, + ]( + m, + mreq, + nil, + func(ctx context.Context, request Request, params Params) (response Response, err error) { + response, err = s.h.TestInlineOneof(ctx) + return response, err + }, + ) + } else { + response, err = s.h.TestInlineOneof(ctx) + } + if err != nil { + recordError("Internal", err) + s.cfg.ErrorHandler(ctx, w, r, err) + return + } + + if err := encodeTestInlineOneofResponse(response, w, span); err != nil { + recordError("EncodeResponse", err) + s.cfg.ErrorHandler(ctx, w, r, err) + return + } +} + // handleTestNullableOneofsRequest handles testNullableOneofs operation. // // GET /testNullableOneofs diff --git a/internal/integration/sample_api/oas_json_gen.go b/internal/integration/sample_api/oas_json_gen.go index 3cac3c721..e2fb67cfd 100644 --- a/internal/integration/sample_api/oas_json_gen.go +++ b/internal/integration/sample_api/oas_json_gen.go @@ -583,6 +583,15 @@ func (s DataDescription) Encode(e *jx.Encoder) { } } +func (s DataDescription) encodeFields(e *jx.Encoder) { + switch s.Type { + case DescriptionDetailedDataDescription: + s.DescriptionDetailed.encodeFields(e) + case DescriptionSimpleDataDescription: + s.DescriptionSimple.encodeFields(e) + } +} + // Decode decodes DataDescription from json. func (s *DataDescription) Decode(d *jx.Decoder) error { if s == nil { @@ -1403,29 +1412,539 @@ func (s *ID) Decode(d *jx.Decoder) error { if err != nil { return err } - s.Type = IntID - case jx.String: - v, err := d.Str() - s.String = string(v) - if err != nil { + s.Type = IntID + case jx.String: + v, err := d.Str() + s.String = string(v) + if err != nil { + return err + } + s.Type = StringID + default: + return errors.Errorf("unexpected json type %q", t) + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s ID) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *ID) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *InlineDiscriminatorOneOf) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *InlineDiscriminatorOneOf) encodeFields(e *jx.Encoder) { + { + e.FieldStart("common") + e.Str(s.Common) + } + s.OneOf.encodeFields(e) +} + +var jsonFieldsNameOfInlineDiscriminatorOneOf = [1]string{ + 0: "common", +} + +// Decode decodes InlineDiscriminatorOneOf from json. +func (s *InlineDiscriminatorOneOf) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode InlineDiscriminatorOneOf to nil") + } + if err := d.Capture(func(d *jx.Decoder) error { + return s.OneOf.Decode(d) + }); err != nil { + return errors.Wrap(err, "decode field OneOf") + } + var requiredBitSet [1]uint8 + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "common": + requiredBitSet[0] |= 1 << 0 + if err := func() error { + v, err := d.Str() + s.Common = string(v) + if err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"common\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode InlineDiscriminatorOneOf") + } + // Validate required fields. + var failures []validate.FieldError + for i, mask := range [1]uint8{ + 0b00000001, + } { + if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { + // Mask only required fields and check equality to mask using XOR. + // + // If XOR result is not zero, result is not equal to expected, so some fields are missed. + // Bits of fields which would be set are actually bits of missed fields. + missed := bits.OnesCount8(result) + for bitN := 0; bitN < missed; bitN++ { + bitIdx := bits.TrailingZeros8(result) + fieldIdx := i*8 + bitIdx + var name string + if fieldIdx < len(jsonFieldsNameOfInlineDiscriminatorOneOf) { + name = jsonFieldsNameOfInlineDiscriminatorOneOf[fieldIdx] + } else { + name = strconv.Itoa(fieldIdx) + } + failures = append(failures, validate.FieldError{ + Name: name, + Error: validate.ErrFieldRequired, + }) + // Reset bit. + result &^= 1 << bitIdx + } + } + } + if len(failures) > 0 { + return &validate.Error{Fields: failures} + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *InlineDiscriminatorOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *InlineDiscriminatorOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode encodes InlineDiscriminatorOneOfSum as json. +func (s InlineDiscriminatorOneOfSum) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s InlineDiscriminatorOneOfSum) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineOneOfBarInlineDiscriminatorOneOfSum: + e.FieldStart("kind") + e.Str("bar") + s.InlineOneOfBar.encodeFields(e) + case InlineOneOfFooInlineDiscriminatorOneOfSum: + e.FieldStart("kind") + e.Str("foo") + s.InlineOneOfFoo.encodeFields(e) + } +} + +// Decode decodes InlineDiscriminatorOneOfSum from json. +func (s *InlineDiscriminatorOneOfSum) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode InlineDiscriminatorOneOfSum to nil") + } + // Sum type discriminator. + if typ := d.Next(); typ != jx.Object { + return errors.Errorf("unexpected json type %q", typ) + } + + var found bool + if err := d.Capture(func(d *jx.Decoder) error { + return d.ObjBytes(func(d *jx.Decoder, key []byte) error { + if found { + return d.Skip() + } + switch string(key) { + case "kind": + typ, err := d.Str() + if err != nil { + return err + } + switch typ { + case "bar": + s.Type = InlineOneOfBarInlineDiscriminatorOneOfSum + found = true + case "foo": + s.Type = InlineOneOfFooInlineDiscriminatorOneOfSum + found = true + default: + return errors.Errorf("unknown type %s", typ) + } + return nil + } + return d.Skip() + }) + }); err != nil { + return errors.Wrap(err, "capture") + } + if !found { + return errors.New("unable to detect sum type variant") + } + switch s.Type { + case InlineOneOfFooInlineDiscriminatorOneOfSum: + if err := s.InlineOneOfFoo.Decode(d); err != nil { + return err + } + case InlineOneOfBarInlineDiscriminatorOneOfSum: + if err := s.InlineOneOfBar.Decode(d); err != nil { + return err + } + default: + return errors.Errorf("inferred invalid type: %s", s.Type) + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s InlineDiscriminatorOneOfSum) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *InlineDiscriminatorOneOfSum) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *InlineOneOfBar) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *InlineOneOfBar) encodeFields(e *jx.Encoder) { + { + if s.Bar.Set { + e.FieldStart("bar") + s.Bar.Encode(e) + } + } +} + +var jsonFieldsNameOfInlineOneOfBar = [1]string{ + 0: "bar", +} + +// Decode decodes InlineOneOfBar from json. +func (s *InlineOneOfBar) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode InlineOneOfBar to nil") + } + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "bar": + if err := func() error { + s.Bar.Reset() + if err := s.Bar.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"bar\"") + } + case "kind": + return d.Skip() + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode InlineOneOfBar") + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *InlineOneOfBar) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *InlineOneOfBar) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *InlineOneOfFoo) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *InlineOneOfFoo) encodeFields(e *jx.Encoder) { + { + if s.Foo.Set { + e.FieldStart("foo") + s.Foo.Encode(e) + } + } +} + +var jsonFieldsNameOfInlineOneOfFoo = [1]string{ + 0: "foo", +} + +// Decode decodes InlineOneOfFoo from json. +func (s *InlineOneOfFoo) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode InlineOneOfFoo to nil") + } + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "foo": + if err := func() error { + s.Foo.Reset() + if err := s.Foo.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"foo\"") + } + case "kind": + return d.Skip() + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode InlineOneOfFoo") + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *InlineOneOfFoo) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *InlineOneOfFoo) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *InlineUniqueFieldsOneOf) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *InlineUniqueFieldsOneOf) encodeFields(e *jx.Encoder) { + { + e.FieldStart("common") + e.Str(s.Common) + } + s.OneOf.encodeFields(e) +} + +var jsonFieldsNameOfInlineUniqueFieldsOneOf = [1]string{ + 0: "common", +} + +// Decode decodes InlineUniqueFieldsOneOf from json. +func (s *InlineUniqueFieldsOneOf) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode InlineUniqueFieldsOneOf to nil") + } + if err := d.Capture(func(d *jx.Decoder) error { + return s.OneOf.Decode(d) + }); err != nil { + return errors.Wrap(err, "decode field OneOf") + } + var requiredBitSet [1]uint8 + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "common": + requiredBitSet[0] |= 1 << 0 + if err := func() error { + v, err := d.Str() + s.Common = string(v) + if err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"common\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode InlineUniqueFieldsOneOf") + } + // Validate required fields. + var failures []validate.FieldError + for i, mask := range [1]uint8{ + 0b00000001, + } { + if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { + // Mask only required fields and check equality to mask using XOR. + // + // If XOR result is not zero, result is not equal to expected, so some fields are missed. + // Bits of fields which would be set are actually bits of missed fields. + missed := bits.OnesCount8(result) + for bitN := 0; bitN < missed; bitN++ { + bitIdx := bits.TrailingZeros8(result) + fieldIdx := i*8 + bitIdx + var name string + if fieldIdx < len(jsonFieldsNameOfInlineUniqueFieldsOneOf) { + name = jsonFieldsNameOfInlineUniqueFieldsOneOf[fieldIdx] + } else { + name = strconv.Itoa(fieldIdx) + } + failures = append(failures, validate.FieldError{ + Name: name, + Error: validate.ErrFieldRequired, + }) + // Reset bit. + result &^= 1 << bitIdx + } + } + } + if len(failures) > 0 { + return &validate.Error{Fields: failures} + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *InlineUniqueFieldsOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *InlineUniqueFieldsOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode encodes InlineUniqueFieldsOneOfSum as json. +func (s InlineUniqueFieldsOneOfSum) Encode(e *jx.Encoder) { + switch s.Type { + case InlineOneOfFooInlineUniqueFieldsOneOfSum: + s.InlineOneOfFoo.Encode(e) + case InlineOneOfBarInlineUniqueFieldsOneOfSum: + s.InlineOneOfBar.Encode(e) + } +} + +func (s InlineUniqueFieldsOneOfSum) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineOneOfFooInlineUniqueFieldsOneOfSum: + s.InlineOneOfFoo.encodeFields(e) + case InlineOneOfBarInlineUniqueFieldsOneOfSum: + s.InlineOneOfBar.encodeFields(e) + } +} + +// Decode decodes InlineUniqueFieldsOneOfSum from json. +func (s *InlineUniqueFieldsOneOfSum) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode InlineUniqueFieldsOneOfSum to nil") + } + // Sum type fields. + if typ := d.Next(); typ != jx.Object { + return errors.Errorf("unexpected json type %q", typ) + } + + var found bool + if err := d.Capture(func(d *jx.Decoder) error { + return d.ObjBytes(func(d *jx.Decoder, key []byte) error { + switch string(key) { + case "foo": + match := InlineOneOfFooInlineUniqueFieldsOneOfSum + if found && s.Type != match { + s.Type = "" + return errors.Errorf("multiple oneOf matches: (%v, %v)", s.Type, match) + } + found = true + s.Type = match + case "bar": + match := InlineOneOfBarInlineUniqueFieldsOneOfSum + if found && s.Type != match { + s.Type = "" + return errors.Errorf("multiple oneOf matches: (%v, %v)", s.Type, match) + } + found = true + s.Type = match + } + return d.Skip() + }) + }); err != nil { + return errors.Wrap(err, "capture") + } + if !found { + return errors.New("unable to detect sum type variant") + } + switch s.Type { + case InlineOneOfFooInlineUniqueFieldsOneOfSum: + if err := s.InlineOneOfFoo.Decode(d); err != nil { + return err + } + case InlineOneOfBarInlineUniqueFieldsOneOfSum: + if err := s.InlineOneOfBar.Decode(d); err != nil { return err } - s.Type = StringID default: - return errors.Errorf("unexpected json type %q", t) + return errors.Errorf("inferred invalid type: %s", s.Type) } return nil } // MarshalJSON implements stdjson.Marshaler. -func (s ID) MarshalJSON() ([]byte, error) { +func (s InlineUniqueFieldsOneOfSum) MarshalJSON() ([]byte, error) { e := jx.Encoder{} s.Encode(&e) return e.Bytes(), nil } // UnmarshalJSON implements stdjson.Unmarshaler. -func (s *ID) UnmarshalJSON(data []byte) error { +func (s *InlineUniqueFieldsOneOfSum) UnmarshalJSON(data []byte) error { d := jx.DecodeBytes(data) return s.Decode(d) } @@ -1444,6 +1963,19 @@ func (s Issue143) Encode(e *jx.Encoder) { } } +func (s Issue143) encodeFields(e *jx.Encoder) { + switch s.Type { + case Issue1430Issue143: + s.Issue1430.encodeFields(e) + case Issue1431Issue143: + s.Issue1431.encodeFields(e) + case Issue1432Issue143: + s.Issue1432.encodeFields(e) + case Issue1433Issue143: + s.Issue1433.encodeFields(e) + } +} + // Decode decodes Issue143 from json. func (s *Issue143) Decode(d *jx.Decoder) error { if s == nil { @@ -2057,25 +2589,25 @@ func (s *Issue1433) UnmarshalJSON(data []byte) error { // Encode encodes Issue943 as json. func (s Issue943) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s Issue943) encodeFields(e *jx.Encoder) { switch s.Type { case Issue943Variant1Issue943: - e.ObjStart() e.FieldStart("selector") e.Str("variant1") s.Issue943Variant1.encodeFields(e) - e.ObjEnd() case Issue943Variant2Issue943: - e.ObjStart() e.FieldStart("selector") e.Str("variant2") s.Issue943Variant2.encodeFields(e) - e.ObjEnd() case Issue943MapIssue943: - e.ObjStart() e.FieldStart("selector") e.Str("variant3") s.Issue943Map.encodeFields(e) - e.ObjEnd() } } @@ -2917,8 +3449,302 @@ func (s *MaxPropertiesTest) Decode(d *jx.Decoder) error { bitIdx := bits.TrailingZeros8(result) fieldIdx := i*8 + bitIdx var name string - if fieldIdx < len(jsonFieldsNameOfMaxPropertiesTest) { - name = jsonFieldsNameOfMaxPropertiesTest[fieldIdx] + if fieldIdx < len(jsonFieldsNameOfMaxPropertiesTest) { + name = jsonFieldsNameOfMaxPropertiesTest[fieldIdx] + } else { + name = strconv.Itoa(fieldIdx) + } + failures = append(failures, validate.FieldError{ + Name: name, + Error: validate.ErrFieldRequired, + }) + // Reset bit. + result &^= 1 << bitIdx + } + } + } + if len(failures) > 0 { + return &validate.Error{Fields: failures} + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *MaxPropertiesTest) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *MaxPropertiesTest) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *MergeDiscriminatorOneOf) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *MergeDiscriminatorOneOf) encodeFields(e *jx.Encoder) { + { + e.FieldStart("common") + e.Str(s.Common) + } + s.OneOf.encodeFields(e) +} + +var jsonFieldsNameOfMergeDiscriminatorOneOf = [1]string{ + 0: "common", +} + +// Decode decodes MergeDiscriminatorOneOf from json. +func (s *MergeDiscriminatorOneOf) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode MergeDiscriminatorOneOf to nil") + } + if err := d.Capture(func(d *jx.Decoder) error { + return s.OneOf.Decode(d) + }); err != nil { + return errors.Wrap(err, "decode field OneOf") + } + var requiredBitSet [1]uint8 + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "common": + requiredBitSet[0] |= 1 << 0 + if err := func() error { + v, err := d.Str() + s.Common = string(v) + if err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"common\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode MergeDiscriminatorOneOf") + } + // Validate required fields. + var failures []validate.FieldError + for i, mask := range [1]uint8{ + 0b00000001, + } { + if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { + // Mask only required fields and check equality to mask using XOR. + // + // If XOR result is not zero, result is not equal to expected, so some fields are missed. + // Bits of fields which would be set are actually bits of missed fields. + missed := bits.OnesCount8(result) + for bitN := 0; bitN < missed; bitN++ { + bitIdx := bits.TrailingZeros8(result) + fieldIdx := i*8 + bitIdx + var name string + if fieldIdx < len(jsonFieldsNameOfMergeDiscriminatorOneOf) { + name = jsonFieldsNameOfMergeDiscriminatorOneOf[fieldIdx] + } else { + name = strconv.Itoa(fieldIdx) + } + failures = append(failures, validate.FieldError{ + Name: name, + Error: validate.ErrFieldRequired, + }) + // Reset bit. + result &^= 1 << bitIdx + } + } + } + if len(failures) > 0 { + return &validate.Error{Fields: failures} + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *MergeDiscriminatorOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *MergeDiscriminatorOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode encodes MergeDiscriminatorOneOfSum as json. +func (s MergeDiscriminatorOneOfSum) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s MergeDiscriminatorOneOfSum) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineOneOfBarMergeDiscriminatorOneOfSum: + e.FieldStart("kind") + e.Str("bar") + s.InlineOneOfBar.encodeFields(e) + case InlineOneOfFooMergeDiscriminatorOneOfSum: + e.FieldStart("kind") + e.Str("foo") + s.InlineOneOfFoo.encodeFields(e) + } +} + +// Decode decodes MergeDiscriminatorOneOfSum from json. +func (s *MergeDiscriminatorOneOfSum) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode MergeDiscriminatorOneOfSum to nil") + } + // Sum type discriminator. + if typ := d.Next(); typ != jx.Object { + return errors.Errorf("unexpected json type %q", typ) + } + + var found bool + if err := d.Capture(func(d *jx.Decoder) error { + return d.ObjBytes(func(d *jx.Decoder, key []byte) error { + if found { + return d.Skip() + } + switch string(key) { + case "kind": + typ, err := d.Str() + if err != nil { + return err + } + switch typ { + case "bar": + s.Type = InlineOneOfBarMergeDiscriminatorOneOfSum + found = true + case "foo": + s.Type = InlineOneOfFooMergeDiscriminatorOneOfSum + found = true + default: + return errors.Errorf("unknown type %s", typ) + } + return nil + } + return d.Skip() + }) + }); err != nil { + return errors.Wrap(err, "capture") + } + if !found { + return errors.New("unable to detect sum type variant") + } + switch s.Type { + case InlineOneOfFooMergeDiscriminatorOneOfSum: + if err := s.InlineOneOfFoo.Decode(d); err != nil { + return err + } + case InlineOneOfBarMergeDiscriminatorOneOfSum: + if err := s.InlineOneOfBar.Decode(d); err != nil { + return err + } + default: + return errors.Errorf("inferred invalid type: %s", s.Type) + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s MergeDiscriminatorOneOfSum) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *MergeDiscriminatorOneOfSum) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *MergeUniqueFieldsOneOf) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *MergeUniqueFieldsOneOf) encodeFields(e *jx.Encoder) { + { + e.FieldStart("common") + e.Str(s.Common) + } + s.OneOf.encodeFields(e) +} + +var jsonFieldsNameOfMergeUniqueFieldsOneOf = [1]string{ + 0: "common", +} + +// Decode decodes MergeUniqueFieldsOneOf from json. +func (s *MergeUniqueFieldsOneOf) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode MergeUniqueFieldsOneOf to nil") + } + if err := d.Capture(func(d *jx.Decoder) error { + return s.OneOf.Decode(d) + }); err != nil { + return errors.Wrap(err, "decode field OneOf") + } + var requiredBitSet [1]uint8 + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "common": + requiredBitSet[0] |= 1 << 0 + if err := func() error { + v, err := d.Str() + s.Common = string(v) + if err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"common\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode MergeUniqueFieldsOneOf") + } + // Validate required fields. + var failures []validate.FieldError + for i, mask := range [1]uint8{ + 0b00000001, + } { + if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { + // Mask only required fields and check equality to mask using XOR. + // + // If XOR result is not zero, result is not equal to expected, so some fields are missed. + // Bits of fields which would be set are actually bits of missed fields. + missed := bits.OnesCount8(result) + for bitN := 0; bitN < missed; bitN++ { + bitIdx := bits.TrailingZeros8(result) + fieldIdx := i*8 + bitIdx + var name string + if fieldIdx < len(jsonFieldsNameOfMergeUniqueFieldsOneOf) { + name = jsonFieldsNameOfMergeUniqueFieldsOneOf[fieldIdx] } else { name = strconv.Itoa(fieldIdx) } @@ -2939,14 +3765,100 @@ func (s *MaxPropertiesTest) Decode(d *jx.Decoder) error { } // MarshalJSON implements stdjson.Marshaler. -func (s *MaxPropertiesTest) MarshalJSON() ([]byte, error) { +func (s *MergeUniqueFieldsOneOf) MarshalJSON() ([]byte, error) { e := jx.Encoder{} s.Encode(&e) return e.Bytes(), nil } // UnmarshalJSON implements stdjson.Unmarshaler. -func (s *MaxPropertiesTest) UnmarshalJSON(data []byte) error { +func (s *MergeUniqueFieldsOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode encodes MergeUniqueFieldsOneOfSum as json. +func (s MergeUniqueFieldsOneOfSum) Encode(e *jx.Encoder) { + switch s.Type { + case InlineOneOfFooMergeUniqueFieldsOneOfSum: + s.InlineOneOfFoo.Encode(e) + case InlineOneOfBarMergeUniqueFieldsOneOfSum: + s.InlineOneOfBar.Encode(e) + } +} + +func (s MergeUniqueFieldsOneOfSum) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineOneOfFooMergeUniqueFieldsOneOfSum: + s.InlineOneOfFoo.encodeFields(e) + case InlineOneOfBarMergeUniqueFieldsOneOfSum: + s.InlineOneOfBar.encodeFields(e) + } +} + +// Decode decodes MergeUniqueFieldsOneOfSum from json. +func (s *MergeUniqueFieldsOneOfSum) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode MergeUniqueFieldsOneOfSum to nil") + } + // Sum type fields. + if typ := d.Next(); typ != jx.Object { + return errors.Errorf("unexpected json type %q", typ) + } + + var found bool + if err := d.Capture(func(d *jx.Decoder) error { + return d.ObjBytes(func(d *jx.Decoder, key []byte) error { + switch string(key) { + case "foo": + match := InlineOneOfFooMergeUniqueFieldsOneOfSum + if found && s.Type != match { + s.Type = "" + return errors.Errorf("multiple oneOf matches: (%v, %v)", s.Type, match) + } + found = true + s.Type = match + case "bar": + match := InlineOneOfBarMergeUniqueFieldsOneOfSum + if found && s.Type != match { + s.Type = "" + return errors.Errorf("multiple oneOf matches: (%v, %v)", s.Type, match) + } + found = true + s.Type = match + } + return d.Skip() + }) + }); err != nil { + return errors.Wrap(err, "capture") + } + if !found { + return errors.New("unable to detect sum type variant") + } + switch s.Type { + case InlineOneOfFooMergeUniqueFieldsOneOfSum: + if err := s.InlineOneOfFoo.Decode(d); err != nil { + return err + } + case InlineOneOfBarMergeUniqueFieldsOneOfSum: + if err := s.InlineOneOfBar.Decode(d); err != nil { + return err + } + default: + return errors.Errorf("inferred invalid type: %s", s.Type) + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s MergeUniqueFieldsOneOfSum) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *MergeUniqueFieldsOneOfSum) UnmarshalJSON(data []byte) error { d := jx.DecodeBytes(data) return s.Decode(d) } @@ -3767,19 +4679,21 @@ func (s *OneOfBugs) UnmarshalJSON(data []byte) error { // Encode encodes OneOfMappingReference as json. func (s OneOfMappingReference) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s OneOfMappingReference) encodeFields(e *jx.Encoder) { switch s.Type { case OneOfMappingReferenceBOneOfMappingReference: - e.ObjStart() e.FieldStart("infoType") e.Str("extended") s.OneOfMappingReferenceB.encodeFields(e) - e.ObjEnd() case OneOfMappingReferenceAOneOfMappingReference: - e.ObjStart() e.FieldStart("infoType") e.Str("simple") s.OneOfMappingReferenceA.encodeFields(e) - e.ObjEnd() } } @@ -4407,6 +5321,15 @@ func (s OneVariantHasNoUniqueFields) Encode(e *jx.Encoder) { } } +func (s OneVariantHasNoUniqueFields) encodeFields(e *jx.Encoder) { + switch s.Type { + case OneVariantHasNoUniqueFields0OneVariantHasNoUniqueFields: + s.OneVariantHasNoUniqueFields0.encodeFields(e) + case OneVariantHasNoUniqueFields1OneVariantHasNoUniqueFields: + s.OneVariantHasNoUniqueFields1.encodeFields(e) + } +} + // Decode decodes OneVariantHasNoUniqueFields from json. func (s *OneVariantHasNoUniqueFields) Decode(d *jx.Decoder) error { if s == nil { @@ -5297,6 +6220,72 @@ func (s *OptIPv6) UnmarshalJSON(data []byte) error { return s.Decode(d) } +// Encode encodes InlineDiscriminatorOneOf as json. +func (o OptInlineDiscriminatorOneOf) Encode(e *jx.Encoder) { + if !o.Set { + return + } + o.Value.Encode(e) +} + +// Decode decodes InlineDiscriminatorOneOf from json. +func (o *OptInlineDiscriminatorOneOf) Decode(d *jx.Decoder) error { + if o == nil { + return errors.New("invalid: unable to decode OptInlineDiscriminatorOneOf to nil") + } + o.Set = true + if err := o.Value.Decode(d); err != nil { + return err + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s OptInlineDiscriminatorOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *OptInlineDiscriminatorOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode encodes InlineUniqueFieldsOneOf as json. +func (o OptInlineUniqueFieldsOneOf) Encode(e *jx.Encoder) { + if !o.Set { + return + } + o.Value.Encode(e) +} + +// Decode decodes InlineUniqueFieldsOneOf from json. +func (o *OptInlineUniqueFieldsOneOf) Decode(d *jx.Decoder) error { + if o == nil { + return errors.New("invalid: unable to decode OptInlineUniqueFieldsOneOf to nil") + } + o.Set = true + if err := o.Value.Decode(d); err != nil { + return err + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s OptInlineUniqueFieldsOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *OptInlineUniqueFieldsOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + // Encode encodes int as json. func (o OptInt) Encode(e *jx.Encoder) { if !o.Set { @@ -5500,6 +6489,72 @@ func (s *OptMaxPropertiesTest) UnmarshalJSON(data []byte) error { return s.Decode(d) } +// Encode encodes MergeDiscriminatorOneOf as json. +func (o OptMergeDiscriminatorOneOf) Encode(e *jx.Encoder) { + if !o.Set { + return + } + o.Value.Encode(e) +} + +// Decode decodes MergeDiscriminatorOneOf from json. +func (o *OptMergeDiscriminatorOneOf) Decode(d *jx.Decoder) error { + if o == nil { + return errors.New("invalid: unable to decode OptMergeDiscriminatorOneOf to nil") + } + o.Set = true + if err := o.Value.Decode(d); err != nil { + return err + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s OptMergeDiscriminatorOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *OptMergeDiscriminatorOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode encodes MergeUniqueFieldsOneOf as json. +func (o OptMergeUniqueFieldsOneOf) Encode(e *jx.Encoder) { + if !o.Set { + return + } + o.Value.Encode(e) +} + +// Decode decodes MergeUniqueFieldsOneOf from json. +func (o *OptMergeUniqueFieldsOneOf) Decode(d *jx.Decoder) error { + if o == nil { + return errors.New("invalid: unable to decode OptMergeUniqueFieldsOneOf to nil") + } + o.Set = true + if err := o.Value.Decode(d); err != nil { + return err + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s OptMergeUniqueFieldsOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *OptMergeUniqueFieldsOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + // Encode encodes string as json. func (o OptNilString) Encode(e *jx.Encoder) { if !o.Set { @@ -7690,6 +8745,120 @@ func (s *TestFloatValidation) UnmarshalJSON(data []byte) error { return s.Decode(d) } +// Encode implements json.Marshaler. +func (s *TestInlineOneOf) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *TestInlineOneOf) encodeFields(e *jx.Encoder) { + { + if s.InlineDiscriminator.Set { + e.FieldStart("inline_discriminator") + s.InlineDiscriminator.Encode(e) + } + } + { + if s.MergeDiscriminator.Set { + e.FieldStart("merge_discriminator") + s.MergeDiscriminator.Encode(e) + } + } + { + if s.InlineUniqueFields.Set { + e.FieldStart("inline_unique_fields") + s.InlineUniqueFields.Encode(e) + } + } + { + if s.MergeUniqueFields.Set { + e.FieldStart("merge_unique_fields") + s.MergeUniqueFields.Encode(e) + } + } +} + +var jsonFieldsNameOfTestInlineOneOf = [4]string{ + 0: "inline_discriminator", + 1: "merge_discriminator", + 2: "inline_unique_fields", + 3: "merge_unique_fields", +} + +// Decode decodes TestInlineOneOf from json. +func (s *TestInlineOneOf) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode TestInlineOneOf to nil") + } + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "inline_discriminator": + if err := func() error { + s.InlineDiscriminator.Reset() + if err := s.InlineDiscriminator.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"inline_discriminator\"") + } + case "merge_discriminator": + if err := func() error { + s.MergeDiscriminator.Reset() + if err := s.MergeDiscriminator.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"merge_discriminator\"") + } + case "inline_unique_fields": + if err := func() error { + s.InlineUniqueFields.Reset() + if err := s.InlineUniqueFields.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"inline_unique_fields\"") + } + case "merge_unique_fields": + if err := func() error { + s.MergeUniqueFields.Reset() + if err := s.MergeUniqueFields.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"merge_unique_fields\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode TestInlineOneOf") + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *TestInlineOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *TestInlineOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + // Encode encodes TestNullableOneofsCreated as json. func (s TestNullableOneofsCreated) Encode(e *jx.Encoder) { unwrapped := OneOfWithNullable(s) diff --git a/internal/integration/sample_api/oas_response_decoders_gen.go b/internal/integration/sample_api/oas_response_decoders_gen.go index 4eee2b373..5f1108161 100644 --- a/internal/integration/sample_api/oas_response_decoders_gen.go +++ b/internal/integration/sample_api/oas_response_decoders_gen.go @@ -1081,6 +1081,47 @@ func decodeTestFloatValidationResponse(resp *http.Response) (res *TestFloatValid return res, validate.UnexpectedStatusCode(resp.StatusCode) } +func decodeTestInlineOneofResponse(resp *http.Response) (res *TestInlineOneOf, _ error) { + switch resp.StatusCode { + case 200: + // Code 200. + ct, _, err := mime.ParseMediaType(resp.Header.Get("Content-Type")) + if err != nil { + return res, errors.Wrap(err, "parse media type") + } + switch { + case ct == "application/json": + buf, err := io.ReadAll(resp.Body) + if err != nil { + return res, err + } + d := jx.DecodeBytes(buf) + + var response TestInlineOneOf + if err := func() error { + if err := response.Decode(d); err != nil { + return err + } + if err := d.Skip(); err != io.EOF { + return errors.New("unexpected trailing data") + } + return nil + }(); err != nil { + err = &ogenerrors.DecodeBodyError{ + ContentType: ct, + Body: buf, + Err: err, + } + return res, err + } + return &response, nil + default: + return res, validate.InvalidContentType(ct) + } + } + return res, validate.UnexpectedStatusCode(resp.StatusCode) +} + func decodeTestNullableOneofsResponse(resp *http.Response) (res TestNullableOneofsRes, _ error) { switch resp.StatusCode { case 200: diff --git a/internal/integration/sample_api/oas_response_encoders_gen.go b/internal/integration/sample_api/oas_response_encoders_gen.go index c19d906c1..a3fc3c5e8 100644 --- a/internal/integration/sample_api/oas_response_encoders_gen.go +++ b/internal/integration/sample_api/oas_response_encoders_gen.go @@ -529,6 +529,19 @@ func encodeTestFloatValidationResponse(response *TestFloatValidationOK, w http.R return nil } +func encodeTestInlineOneofResponse(response *TestInlineOneOf, w http.ResponseWriter, span trace.Span) error { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(200) + span.SetStatus(codes.Ok, http.StatusText(200)) + + e := jx.GetEncoder() + response.Encode(e) + if _, err := e.WriteTo(w); err != nil { + return errors.Wrap(err, "write") + } + return nil +} + func encodeTestNullableOneofsResponse(response TestNullableOneofsRes, w http.ResponseWriter, span trace.Span) error { switch response := response.(type) { case *TestNullableOneofsOK: diff --git a/internal/integration/sample_api/oas_router_gen.go b/internal/integration/sample_api/oas_router_gen.go index 423ae9b1c..5672a8de5 100644 --- a/internal/integration/sample_api/oas_router_gen.go +++ b/internal/integration/sample_api/oas_router_gen.go @@ -628,6 +628,24 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { s.notAllowed(w, r, "POST") } + return + } + case 'I': // Prefix: "InlineOneof" + if l := len("InlineOneof"); len(elem) >= l && elem[0:l] == "InlineOneof" { + elem = elem[l:] + } else { + break + } + + if len(elem) == 0 { + // Leaf node. + switch r.Method { + case "GET": + s.handleTestInlineOneofRequest([0]string{}, elemIsEscaped, w, r) + default: + s.notAllowed(w, r, "GET") + } + return } case 'N': // Prefix: "NullableOneofs" @@ -1432,6 +1450,27 @@ func (s *Server) FindPath(method string, u *url.URL) (r Route, _ bool) { return } } + case 'I': // Prefix: "InlineOneof" + if l := len("InlineOneof"); len(elem) >= l && elem[0:l] == "InlineOneof" { + elem = elem[l:] + } else { + break + } + + if len(elem) == 0 { + switch method { + case "GET": + // Leaf: TestInlineOneof + r.name = "TestInlineOneof" + r.operationID = "testInlineOneof" + r.pathPattern = "/testInlineOneof" + r.args = args + r.count = 0 + return r, true + default: + return + } + } case 'N': // Prefix: "NullableOneofs" if l := len("NullableOneofs"); len(elem) >= l && elem[0:l] == "NullableOneofs" { elem = elem[l:] diff --git a/internal/integration/sample_api/oas_schemas_gen.go b/internal/integration/sample_api/oas_schemas_gen.go index 3a7f2f4d7..aa148b6aa 100644 --- a/internal/integration/sample_api/oas_schemas_gen.go +++ b/internal/integration/sample_api/oas_schemas_gen.go @@ -663,6 +663,224 @@ func NewIntID(v int) ID { return s } +// Ref: #/components/schemas/InlineDiscriminatorOneOf +type InlineDiscriminatorOneOf struct { + Common string `json:"common"` + OneOf InlineDiscriminatorOneOfSum +} + +// GetCommon returns the value of Common. +func (s *InlineDiscriminatorOneOf) GetCommon() string { + return s.Common +} + +// GetOneOf returns the value of OneOf. +func (s *InlineDiscriminatorOneOf) GetOneOf() InlineDiscriminatorOneOfSum { + return s.OneOf +} + +// SetCommon sets the value of Common. +func (s *InlineDiscriminatorOneOf) SetCommon(val string) { + s.Common = val +} + +// SetOneOf sets the value of OneOf. +func (s *InlineDiscriminatorOneOf) SetOneOf(val InlineDiscriminatorOneOfSum) { + s.OneOf = val +} + +// InlineDiscriminatorOneOfSum represents sum type. +type InlineDiscriminatorOneOfSum struct { + Type InlineDiscriminatorOneOfSumType // switch on this field + InlineOneOfFoo InlineOneOfFoo + InlineOneOfBar InlineOneOfBar +} + +// InlineDiscriminatorOneOfSumType is oneOf type of InlineDiscriminatorOneOfSum. +type InlineDiscriminatorOneOfSumType string + +// Possible values for InlineDiscriminatorOneOfSumType. +const ( + InlineOneOfFooInlineDiscriminatorOneOfSum InlineDiscriminatorOneOfSumType = "foo" + InlineOneOfBarInlineDiscriminatorOneOfSum InlineDiscriminatorOneOfSumType = "bar" +) + +// IsInlineOneOfFoo reports whether InlineDiscriminatorOneOfSum is InlineOneOfFoo. +func (s InlineDiscriminatorOneOfSum) IsInlineOneOfFoo() bool { + return s.Type == InlineOneOfFooInlineDiscriminatorOneOfSum +} + +// IsInlineOneOfBar reports whether InlineDiscriminatorOneOfSum is InlineOneOfBar. +func (s InlineDiscriminatorOneOfSum) IsInlineOneOfBar() bool { + return s.Type == InlineOneOfBarInlineDiscriminatorOneOfSum +} + +// SetInlineOneOfFoo sets InlineDiscriminatorOneOfSum to InlineOneOfFoo. +func (s *InlineDiscriminatorOneOfSum) SetInlineOneOfFoo(v InlineOneOfFoo) { + s.Type = InlineOneOfFooInlineDiscriminatorOneOfSum + s.InlineOneOfFoo = v +} + +// GetInlineOneOfFoo returns InlineOneOfFoo and true boolean if InlineDiscriminatorOneOfSum is InlineOneOfFoo. +func (s InlineDiscriminatorOneOfSum) GetInlineOneOfFoo() (v InlineOneOfFoo, ok bool) { + if !s.IsInlineOneOfFoo() { + return v, false + } + return s.InlineOneOfFoo, true +} + +// NewInlineOneOfFooInlineDiscriminatorOneOfSum returns new InlineDiscriminatorOneOfSum from InlineOneOfFoo. +func NewInlineOneOfFooInlineDiscriminatorOneOfSum(v InlineOneOfFoo) InlineDiscriminatorOneOfSum { + var s InlineDiscriminatorOneOfSum + s.SetInlineOneOfFoo(v) + return s +} + +// SetInlineOneOfBar sets InlineDiscriminatorOneOfSum to InlineOneOfBar. +func (s *InlineDiscriminatorOneOfSum) SetInlineOneOfBar(v InlineOneOfBar) { + s.Type = InlineOneOfBarInlineDiscriminatorOneOfSum + s.InlineOneOfBar = v +} + +// GetInlineOneOfBar returns InlineOneOfBar and true boolean if InlineDiscriminatorOneOfSum is InlineOneOfBar. +func (s InlineDiscriminatorOneOfSum) GetInlineOneOfBar() (v InlineOneOfBar, ok bool) { + if !s.IsInlineOneOfBar() { + return v, false + } + return s.InlineOneOfBar, true +} + +// NewInlineOneOfBarInlineDiscriminatorOneOfSum returns new InlineDiscriminatorOneOfSum from InlineOneOfBar. +func NewInlineOneOfBarInlineDiscriminatorOneOfSum(v InlineOneOfBar) InlineDiscriminatorOneOfSum { + var s InlineDiscriminatorOneOfSum + s.SetInlineOneOfBar(v) + return s +} + +// Ref: #/components/schemas/InlineOneOfBar +type InlineOneOfBar struct { + Bar OptString `json:"bar"` +} + +// GetBar returns the value of Bar. +func (s *InlineOneOfBar) GetBar() OptString { + return s.Bar +} + +// SetBar sets the value of Bar. +func (s *InlineOneOfBar) SetBar(val OptString) { + s.Bar = val +} + +// Ref: #/components/schemas/InlineOneOfFoo +type InlineOneOfFoo struct { + Foo OptString `json:"foo"` +} + +// GetFoo returns the value of Foo. +func (s *InlineOneOfFoo) GetFoo() OptString { + return s.Foo +} + +// SetFoo sets the value of Foo. +func (s *InlineOneOfFoo) SetFoo(val OptString) { + s.Foo = val +} + +// Ref: #/components/schemas/InlineUniqueFieldsOneOf +type InlineUniqueFieldsOneOf struct { + Common string `json:"common"` + OneOf InlineUniqueFieldsOneOfSum +} + +// GetCommon returns the value of Common. +func (s *InlineUniqueFieldsOneOf) GetCommon() string { + return s.Common +} + +// GetOneOf returns the value of OneOf. +func (s *InlineUniqueFieldsOneOf) GetOneOf() InlineUniqueFieldsOneOfSum { + return s.OneOf +} + +// SetCommon sets the value of Common. +func (s *InlineUniqueFieldsOneOf) SetCommon(val string) { + s.Common = val +} + +// SetOneOf sets the value of OneOf. +func (s *InlineUniqueFieldsOneOf) SetOneOf(val InlineUniqueFieldsOneOfSum) { + s.OneOf = val +} + +// InlineUniqueFieldsOneOfSum represents sum type. +type InlineUniqueFieldsOneOfSum struct { + Type InlineUniqueFieldsOneOfSumType // switch on this field + InlineOneOfFoo InlineOneOfFoo + InlineOneOfBar InlineOneOfBar +} + +// InlineUniqueFieldsOneOfSumType is oneOf type of InlineUniqueFieldsOneOfSum. +type InlineUniqueFieldsOneOfSumType string + +// Possible values for InlineUniqueFieldsOneOfSumType. +const ( + InlineOneOfFooInlineUniqueFieldsOneOfSum InlineUniqueFieldsOneOfSumType = "InlineOneOfFoo" + InlineOneOfBarInlineUniqueFieldsOneOfSum InlineUniqueFieldsOneOfSumType = "InlineOneOfBar" +) + +// IsInlineOneOfFoo reports whether InlineUniqueFieldsOneOfSum is InlineOneOfFoo. +func (s InlineUniqueFieldsOneOfSum) IsInlineOneOfFoo() bool { + return s.Type == InlineOneOfFooInlineUniqueFieldsOneOfSum +} + +// IsInlineOneOfBar reports whether InlineUniqueFieldsOneOfSum is InlineOneOfBar. +func (s InlineUniqueFieldsOneOfSum) IsInlineOneOfBar() bool { + return s.Type == InlineOneOfBarInlineUniqueFieldsOneOfSum +} + +// SetInlineOneOfFoo sets InlineUniqueFieldsOneOfSum to InlineOneOfFoo. +func (s *InlineUniqueFieldsOneOfSum) SetInlineOneOfFoo(v InlineOneOfFoo) { + s.Type = InlineOneOfFooInlineUniqueFieldsOneOfSum + s.InlineOneOfFoo = v +} + +// GetInlineOneOfFoo returns InlineOneOfFoo and true boolean if InlineUniqueFieldsOneOfSum is InlineOneOfFoo. +func (s InlineUniqueFieldsOneOfSum) GetInlineOneOfFoo() (v InlineOneOfFoo, ok bool) { + if !s.IsInlineOneOfFoo() { + return v, false + } + return s.InlineOneOfFoo, true +} + +// NewInlineOneOfFooInlineUniqueFieldsOneOfSum returns new InlineUniqueFieldsOneOfSum from InlineOneOfFoo. +func NewInlineOneOfFooInlineUniqueFieldsOneOfSum(v InlineOneOfFoo) InlineUniqueFieldsOneOfSum { + var s InlineUniqueFieldsOneOfSum + s.SetInlineOneOfFoo(v) + return s +} + +// SetInlineOneOfBar sets InlineUniqueFieldsOneOfSum to InlineOneOfBar. +func (s *InlineUniqueFieldsOneOfSum) SetInlineOneOfBar(v InlineOneOfBar) { + s.Type = InlineOneOfBarInlineUniqueFieldsOneOfSum + s.InlineOneOfBar = v +} + +// GetInlineOneOfBar returns InlineOneOfBar and true boolean if InlineUniqueFieldsOneOfSum is InlineOneOfBar. +func (s InlineUniqueFieldsOneOfSum) GetInlineOneOfBar() (v InlineOneOfBar, ok bool) { + if !s.IsInlineOneOfBar() { + return v, false + } + return s.InlineOneOfBar, true +} + +// NewInlineOneOfBarInlineUniqueFieldsOneOfSum returns new InlineUniqueFieldsOneOfSum from InlineOneOfBar. +func NewInlineOneOfBarInlineUniqueFieldsOneOfSum(v InlineOneOfBar) InlineUniqueFieldsOneOfSum { + var s InlineUniqueFieldsOneOfSum + s.SetInlineOneOfBar(v) + return s +} + // Ref: #/components/schemas/Issue143 // Issue143 represents sum type. type Issue143 struct { @@ -1212,6 +1430,196 @@ func (s *MaxPropertiesTest) SetOptionalC(val OptInt) { s.OptionalC = val } +// Merged schema. +// Ref: #/components/schemas/MergeDiscriminatorOneOf +type MergeDiscriminatorOneOf struct { + Common string `json:"common"` + OneOf MergeDiscriminatorOneOfSum +} + +// GetCommon returns the value of Common. +func (s *MergeDiscriminatorOneOf) GetCommon() string { + return s.Common +} + +// GetOneOf returns the value of OneOf. +func (s *MergeDiscriminatorOneOf) GetOneOf() MergeDiscriminatorOneOfSum { + return s.OneOf +} + +// SetCommon sets the value of Common. +func (s *MergeDiscriminatorOneOf) SetCommon(val string) { + s.Common = val +} + +// SetOneOf sets the value of OneOf. +func (s *MergeDiscriminatorOneOf) SetOneOf(val MergeDiscriminatorOneOfSum) { + s.OneOf = val +} + +// MergeDiscriminatorOneOfSum represents sum type. +type MergeDiscriminatorOneOfSum struct { + Type MergeDiscriminatorOneOfSumType // switch on this field + InlineOneOfFoo InlineOneOfFoo + InlineOneOfBar InlineOneOfBar +} + +// MergeDiscriminatorOneOfSumType is oneOf type of MergeDiscriminatorOneOfSum. +type MergeDiscriminatorOneOfSumType string + +// Possible values for MergeDiscriminatorOneOfSumType. +const ( + InlineOneOfFooMergeDiscriminatorOneOfSum MergeDiscriminatorOneOfSumType = "foo" + InlineOneOfBarMergeDiscriminatorOneOfSum MergeDiscriminatorOneOfSumType = "bar" +) + +// IsInlineOneOfFoo reports whether MergeDiscriminatorOneOfSum is InlineOneOfFoo. +func (s MergeDiscriminatorOneOfSum) IsInlineOneOfFoo() bool { + return s.Type == InlineOneOfFooMergeDiscriminatorOneOfSum +} + +// IsInlineOneOfBar reports whether MergeDiscriminatorOneOfSum is InlineOneOfBar. +func (s MergeDiscriminatorOneOfSum) IsInlineOneOfBar() bool { + return s.Type == InlineOneOfBarMergeDiscriminatorOneOfSum +} + +// SetInlineOneOfFoo sets MergeDiscriminatorOneOfSum to InlineOneOfFoo. +func (s *MergeDiscriminatorOneOfSum) SetInlineOneOfFoo(v InlineOneOfFoo) { + s.Type = InlineOneOfFooMergeDiscriminatorOneOfSum + s.InlineOneOfFoo = v +} + +// GetInlineOneOfFoo returns InlineOneOfFoo and true boolean if MergeDiscriminatorOneOfSum is InlineOneOfFoo. +func (s MergeDiscriminatorOneOfSum) GetInlineOneOfFoo() (v InlineOneOfFoo, ok bool) { + if !s.IsInlineOneOfFoo() { + return v, false + } + return s.InlineOneOfFoo, true +} + +// NewInlineOneOfFooMergeDiscriminatorOneOfSum returns new MergeDiscriminatorOneOfSum from InlineOneOfFoo. +func NewInlineOneOfFooMergeDiscriminatorOneOfSum(v InlineOneOfFoo) MergeDiscriminatorOneOfSum { + var s MergeDiscriminatorOneOfSum + s.SetInlineOneOfFoo(v) + return s +} + +// SetInlineOneOfBar sets MergeDiscriminatorOneOfSum to InlineOneOfBar. +func (s *MergeDiscriminatorOneOfSum) SetInlineOneOfBar(v InlineOneOfBar) { + s.Type = InlineOneOfBarMergeDiscriminatorOneOfSum + s.InlineOneOfBar = v +} + +// GetInlineOneOfBar returns InlineOneOfBar and true boolean if MergeDiscriminatorOneOfSum is InlineOneOfBar. +func (s MergeDiscriminatorOneOfSum) GetInlineOneOfBar() (v InlineOneOfBar, ok bool) { + if !s.IsInlineOneOfBar() { + return v, false + } + return s.InlineOneOfBar, true +} + +// NewInlineOneOfBarMergeDiscriminatorOneOfSum returns new MergeDiscriminatorOneOfSum from InlineOneOfBar. +func NewInlineOneOfBarMergeDiscriminatorOneOfSum(v InlineOneOfBar) MergeDiscriminatorOneOfSum { + var s MergeDiscriminatorOneOfSum + s.SetInlineOneOfBar(v) + return s +} + +// Merged schema. +// Ref: #/components/schemas/MergeUniqueFieldsOneOf +type MergeUniqueFieldsOneOf struct { + Common string `json:"common"` + OneOf MergeUniqueFieldsOneOfSum +} + +// GetCommon returns the value of Common. +func (s *MergeUniqueFieldsOneOf) GetCommon() string { + return s.Common +} + +// GetOneOf returns the value of OneOf. +func (s *MergeUniqueFieldsOneOf) GetOneOf() MergeUniqueFieldsOneOfSum { + return s.OneOf +} + +// SetCommon sets the value of Common. +func (s *MergeUniqueFieldsOneOf) SetCommon(val string) { + s.Common = val +} + +// SetOneOf sets the value of OneOf. +func (s *MergeUniqueFieldsOneOf) SetOneOf(val MergeUniqueFieldsOneOfSum) { + s.OneOf = val +} + +// MergeUniqueFieldsOneOfSum represents sum type. +type MergeUniqueFieldsOneOfSum struct { + Type MergeUniqueFieldsOneOfSumType // switch on this field + InlineOneOfFoo InlineOneOfFoo + InlineOneOfBar InlineOneOfBar +} + +// MergeUniqueFieldsOneOfSumType is oneOf type of MergeUniqueFieldsOneOfSum. +type MergeUniqueFieldsOneOfSumType string + +// Possible values for MergeUniqueFieldsOneOfSumType. +const ( + InlineOneOfFooMergeUniqueFieldsOneOfSum MergeUniqueFieldsOneOfSumType = "InlineOneOfFoo" + InlineOneOfBarMergeUniqueFieldsOneOfSum MergeUniqueFieldsOneOfSumType = "InlineOneOfBar" +) + +// IsInlineOneOfFoo reports whether MergeUniqueFieldsOneOfSum is InlineOneOfFoo. +func (s MergeUniqueFieldsOneOfSum) IsInlineOneOfFoo() bool { + return s.Type == InlineOneOfFooMergeUniqueFieldsOneOfSum +} + +// IsInlineOneOfBar reports whether MergeUniqueFieldsOneOfSum is InlineOneOfBar. +func (s MergeUniqueFieldsOneOfSum) IsInlineOneOfBar() bool { + return s.Type == InlineOneOfBarMergeUniqueFieldsOneOfSum +} + +// SetInlineOneOfFoo sets MergeUniqueFieldsOneOfSum to InlineOneOfFoo. +func (s *MergeUniqueFieldsOneOfSum) SetInlineOneOfFoo(v InlineOneOfFoo) { + s.Type = InlineOneOfFooMergeUniqueFieldsOneOfSum + s.InlineOneOfFoo = v +} + +// GetInlineOneOfFoo returns InlineOneOfFoo and true boolean if MergeUniqueFieldsOneOfSum is InlineOneOfFoo. +func (s MergeUniqueFieldsOneOfSum) GetInlineOneOfFoo() (v InlineOneOfFoo, ok bool) { + if !s.IsInlineOneOfFoo() { + return v, false + } + return s.InlineOneOfFoo, true +} + +// NewInlineOneOfFooMergeUniqueFieldsOneOfSum returns new MergeUniqueFieldsOneOfSum from InlineOneOfFoo. +func NewInlineOneOfFooMergeUniqueFieldsOneOfSum(v InlineOneOfFoo) MergeUniqueFieldsOneOfSum { + var s MergeUniqueFieldsOneOfSum + s.SetInlineOneOfFoo(v) + return s +} + +// SetInlineOneOfBar sets MergeUniqueFieldsOneOfSum to InlineOneOfBar. +func (s *MergeUniqueFieldsOneOfSum) SetInlineOneOfBar(v InlineOneOfBar) { + s.Type = InlineOneOfBarMergeUniqueFieldsOneOfSum + s.InlineOneOfBar = v +} + +// GetInlineOneOfBar returns InlineOneOfBar and true boolean if MergeUniqueFieldsOneOfSum is InlineOneOfBar. +func (s MergeUniqueFieldsOneOfSum) GetInlineOneOfBar() (v InlineOneOfBar, ok bool) { + if !s.IsInlineOneOfBar() { + return v, false + } + return s.InlineOneOfBar, true +} + +// NewInlineOneOfBarMergeUniqueFieldsOneOfSum returns new MergeUniqueFieldsOneOfSum from InlineOneOfBar. +func NewInlineOneOfBarMergeUniqueFieldsOneOfSum(v InlineOneOfBar) MergeUniqueFieldsOneOfSum { + var s MergeUniqueFieldsOneOfSum + s.SetInlineOneOfBar(v) + return s +} + // NewNilInt returns new NilInt with value set to v. func NewNilInt(v int) NilInt { return NilInt{ @@ -2995,6 +3403,98 @@ func (o OptIPv6) Or(d netip.Addr) netip.Addr { return d } +// NewOptInlineDiscriminatorOneOf returns new OptInlineDiscriminatorOneOf with value set to v. +func NewOptInlineDiscriminatorOneOf(v InlineDiscriminatorOneOf) OptInlineDiscriminatorOneOf { + return OptInlineDiscriminatorOneOf{ + Value: v, + Set: true, + } +} + +// OptInlineDiscriminatorOneOf is optional InlineDiscriminatorOneOf. +type OptInlineDiscriminatorOneOf struct { + Value InlineDiscriminatorOneOf + Set bool +} + +// IsSet returns true if OptInlineDiscriminatorOneOf was set. +func (o OptInlineDiscriminatorOneOf) IsSet() bool { return o.Set } + +// Reset unsets value. +func (o *OptInlineDiscriminatorOneOf) Reset() { + var v InlineDiscriminatorOneOf + o.Value = v + o.Set = false +} + +// SetTo sets value to v. +func (o *OptInlineDiscriminatorOneOf) SetTo(v InlineDiscriminatorOneOf) { + o.Set = true + o.Value = v +} + +// Get returns value and boolean that denotes whether value was set. +func (o OptInlineDiscriminatorOneOf) Get() (v InlineDiscriminatorOneOf, ok bool) { + if !o.Set { + return v, false + } + return o.Value, true +} + +// Or returns value if set, or given parameter if does not. +func (o OptInlineDiscriminatorOneOf) Or(d InlineDiscriminatorOneOf) InlineDiscriminatorOneOf { + if v, ok := o.Get(); ok { + return v + } + return d +} + +// NewOptInlineUniqueFieldsOneOf returns new OptInlineUniqueFieldsOneOf with value set to v. +func NewOptInlineUniqueFieldsOneOf(v InlineUniqueFieldsOneOf) OptInlineUniqueFieldsOneOf { + return OptInlineUniqueFieldsOneOf{ + Value: v, + Set: true, + } +} + +// OptInlineUniqueFieldsOneOf is optional InlineUniqueFieldsOneOf. +type OptInlineUniqueFieldsOneOf struct { + Value InlineUniqueFieldsOneOf + Set bool +} + +// IsSet returns true if OptInlineUniqueFieldsOneOf was set. +func (o OptInlineUniqueFieldsOneOf) IsSet() bool { return o.Set } + +// Reset unsets value. +func (o *OptInlineUniqueFieldsOneOf) Reset() { + var v InlineUniqueFieldsOneOf + o.Value = v + o.Set = false +} + +// SetTo sets value to v. +func (o *OptInlineUniqueFieldsOneOf) SetTo(v InlineUniqueFieldsOneOf) { + o.Set = true + o.Value = v +} + +// Get returns value and boolean that denotes whether value was set. +func (o OptInlineUniqueFieldsOneOf) Get() (v InlineUniqueFieldsOneOf, ok bool) { + if !o.Set { + return v, false + } + return o.Value, true +} + +// Or returns value if set, or given parameter if does not. +func (o OptInlineUniqueFieldsOneOf) Or(d InlineUniqueFieldsOneOf) InlineUniqueFieldsOneOf { + if v, ok := o.Get(); ok { + return v + } + return d +} + // NewOptInt returns new OptInt with value set to v. func NewOptInt(v int) OptInt { return OptInt{ @@ -3271,6 +3771,98 @@ func (o OptMaxPropertiesTest) Or(d MaxPropertiesTest) MaxPropertiesTest { return d } +// NewOptMergeDiscriminatorOneOf returns new OptMergeDiscriminatorOneOf with value set to v. +func NewOptMergeDiscriminatorOneOf(v MergeDiscriminatorOneOf) OptMergeDiscriminatorOneOf { + return OptMergeDiscriminatorOneOf{ + Value: v, + Set: true, + } +} + +// OptMergeDiscriminatorOneOf is optional MergeDiscriminatorOneOf. +type OptMergeDiscriminatorOneOf struct { + Value MergeDiscriminatorOneOf + Set bool +} + +// IsSet returns true if OptMergeDiscriminatorOneOf was set. +func (o OptMergeDiscriminatorOneOf) IsSet() bool { return o.Set } + +// Reset unsets value. +func (o *OptMergeDiscriminatorOneOf) Reset() { + var v MergeDiscriminatorOneOf + o.Value = v + o.Set = false +} + +// SetTo sets value to v. +func (o *OptMergeDiscriminatorOneOf) SetTo(v MergeDiscriminatorOneOf) { + o.Set = true + o.Value = v +} + +// Get returns value and boolean that denotes whether value was set. +func (o OptMergeDiscriminatorOneOf) Get() (v MergeDiscriminatorOneOf, ok bool) { + if !o.Set { + return v, false + } + return o.Value, true +} + +// Or returns value if set, or given parameter if does not. +func (o OptMergeDiscriminatorOneOf) Or(d MergeDiscriminatorOneOf) MergeDiscriminatorOneOf { + if v, ok := o.Get(); ok { + return v + } + return d +} + +// NewOptMergeUniqueFieldsOneOf returns new OptMergeUniqueFieldsOneOf with value set to v. +func NewOptMergeUniqueFieldsOneOf(v MergeUniqueFieldsOneOf) OptMergeUniqueFieldsOneOf { + return OptMergeUniqueFieldsOneOf{ + Value: v, + Set: true, + } +} + +// OptMergeUniqueFieldsOneOf is optional MergeUniqueFieldsOneOf. +type OptMergeUniqueFieldsOneOf struct { + Value MergeUniqueFieldsOneOf + Set bool +} + +// IsSet returns true if OptMergeUniqueFieldsOneOf was set. +func (o OptMergeUniqueFieldsOneOf) IsSet() bool { return o.Set } + +// Reset unsets value. +func (o *OptMergeUniqueFieldsOneOf) Reset() { + var v MergeUniqueFieldsOneOf + o.Value = v + o.Set = false +} + +// SetTo sets value to v. +func (o *OptMergeUniqueFieldsOneOf) SetTo(v MergeUniqueFieldsOneOf) { + o.Set = true + o.Value = v +} + +// Get returns value and boolean that denotes whether value was set. +func (o OptMergeUniqueFieldsOneOf) Get() (v MergeUniqueFieldsOneOf, ok bool) { + if !o.Set { + return v, false + } + return o.Value, true +} + +// Or returns value if set, or given parameter if does not. +func (o OptMergeUniqueFieldsOneOf) Or(d MergeUniqueFieldsOneOf) MergeUniqueFieldsOneOf { + if v, ok := o.Get(); ok { + return v + } + return d +} + // NewOptNilString returns new OptNilString with value set to v. func NewOptNilString(v string) OptNilString { return OptNilString{ @@ -4827,6 +5419,54 @@ func (s *TestFloatValidation) SetMultipleOf(val float64) { // TestFloatValidationOK is response for TestFloatValidation operation. type TestFloatValidationOK struct{} +// Ref: #/components/schemas/TestInlineOneOf +type TestInlineOneOf struct { + InlineDiscriminator OptInlineDiscriminatorOneOf `json:"inline_discriminator"` + MergeDiscriminator OptMergeDiscriminatorOneOf `json:"merge_discriminator"` + InlineUniqueFields OptInlineUniqueFieldsOneOf `json:"inline_unique_fields"` + MergeUniqueFields OptMergeUniqueFieldsOneOf `json:"merge_unique_fields"` +} + +// GetInlineDiscriminator returns the value of InlineDiscriminator. +func (s *TestInlineOneOf) GetInlineDiscriminator() OptInlineDiscriminatorOneOf { + return s.InlineDiscriminator +} + +// GetMergeDiscriminator returns the value of MergeDiscriminator. +func (s *TestInlineOneOf) GetMergeDiscriminator() OptMergeDiscriminatorOneOf { + return s.MergeDiscriminator +} + +// GetInlineUniqueFields returns the value of InlineUniqueFields. +func (s *TestInlineOneOf) GetInlineUniqueFields() OptInlineUniqueFieldsOneOf { + return s.InlineUniqueFields +} + +// GetMergeUniqueFields returns the value of MergeUniqueFields. +func (s *TestInlineOneOf) GetMergeUniqueFields() OptMergeUniqueFieldsOneOf { + return s.MergeUniqueFields +} + +// SetInlineDiscriminator sets the value of InlineDiscriminator. +func (s *TestInlineOneOf) SetInlineDiscriminator(val OptInlineDiscriminatorOneOf) { + s.InlineDiscriminator = val +} + +// SetMergeDiscriminator sets the value of MergeDiscriminator. +func (s *TestInlineOneOf) SetMergeDiscriminator(val OptMergeDiscriminatorOneOf) { + s.MergeDiscriminator = val +} + +// SetInlineUniqueFields sets the value of InlineUniqueFields. +func (s *TestInlineOneOf) SetInlineUniqueFields(val OptInlineUniqueFieldsOneOf) { + s.InlineUniqueFields = val +} + +// SetMergeUniqueFields sets the value of MergeUniqueFields. +func (s *TestInlineOneOf) SetMergeUniqueFields(val OptMergeUniqueFieldsOneOf) { + s.MergeUniqueFields = val +} + type TestNullableOneofsCreated OneOfWithNullable func (*TestNullableOneofsCreated) testNullableOneofsRes() {} diff --git a/internal/integration/sample_api/oas_server_gen.go b/internal/integration/sample_api/oas_server_gen.go index 7118f5121..0d0f1661f 100644 --- a/internal/integration/sample_api/oas_server_gen.go +++ b/internal/integration/sample_api/oas_server_gen.go @@ -132,6 +132,10 @@ type Handler interface { // // POST /testFloatValidation TestFloatValidation(ctx context.Context, req *TestFloatValidation) error + // TestInlineOneof implements testInlineOneof operation. + // + // GET /testInlineOneof + TestInlineOneof(ctx context.Context) (*TestInlineOneOf, error) // TestNullableOneofs implements testNullableOneofs operation. // // GET /testNullableOneofs diff --git a/internal/integration/sample_api/oas_test_examples_gen_test.go b/internal/integration/sample_api/oas_test_examples_gen_test.go index f23202294..be1228716 100644 --- a/internal/integration/sample_api/oas_test_examples_gen_test.go +++ b/internal/integration/sample_api/oas_test_examples_gen_test.go @@ -145,6 +145,78 @@ func TestID_EncodeDecode(t *testing.T) { var typ2 ID require.NoError(t, typ2.Decode(jx.DecodeBytes(data))) } +func TestInlineDiscriminatorOneOf_EncodeDecode(t *testing.T) { + var typ InlineDiscriminatorOneOf + typ.SetFake() + + e := jx.Encoder{} + typ.Encode(&e) + data := e.Bytes() + require.True(t, std.Valid(data), "Encoded: %s", data) + + var typ2 InlineDiscriminatorOneOf + require.NoError(t, typ2.Decode(jx.DecodeBytes(data))) +} +func TestInlineDiscriminatorOneOfSum_EncodeDecode(t *testing.T) { + var typ InlineDiscriminatorOneOfSum + typ.SetFake() + + e := jx.Encoder{} + typ.Encode(&e) + data := e.Bytes() + require.True(t, std.Valid(data), "Encoded: %s", data) + + var typ2 InlineDiscriminatorOneOfSum + require.NoError(t, typ2.Decode(jx.DecodeBytes(data))) +} +func TestInlineOneOfBar_EncodeDecode(t *testing.T) { + var typ InlineOneOfBar + typ.SetFake() + + e := jx.Encoder{} + typ.Encode(&e) + data := e.Bytes() + require.True(t, std.Valid(data), "Encoded: %s", data) + + var typ2 InlineOneOfBar + require.NoError(t, typ2.Decode(jx.DecodeBytes(data))) +} +func TestInlineOneOfFoo_EncodeDecode(t *testing.T) { + var typ InlineOneOfFoo + typ.SetFake() + + e := jx.Encoder{} + typ.Encode(&e) + data := e.Bytes() + require.True(t, std.Valid(data), "Encoded: %s", data) + + var typ2 InlineOneOfFoo + require.NoError(t, typ2.Decode(jx.DecodeBytes(data))) +} +func TestInlineUniqueFieldsOneOf_EncodeDecode(t *testing.T) { + var typ InlineUniqueFieldsOneOf + typ.SetFake() + + e := jx.Encoder{} + typ.Encode(&e) + data := e.Bytes() + require.True(t, std.Valid(data), "Encoded: %s", data) + + var typ2 InlineUniqueFieldsOneOf + require.NoError(t, typ2.Decode(jx.DecodeBytes(data))) +} +func TestInlineUniqueFieldsOneOfSum_EncodeDecode(t *testing.T) { + var typ InlineUniqueFieldsOneOfSum + typ.SetFake() + + e := jx.Encoder{} + typ.Encode(&e) + data := e.Bytes() + require.True(t, std.Valid(data), "Encoded: %s", data) + + var typ2 InlineUniqueFieldsOneOfSum + require.NoError(t, typ2.Decode(jx.DecodeBytes(data))) +} func TestIssue143_EncodeDecode(t *testing.T) { var typ Issue143 typ.SetFake() @@ -319,6 +391,54 @@ func TestMaxPropertiesTest_EncodeDecode(t *testing.T) { var typ2 MaxPropertiesTest require.NoError(t, typ2.Decode(jx.DecodeBytes(data))) } +func TestMergeDiscriminatorOneOf_EncodeDecode(t *testing.T) { + var typ MergeDiscriminatorOneOf + typ.SetFake() + + e := jx.Encoder{} + typ.Encode(&e) + data := e.Bytes() + require.True(t, std.Valid(data), "Encoded: %s", data) + + var typ2 MergeDiscriminatorOneOf + require.NoError(t, typ2.Decode(jx.DecodeBytes(data))) +} +func TestMergeDiscriminatorOneOfSum_EncodeDecode(t *testing.T) { + var typ MergeDiscriminatorOneOfSum + typ.SetFake() + + e := jx.Encoder{} + typ.Encode(&e) + data := e.Bytes() + require.True(t, std.Valid(data), "Encoded: %s", data) + + var typ2 MergeDiscriminatorOneOfSum + require.NoError(t, typ2.Decode(jx.DecodeBytes(data))) +} +func TestMergeUniqueFieldsOneOf_EncodeDecode(t *testing.T) { + var typ MergeUniqueFieldsOneOf + typ.SetFake() + + e := jx.Encoder{} + typ.Encode(&e) + data := e.Bytes() + require.True(t, std.Valid(data), "Encoded: %s", data) + + var typ2 MergeUniqueFieldsOneOf + require.NoError(t, typ2.Decode(jx.DecodeBytes(data))) +} +func TestMergeUniqueFieldsOneOfSum_EncodeDecode(t *testing.T) { + var typ MergeUniqueFieldsOneOfSum + typ.SetFake() + + e := jx.Encoder{} + typ.Encode(&e) + data := e.Bytes() + require.True(t, std.Valid(data), "Encoded: %s", data) + + var typ2 MergeUniqueFieldsOneOfSum + require.NoError(t, typ2.Decode(jx.DecodeBytes(data))) +} func TestNoAdditionalPropertiesTest_EncodeDecode(t *testing.T) { var typ NoAdditionalPropertiesTest typ.SetFake() @@ -777,6 +897,18 @@ func TestTestFloatValidation_EncodeDecode(t *testing.T) { var typ2 TestFloatValidation require.NoError(t, typ2.Decode(jx.DecodeBytes(data))) } +func TestTestInlineOneOf_EncodeDecode(t *testing.T) { + var typ TestInlineOneOf + typ.SetFake() + + e := jx.Encoder{} + typ.Encode(&e) + data := e.Bytes() + require.True(t, std.Valid(data), "Encoded: %s", data) + + var typ2 TestInlineOneOf + require.NoError(t, typ2.Decode(jx.DecodeBytes(data))) +} func TestTestNullableOneofsCreated_EncodeDecode(t *testing.T) { var typ TestNullableOneofsCreated typ.SetFake() diff --git a/internal/integration/sample_api/oas_unimplemented_gen.go b/internal/integration/sample_api/oas_unimplemented_gen.go index 8d098c830..0e76e9b88 100644 --- a/internal/integration/sample_api/oas_unimplemented_gen.go +++ b/internal/integration/sample_api/oas_unimplemented_gen.go @@ -212,6 +212,13 @@ func (UnimplementedHandler) TestFloatValidation(ctx context.Context, req *TestFl return ht.ErrNotImplemented } +// TestInlineOneof implements testInlineOneof operation. +// +// GET /testInlineOneof +func (UnimplementedHandler) TestInlineOneof(ctx context.Context) (r *TestInlineOneOf, _ error) { + return r, ht.ErrNotImplemented +} + // TestNullableOneofs implements testNullableOneofs operation. // // GET /testNullableOneofs diff --git a/internal/integration/sample_api_nc/oas_handlers_gen.go b/internal/integration/sample_api_nc/oas_handlers_gen.go index cec788cce..db5b3d99f 100644 --- a/internal/integration/sample_api_nc/oas_handlers_gen.go +++ b/internal/integration/sample_api_nc/oas_handlers_gen.go @@ -2471,6 +2471,88 @@ func (s *Server) handleTestFloatValidationRequest(args [0]string, argsEscaped bo } } +// handleTestInlineOneofRequest handles testInlineOneof operation. +// +// GET /testInlineOneof +func (s *Server) handleTestInlineOneofRequest(args [0]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) { + otelAttrs := []attribute.KeyValue{ + otelogen.OperationID("testInlineOneof"), + semconv.HTTPMethodKey.String("GET"), + semconv.HTTPRouteKey.String("/testInlineOneof"), + } + + // Start a span for this request. + ctx, span := s.cfg.Tracer.Start(r.Context(), "TestInlineOneof", + trace.WithAttributes(otelAttrs...), + serverSpanKind, + ) + defer span.End() + + // Run stopwatch. + startTime := time.Now() + defer func() { + elapsedDuration := time.Since(startTime) + // Use floating point division here for higher precision (instead of Millisecond method). + s.duration.Record(ctx, float64(float64(elapsedDuration)/float64(time.Millisecond)), metric.WithAttributes(otelAttrs...)) + }() + + // Increment request counter. + s.requests.Add(ctx, 1, metric.WithAttributes(otelAttrs...)) + + var ( + recordError = func(stage string, err error) { + span.RecordError(err) + span.SetStatus(codes.Error, stage) + s.errors.Add(ctx, 1, metric.WithAttributes(otelAttrs...)) + } + err error + ) + + var response *TestInlineOneOf + if m := s.cfg.Middleware; m != nil { + mreq := middleware.Request{ + Context: ctx, + OperationName: "TestInlineOneof", + OperationID: "testInlineOneof", + Body: nil, + Params: middleware.Parameters{}, + Raw: r, + } + + type ( + Request = struct{} + Params = struct{} + Response = *TestInlineOneOf + ) + response, err = middleware.HookMiddleware[ + Request, + Params, + Response, + ]( + m, + mreq, + nil, + func(ctx context.Context, request Request, params Params) (response Response, err error) { + response, err = s.h.TestInlineOneof(ctx) + return response, err + }, + ) + } else { + response, err = s.h.TestInlineOneof(ctx) + } + if err != nil { + recordError("Internal", err) + s.cfg.ErrorHandler(ctx, w, r, err) + return + } + + if err := encodeTestInlineOneofResponse(response, w, span); err != nil { + recordError("EncodeResponse", err) + s.cfg.ErrorHandler(ctx, w, r, err) + return + } +} + // handleTestNullableOneofsRequest handles testNullableOneofs operation. // // GET /testNullableOneofs diff --git a/internal/integration/sample_api_nc/oas_json_gen.go b/internal/integration/sample_api_nc/oas_json_gen.go index 3cac3c721..e2fb67cfd 100644 --- a/internal/integration/sample_api_nc/oas_json_gen.go +++ b/internal/integration/sample_api_nc/oas_json_gen.go @@ -583,6 +583,15 @@ func (s DataDescription) Encode(e *jx.Encoder) { } } +func (s DataDescription) encodeFields(e *jx.Encoder) { + switch s.Type { + case DescriptionDetailedDataDescription: + s.DescriptionDetailed.encodeFields(e) + case DescriptionSimpleDataDescription: + s.DescriptionSimple.encodeFields(e) + } +} + // Decode decodes DataDescription from json. func (s *DataDescription) Decode(d *jx.Decoder) error { if s == nil { @@ -1403,29 +1412,539 @@ func (s *ID) Decode(d *jx.Decoder) error { if err != nil { return err } - s.Type = IntID - case jx.String: - v, err := d.Str() - s.String = string(v) - if err != nil { + s.Type = IntID + case jx.String: + v, err := d.Str() + s.String = string(v) + if err != nil { + return err + } + s.Type = StringID + default: + return errors.Errorf("unexpected json type %q", t) + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s ID) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *ID) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *InlineDiscriminatorOneOf) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *InlineDiscriminatorOneOf) encodeFields(e *jx.Encoder) { + { + e.FieldStart("common") + e.Str(s.Common) + } + s.OneOf.encodeFields(e) +} + +var jsonFieldsNameOfInlineDiscriminatorOneOf = [1]string{ + 0: "common", +} + +// Decode decodes InlineDiscriminatorOneOf from json. +func (s *InlineDiscriminatorOneOf) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode InlineDiscriminatorOneOf to nil") + } + if err := d.Capture(func(d *jx.Decoder) error { + return s.OneOf.Decode(d) + }); err != nil { + return errors.Wrap(err, "decode field OneOf") + } + var requiredBitSet [1]uint8 + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "common": + requiredBitSet[0] |= 1 << 0 + if err := func() error { + v, err := d.Str() + s.Common = string(v) + if err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"common\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode InlineDiscriminatorOneOf") + } + // Validate required fields. + var failures []validate.FieldError + for i, mask := range [1]uint8{ + 0b00000001, + } { + if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { + // Mask only required fields and check equality to mask using XOR. + // + // If XOR result is not zero, result is not equal to expected, so some fields are missed. + // Bits of fields which would be set are actually bits of missed fields. + missed := bits.OnesCount8(result) + for bitN := 0; bitN < missed; bitN++ { + bitIdx := bits.TrailingZeros8(result) + fieldIdx := i*8 + bitIdx + var name string + if fieldIdx < len(jsonFieldsNameOfInlineDiscriminatorOneOf) { + name = jsonFieldsNameOfInlineDiscriminatorOneOf[fieldIdx] + } else { + name = strconv.Itoa(fieldIdx) + } + failures = append(failures, validate.FieldError{ + Name: name, + Error: validate.ErrFieldRequired, + }) + // Reset bit. + result &^= 1 << bitIdx + } + } + } + if len(failures) > 0 { + return &validate.Error{Fields: failures} + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *InlineDiscriminatorOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *InlineDiscriminatorOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode encodes InlineDiscriminatorOneOfSum as json. +func (s InlineDiscriminatorOneOfSum) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s InlineDiscriminatorOneOfSum) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineOneOfBarInlineDiscriminatorOneOfSum: + e.FieldStart("kind") + e.Str("bar") + s.InlineOneOfBar.encodeFields(e) + case InlineOneOfFooInlineDiscriminatorOneOfSum: + e.FieldStart("kind") + e.Str("foo") + s.InlineOneOfFoo.encodeFields(e) + } +} + +// Decode decodes InlineDiscriminatorOneOfSum from json. +func (s *InlineDiscriminatorOneOfSum) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode InlineDiscriminatorOneOfSum to nil") + } + // Sum type discriminator. + if typ := d.Next(); typ != jx.Object { + return errors.Errorf("unexpected json type %q", typ) + } + + var found bool + if err := d.Capture(func(d *jx.Decoder) error { + return d.ObjBytes(func(d *jx.Decoder, key []byte) error { + if found { + return d.Skip() + } + switch string(key) { + case "kind": + typ, err := d.Str() + if err != nil { + return err + } + switch typ { + case "bar": + s.Type = InlineOneOfBarInlineDiscriminatorOneOfSum + found = true + case "foo": + s.Type = InlineOneOfFooInlineDiscriminatorOneOfSum + found = true + default: + return errors.Errorf("unknown type %s", typ) + } + return nil + } + return d.Skip() + }) + }); err != nil { + return errors.Wrap(err, "capture") + } + if !found { + return errors.New("unable to detect sum type variant") + } + switch s.Type { + case InlineOneOfFooInlineDiscriminatorOneOfSum: + if err := s.InlineOneOfFoo.Decode(d); err != nil { + return err + } + case InlineOneOfBarInlineDiscriminatorOneOfSum: + if err := s.InlineOneOfBar.Decode(d); err != nil { + return err + } + default: + return errors.Errorf("inferred invalid type: %s", s.Type) + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s InlineDiscriminatorOneOfSum) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *InlineDiscriminatorOneOfSum) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *InlineOneOfBar) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *InlineOneOfBar) encodeFields(e *jx.Encoder) { + { + if s.Bar.Set { + e.FieldStart("bar") + s.Bar.Encode(e) + } + } +} + +var jsonFieldsNameOfInlineOneOfBar = [1]string{ + 0: "bar", +} + +// Decode decodes InlineOneOfBar from json. +func (s *InlineOneOfBar) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode InlineOneOfBar to nil") + } + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "bar": + if err := func() error { + s.Bar.Reset() + if err := s.Bar.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"bar\"") + } + case "kind": + return d.Skip() + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode InlineOneOfBar") + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *InlineOneOfBar) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *InlineOneOfBar) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *InlineOneOfFoo) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *InlineOneOfFoo) encodeFields(e *jx.Encoder) { + { + if s.Foo.Set { + e.FieldStart("foo") + s.Foo.Encode(e) + } + } +} + +var jsonFieldsNameOfInlineOneOfFoo = [1]string{ + 0: "foo", +} + +// Decode decodes InlineOneOfFoo from json. +func (s *InlineOneOfFoo) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode InlineOneOfFoo to nil") + } + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "foo": + if err := func() error { + s.Foo.Reset() + if err := s.Foo.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"foo\"") + } + case "kind": + return d.Skip() + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode InlineOneOfFoo") + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *InlineOneOfFoo) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *InlineOneOfFoo) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *InlineUniqueFieldsOneOf) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *InlineUniqueFieldsOneOf) encodeFields(e *jx.Encoder) { + { + e.FieldStart("common") + e.Str(s.Common) + } + s.OneOf.encodeFields(e) +} + +var jsonFieldsNameOfInlineUniqueFieldsOneOf = [1]string{ + 0: "common", +} + +// Decode decodes InlineUniqueFieldsOneOf from json. +func (s *InlineUniqueFieldsOneOf) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode InlineUniqueFieldsOneOf to nil") + } + if err := d.Capture(func(d *jx.Decoder) error { + return s.OneOf.Decode(d) + }); err != nil { + return errors.Wrap(err, "decode field OneOf") + } + var requiredBitSet [1]uint8 + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "common": + requiredBitSet[0] |= 1 << 0 + if err := func() error { + v, err := d.Str() + s.Common = string(v) + if err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"common\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode InlineUniqueFieldsOneOf") + } + // Validate required fields. + var failures []validate.FieldError + for i, mask := range [1]uint8{ + 0b00000001, + } { + if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { + // Mask only required fields and check equality to mask using XOR. + // + // If XOR result is not zero, result is not equal to expected, so some fields are missed. + // Bits of fields which would be set are actually bits of missed fields. + missed := bits.OnesCount8(result) + for bitN := 0; bitN < missed; bitN++ { + bitIdx := bits.TrailingZeros8(result) + fieldIdx := i*8 + bitIdx + var name string + if fieldIdx < len(jsonFieldsNameOfInlineUniqueFieldsOneOf) { + name = jsonFieldsNameOfInlineUniqueFieldsOneOf[fieldIdx] + } else { + name = strconv.Itoa(fieldIdx) + } + failures = append(failures, validate.FieldError{ + Name: name, + Error: validate.ErrFieldRequired, + }) + // Reset bit. + result &^= 1 << bitIdx + } + } + } + if len(failures) > 0 { + return &validate.Error{Fields: failures} + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *InlineUniqueFieldsOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *InlineUniqueFieldsOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode encodes InlineUniqueFieldsOneOfSum as json. +func (s InlineUniqueFieldsOneOfSum) Encode(e *jx.Encoder) { + switch s.Type { + case InlineOneOfFooInlineUniqueFieldsOneOfSum: + s.InlineOneOfFoo.Encode(e) + case InlineOneOfBarInlineUniqueFieldsOneOfSum: + s.InlineOneOfBar.Encode(e) + } +} + +func (s InlineUniqueFieldsOneOfSum) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineOneOfFooInlineUniqueFieldsOneOfSum: + s.InlineOneOfFoo.encodeFields(e) + case InlineOneOfBarInlineUniqueFieldsOneOfSum: + s.InlineOneOfBar.encodeFields(e) + } +} + +// Decode decodes InlineUniqueFieldsOneOfSum from json. +func (s *InlineUniqueFieldsOneOfSum) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode InlineUniqueFieldsOneOfSum to nil") + } + // Sum type fields. + if typ := d.Next(); typ != jx.Object { + return errors.Errorf("unexpected json type %q", typ) + } + + var found bool + if err := d.Capture(func(d *jx.Decoder) error { + return d.ObjBytes(func(d *jx.Decoder, key []byte) error { + switch string(key) { + case "foo": + match := InlineOneOfFooInlineUniqueFieldsOneOfSum + if found && s.Type != match { + s.Type = "" + return errors.Errorf("multiple oneOf matches: (%v, %v)", s.Type, match) + } + found = true + s.Type = match + case "bar": + match := InlineOneOfBarInlineUniqueFieldsOneOfSum + if found && s.Type != match { + s.Type = "" + return errors.Errorf("multiple oneOf matches: (%v, %v)", s.Type, match) + } + found = true + s.Type = match + } + return d.Skip() + }) + }); err != nil { + return errors.Wrap(err, "capture") + } + if !found { + return errors.New("unable to detect sum type variant") + } + switch s.Type { + case InlineOneOfFooInlineUniqueFieldsOneOfSum: + if err := s.InlineOneOfFoo.Decode(d); err != nil { + return err + } + case InlineOneOfBarInlineUniqueFieldsOneOfSum: + if err := s.InlineOneOfBar.Decode(d); err != nil { return err } - s.Type = StringID default: - return errors.Errorf("unexpected json type %q", t) + return errors.Errorf("inferred invalid type: %s", s.Type) } return nil } // MarshalJSON implements stdjson.Marshaler. -func (s ID) MarshalJSON() ([]byte, error) { +func (s InlineUniqueFieldsOneOfSum) MarshalJSON() ([]byte, error) { e := jx.Encoder{} s.Encode(&e) return e.Bytes(), nil } // UnmarshalJSON implements stdjson.Unmarshaler. -func (s *ID) UnmarshalJSON(data []byte) error { +func (s *InlineUniqueFieldsOneOfSum) UnmarshalJSON(data []byte) error { d := jx.DecodeBytes(data) return s.Decode(d) } @@ -1444,6 +1963,19 @@ func (s Issue143) Encode(e *jx.Encoder) { } } +func (s Issue143) encodeFields(e *jx.Encoder) { + switch s.Type { + case Issue1430Issue143: + s.Issue1430.encodeFields(e) + case Issue1431Issue143: + s.Issue1431.encodeFields(e) + case Issue1432Issue143: + s.Issue1432.encodeFields(e) + case Issue1433Issue143: + s.Issue1433.encodeFields(e) + } +} + // Decode decodes Issue143 from json. func (s *Issue143) Decode(d *jx.Decoder) error { if s == nil { @@ -2057,25 +2589,25 @@ func (s *Issue1433) UnmarshalJSON(data []byte) error { // Encode encodes Issue943 as json. func (s Issue943) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s Issue943) encodeFields(e *jx.Encoder) { switch s.Type { case Issue943Variant1Issue943: - e.ObjStart() e.FieldStart("selector") e.Str("variant1") s.Issue943Variant1.encodeFields(e) - e.ObjEnd() case Issue943Variant2Issue943: - e.ObjStart() e.FieldStart("selector") e.Str("variant2") s.Issue943Variant2.encodeFields(e) - e.ObjEnd() case Issue943MapIssue943: - e.ObjStart() e.FieldStart("selector") e.Str("variant3") s.Issue943Map.encodeFields(e) - e.ObjEnd() } } @@ -2917,8 +3449,302 @@ func (s *MaxPropertiesTest) Decode(d *jx.Decoder) error { bitIdx := bits.TrailingZeros8(result) fieldIdx := i*8 + bitIdx var name string - if fieldIdx < len(jsonFieldsNameOfMaxPropertiesTest) { - name = jsonFieldsNameOfMaxPropertiesTest[fieldIdx] + if fieldIdx < len(jsonFieldsNameOfMaxPropertiesTest) { + name = jsonFieldsNameOfMaxPropertiesTest[fieldIdx] + } else { + name = strconv.Itoa(fieldIdx) + } + failures = append(failures, validate.FieldError{ + Name: name, + Error: validate.ErrFieldRequired, + }) + // Reset bit. + result &^= 1 << bitIdx + } + } + } + if len(failures) > 0 { + return &validate.Error{Fields: failures} + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *MaxPropertiesTest) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *MaxPropertiesTest) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *MergeDiscriminatorOneOf) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *MergeDiscriminatorOneOf) encodeFields(e *jx.Encoder) { + { + e.FieldStart("common") + e.Str(s.Common) + } + s.OneOf.encodeFields(e) +} + +var jsonFieldsNameOfMergeDiscriminatorOneOf = [1]string{ + 0: "common", +} + +// Decode decodes MergeDiscriminatorOneOf from json. +func (s *MergeDiscriminatorOneOf) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode MergeDiscriminatorOneOf to nil") + } + if err := d.Capture(func(d *jx.Decoder) error { + return s.OneOf.Decode(d) + }); err != nil { + return errors.Wrap(err, "decode field OneOf") + } + var requiredBitSet [1]uint8 + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "common": + requiredBitSet[0] |= 1 << 0 + if err := func() error { + v, err := d.Str() + s.Common = string(v) + if err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"common\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode MergeDiscriminatorOneOf") + } + // Validate required fields. + var failures []validate.FieldError + for i, mask := range [1]uint8{ + 0b00000001, + } { + if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { + // Mask only required fields and check equality to mask using XOR. + // + // If XOR result is not zero, result is not equal to expected, so some fields are missed. + // Bits of fields which would be set are actually bits of missed fields. + missed := bits.OnesCount8(result) + for bitN := 0; bitN < missed; bitN++ { + bitIdx := bits.TrailingZeros8(result) + fieldIdx := i*8 + bitIdx + var name string + if fieldIdx < len(jsonFieldsNameOfMergeDiscriminatorOneOf) { + name = jsonFieldsNameOfMergeDiscriminatorOneOf[fieldIdx] + } else { + name = strconv.Itoa(fieldIdx) + } + failures = append(failures, validate.FieldError{ + Name: name, + Error: validate.ErrFieldRequired, + }) + // Reset bit. + result &^= 1 << bitIdx + } + } + } + if len(failures) > 0 { + return &validate.Error{Fields: failures} + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *MergeDiscriminatorOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *MergeDiscriminatorOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode encodes MergeDiscriminatorOneOfSum as json. +func (s MergeDiscriminatorOneOfSum) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s MergeDiscriminatorOneOfSum) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineOneOfBarMergeDiscriminatorOneOfSum: + e.FieldStart("kind") + e.Str("bar") + s.InlineOneOfBar.encodeFields(e) + case InlineOneOfFooMergeDiscriminatorOneOfSum: + e.FieldStart("kind") + e.Str("foo") + s.InlineOneOfFoo.encodeFields(e) + } +} + +// Decode decodes MergeDiscriminatorOneOfSum from json. +func (s *MergeDiscriminatorOneOfSum) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode MergeDiscriminatorOneOfSum to nil") + } + // Sum type discriminator. + if typ := d.Next(); typ != jx.Object { + return errors.Errorf("unexpected json type %q", typ) + } + + var found bool + if err := d.Capture(func(d *jx.Decoder) error { + return d.ObjBytes(func(d *jx.Decoder, key []byte) error { + if found { + return d.Skip() + } + switch string(key) { + case "kind": + typ, err := d.Str() + if err != nil { + return err + } + switch typ { + case "bar": + s.Type = InlineOneOfBarMergeDiscriminatorOneOfSum + found = true + case "foo": + s.Type = InlineOneOfFooMergeDiscriminatorOneOfSum + found = true + default: + return errors.Errorf("unknown type %s", typ) + } + return nil + } + return d.Skip() + }) + }); err != nil { + return errors.Wrap(err, "capture") + } + if !found { + return errors.New("unable to detect sum type variant") + } + switch s.Type { + case InlineOneOfFooMergeDiscriminatorOneOfSum: + if err := s.InlineOneOfFoo.Decode(d); err != nil { + return err + } + case InlineOneOfBarMergeDiscriminatorOneOfSum: + if err := s.InlineOneOfBar.Decode(d); err != nil { + return err + } + default: + return errors.Errorf("inferred invalid type: %s", s.Type) + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s MergeDiscriminatorOneOfSum) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *MergeDiscriminatorOneOfSum) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *MergeUniqueFieldsOneOf) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *MergeUniqueFieldsOneOf) encodeFields(e *jx.Encoder) { + { + e.FieldStart("common") + e.Str(s.Common) + } + s.OneOf.encodeFields(e) +} + +var jsonFieldsNameOfMergeUniqueFieldsOneOf = [1]string{ + 0: "common", +} + +// Decode decodes MergeUniqueFieldsOneOf from json. +func (s *MergeUniqueFieldsOneOf) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode MergeUniqueFieldsOneOf to nil") + } + if err := d.Capture(func(d *jx.Decoder) error { + return s.OneOf.Decode(d) + }); err != nil { + return errors.Wrap(err, "decode field OneOf") + } + var requiredBitSet [1]uint8 + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "common": + requiredBitSet[0] |= 1 << 0 + if err := func() error { + v, err := d.Str() + s.Common = string(v) + if err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"common\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode MergeUniqueFieldsOneOf") + } + // Validate required fields. + var failures []validate.FieldError + for i, mask := range [1]uint8{ + 0b00000001, + } { + if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { + // Mask only required fields and check equality to mask using XOR. + // + // If XOR result is not zero, result is not equal to expected, so some fields are missed. + // Bits of fields which would be set are actually bits of missed fields. + missed := bits.OnesCount8(result) + for bitN := 0; bitN < missed; bitN++ { + bitIdx := bits.TrailingZeros8(result) + fieldIdx := i*8 + bitIdx + var name string + if fieldIdx < len(jsonFieldsNameOfMergeUniqueFieldsOneOf) { + name = jsonFieldsNameOfMergeUniqueFieldsOneOf[fieldIdx] } else { name = strconv.Itoa(fieldIdx) } @@ -2939,14 +3765,100 @@ func (s *MaxPropertiesTest) Decode(d *jx.Decoder) error { } // MarshalJSON implements stdjson.Marshaler. -func (s *MaxPropertiesTest) MarshalJSON() ([]byte, error) { +func (s *MergeUniqueFieldsOneOf) MarshalJSON() ([]byte, error) { e := jx.Encoder{} s.Encode(&e) return e.Bytes(), nil } // UnmarshalJSON implements stdjson.Unmarshaler. -func (s *MaxPropertiesTest) UnmarshalJSON(data []byte) error { +func (s *MergeUniqueFieldsOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode encodes MergeUniqueFieldsOneOfSum as json. +func (s MergeUniqueFieldsOneOfSum) Encode(e *jx.Encoder) { + switch s.Type { + case InlineOneOfFooMergeUniqueFieldsOneOfSum: + s.InlineOneOfFoo.Encode(e) + case InlineOneOfBarMergeUniqueFieldsOneOfSum: + s.InlineOneOfBar.Encode(e) + } +} + +func (s MergeUniqueFieldsOneOfSum) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineOneOfFooMergeUniqueFieldsOneOfSum: + s.InlineOneOfFoo.encodeFields(e) + case InlineOneOfBarMergeUniqueFieldsOneOfSum: + s.InlineOneOfBar.encodeFields(e) + } +} + +// Decode decodes MergeUniqueFieldsOneOfSum from json. +func (s *MergeUniqueFieldsOneOfSum) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode MergeUniqueFieldsOneOfSum to nil") + } + // Sum type fields. + if typ := d.Next(); typ != jx.Object { + return errors.Errorf("unexpected json type %q", typ) + } + + var found bool + if err := d.Capture(func(d *jx.Decoder) error { + return d.ObjBytes(func(d *jx.Decoder, key []byte) error { + switch string(key) { + case "foo": + match := InlineOneOfFooMergeUniqueFieldsOneOfSum + if found && s.Type != match { + s.Type = "" + return errors.Errorf("multiple oneOf matches: (%v, %v)", s.Type, match) + } + found = true + s.Type = match + case "bar": + match := InlineOneOfBarMergeUniqueFieldsOneOfSum + if found && s.Type != match { + s.Type = "" + return errors.Errorf("multiple oneOf matches: (%v, %v)", s.Type, match) + } + found = true + s.Type = match + } + return d.Skip() + }) + }); err != nil { + return errors.Wrap(err, "capture") + } + if !found { + return errors.New("unable to detect sum type variant") + } + switch s.Type { + case InlineOneOfFooMergeUniqueFieldsOneOfSum: + if err := s.InlineOneOfFoo.Decode(d); err != nil { + return err + } + case InlineOneOfBarMergeUniqueFieldsOneOfSum: + if err := s.InlineOneOfBar.Decode(d); err != nil { + return err + } + default: + return errors.Errorf("inferred invalid type: %s", s.Type) + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s MergeUniqueFieldsOneOfSum) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *MergeUniqueFieldsOneOfSum) UnmarshalJSON(data []byte) error { d := jx.DecodeBytes(data) return s.Decode(d) } @@ -3767,19 +4679,21 @@ func (s *OneOfBugs) UnmarshalJSON(data []byte) error { // Encode encodes OneOfMappingReference as json. func (s OneOfMappingReference) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s OneOfMappingReference) encodeFields(e *jx.Encoder) { switch s.Type { case OneOfMappingReferenceBOneOfMappingReference: - e.ObjStart() e.FieldStart("infoType") e.Str("extended") s.OneOfMappingReferenceB.encodeFields(e) - e.ObjEnd() case OneOfMappingReferenceAOneOfMappingReference: - e.ObjStart() e.FieldStart("infoType") e.Str("simple") s.OneOfMappingReferenceA.encodeFields(e) - e.ObjEnd() } } @@ -4407,6 +5321,15 @@ func (s OneVariantHasNoUniqueFields) Encode(e *jx.Encoder) { } } +func (s OneVariantHasNoUniqueFields) encodeFields(e *jx.Encoder) { + switch s.Type { + case OneVariantHasNoUniqueFields0OneVariantHasNoUniqueFields: + s.OneVariantHasNoUniqueFields0.encodeFields(e) + case OneVariantHasNoUniqueFields1OneVariantHasNoUniqueFields: + s.OneVariantHasNoUniqueFields1.encodeFields(e) + } +} + // Decode decodes OneVariantHasNoUniqueFields from json. func (s *OneVariantHasNoUniqueFields) Decode(d *jx.Decoder) error { if s == nil { @@ -5297,6 +6220,72 @@ func (s *OptIPv6) UnmarshalJSON(data []byte) error { return s.Decode(d) } +// Encode encodes InlineDiscriminatorOneOf as json. +func (o OptInlineDiscriminatorOneOf) Encode(e *jx.Encoder) { + if !o.Set { + return + } + o.Value.Encode(e) +} + +// Decode decodes InlineDiscriminatorOneOf from json. +func (o *OptInlineDiscriminatorOneOf) Decode(d *jx.Decoder) error { + if o == nil { + return errors.New("invalid: unable to decode OptInlineDiscriminatorOneOf to nil") + } + o.Set = true + if err := o.Value.Decode(d); err != nil { + return err + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s OptInlineDiscriminatorOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *OptInlineDiscriminatorOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode encodes InlineUniqueFieldsOneOf as json. +func (o OptInlineUniqueFieldsOneOf) Encode(e *jx.Encoder) { + if !o.Set { + return + } + o.Value.Encode(e) +} + +// Decode decodes InlineUniqueFieldsOneOf from json. +func (o *OptInlineUniqueFieldsOneOf) Decode(d *jx.Decoder) error { + if o == nil { + return errors.New("invalid: unable to decode OptInlineUniqueFieldsOneOf to nil") + } + o.Set = true + if err := o.Value.Decode(d); err != nil { + return err + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s OptInlineUniqueFieldsOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *OptInlineUniqueFieldsOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + // Encode encodes int as json. func (o OptInt) Encode(e *jx.Encoder) { if !o.Set { @@ -5500,6 +6489,72 @@ func (s *OptMaxPropertiesTest) UnmarshalJSON(data []byte) error { return s.Decode(d) } +// Encode encodes MergeDiscriminatorOneOf as json. +func (o OptMergeDiscriminatorOneOf) Encode(e *jx.Encoder) { + if !o.Set { + return + } + o.Value.Encode(e) +} + +// Decode decodes MergeDiscriminatorOneOf from json. +func (o *OptMergeDiscriminatorOneOf) Decode(d *jx.Decoder) error { + if o == nil { + return errors.New("invalid: unable to decode OptMergeDiscriminatorOneOf to nil") + } + o.Set = true + if err := o.Value.Decode(d); err != nil { + return err + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s OptMergeDiscriminatorOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *OptMergeDiscriminatorOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode encodes MergeUniqueFieldsOneOf as json. +func (o OptMergeUniqueFieldsOneOf) Encode(e *jx.Encoder) { + if !o.Set { + return + } + o.Value.Encode(e) +} + +// Decode decodes MergeUniqueFieldsOneOf from json. +func (o *OptMergeUniqueFieldsOneOf) Decode(d *jx.Decoder) error { + if o == nil { + return errors.New("invalid: unable to decode OptMergeUniqueFieldsOneOf to nil") + } + o.Set = true + if err := o.Value.Decode(d); err != nil { + return err + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s OptMergeUniqueFieldsOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *OptMergeUniqueFieldsOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + // Encode encodes string as json. func (o OptNilString) Encode(e *jx.Encoder) { if !o.Set { @@ -7690,6 +8745,120 @@ func (s *TestFloatValidation) UnmarshalJSON(data []byte) error { return s.Decode(d) } +// Encode implements json.Marshaler. +func (s *TestInlineOneOf) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *TestInlineOneOf) encodeFields(e *jx.Encoder) { + { + if s.InlineDiscriminator.Set { + e.FieldStart("inline_discriminator") + s.InlineDiscriminator.Encode(e) + } + } + { + if s.MergeDiscriminator.Set { + e.FieldStart("merge_discriminator") + s.MergeDiscriminator.Encode(e) + } + } + { + if s.InlineUniqueFields.Set { + e.FieldStart("inline_unique_fields") + s.InlineUniqueFields.Encode(e) + } + } + { + if s.MergeUniqueFields.Set { + e.FieldStart("merge_unique_fields") + s.MergeUniqueFields.Encode(e) + } + } +} + +var jsonFieldsNameOfTestInlineOneOf = [4]string{ + 0: "inline_discriminator", + 1: "merge_discriminator", + 2: "inline_unique_fields", + 3: "merge_unique_fields", +} + +// Decode decodes TestInlineOneOf from json. +func (s *TestInlineOneOf) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode TestInlineOneOf to nil") + } + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "inline_discriminator": + if err := func() error { + s.InlineDiscriminator.Reset() + if err := s.InlineDiscriminator.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"inline_discriminator\"") + } + case "merge_discriminator": + if err := func() error { + s.MergeDiscriminator.Reset() + if err := s.MergeDiscriminator.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"merge_discriminator\"") + } + case "inline_unique_fields": + if err := func() error { + s.InlineUniqueFields.Reset() + if err := s.InlineUniqueFields.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"inline_unique_fields\"") + } + case "merge_unique_fields": + if err := func() error { + s.MergeUniqueFields.Reset() + if err := s.MergeUniqueFields.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"merge_unique_fields\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode TestInlineOneOf") + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *TestInlineOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *TestInlineOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + // Encode encodes TestNullableOneofsCreated as json. func (s TestNullableOneofsCreated) Encode(e *jx.Encoder) { unwrapped := OneOfWithNullable(s) diff --git a/internal/integration/sample_api_nc/oas_response_encoders_gen.go b/internal/integration/sample_api_nc/oas_response_encoders_gen.go index c19d906c1..a3fc3c5e8 100644 --- a/internal/integration/sample_api_nc/oas_response_encoders_gen.go +++ b/internal/integration/sample_api_nc/oas_response_encoders_gen.go @@ -529,6 +529,19 @@ func encodeTestFloatValidationResponse(response *TestFloatValidationOK, w http.R return nil } +func encodeTestInlineOneofResponse(response *TestInlineOneOf, w http.ResponseWriter, span trace.Span) error { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(200) + span.SetStatus(codes.Ok, http.StatusText(200)) + + e := jx.GetEncoder() + response.Encode(e) + if _, err := e.WriteTo(w); err != nil { + return errors.Wrap(err, "write") + } + return nil +} + func encodeTestNullableOneofsResponse(response TestNullableOneofsRes, w http.ResponseWriter, span trace.Span) error { switch response := response.(type) { case *TestNullableOneofsOK: diff --git a/internal/integration/sample_api_nc/oas_router_gen.go b/internal/integration/sample_api_nc/oas_router_gen.go index 423ae9b1c..5672a8de5 100644 --- a/internal/integration/sample_api_nc/oas_router_gen.go +++ b/internal/integration/sample_api_nc/oas_router_gen.go @@ -628,6 +628,24 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { s.notAllowed(w, r, "POST") } + return + } + case 'I': // Prefix: "InlineOneof" + if l := len("InlineOneof"); len(elem) >= l && elem[0:l] == "InlineOneof" { + elem = elem[l:] + } else { + break + } + + if len(elem) == 0 { + // Leaf node. + switch r.Method { + case "GET": + s.handleTestInlineOneofRequest([0]string{}, elemIsEscaped, w, r) + default: + s.notAllowed(w, r, "GET") + } + return } case 'N': // Prefix: "NullableOneofs" @@ -1432,6 +1450,27 @@ func (s *Server) FindPath(method string, u *url.URL) (r Route, _ bool) { return } } + case 'I': // Prefix: "InlineOneof" + if l := len("InlineOneof"); len(elem) >= l && elem[0:l] == "InlineOneof" { + elem = elem[l:] + } else { + break + } + + if len(elem) == 0 { + switch method { + case "GET": + // Leaf: TestInlineOneof + r.name = "TestInlineOneof" + r.operationID = "testInlineOneof" + r.pathPattern = "/testInlineOneof" + r.args = args + r.count = 0 + return r, true + default: + return + } + } case 'N': // Prefix: "NullableOneofs" if l := len("NullableOneofs"); len(elem) >= l && elem[0:l] == "NullableOneofs" { elem = elem[l:] diff --git a/internal/integration/sample_api_nc/oas_schemas_gen.go b/internal/integration/sample_api_nc/oas_schemas_gen.go index 3a7f2f4d7..aa148b6aa 100644 --- a/internal/integration/sample_api_nc/oas_schemas_gen.go +++ b/internal/integration/sample_api_nc/oas_schemas_gen.go @@ -663,6 +663,224 @@ func NewIntID(v int) ID { return s } +// Ref: #/components/schemas/InlineDiscriminatorOneOf +type InlineDiscriminatorOneOf struct { + Common string `json:"common"` + OneOf InlineDiscriminatorOneOfSum +} + +// GetCommon returns the value of Common. +func (s *InlineDiscriminatorOneOf) GetCommon() string { + return s.Common +} + +// GetOneOf returns the value of OneOf. +func (s *InlineDiscriminatorOneOf) GetOneOf() InlineDiscriminatorOneOfSum { + return s.OneOf +} + +// SetCommon sets the value of Common. +func (s *InlineDiscriminatorOneOf) SetCommon(val string) { + s.Common = val +} + +// SetOneOf sets the value of OneOf. +func (s *InlineDiscriminatorOneOf) SetOneOf(val InlineDiscriminatorOneOfSum) { + s.OneOf = val +} + +// InlineDiscriminatorOneOfSum represents sum type. +type InlineDiscriminatorOneOfSum struct { + Type InlineDiscriminatorOneOfSumType // switch on this field + InlineOneOfFoo InlineOneOfFoo + InlineOneOfBar InlineOneOfBar +} + +// InlineDiscriminatorOneOfSumType is oneOf type of InlineDiscriminatorOneOfSum. +type InlineDiscriminatorOneOfSumType string + +// Possible values for InlineDiscriminatorOneOfSumType. +const ( + InlineOneOfFooInlineDiscriminatorOneOfSum InlineDiscriminatorOneOfSumType = "foo" + InlineOneOfBarInlineDiscriminatorOneOfSum InlineDiscriminatorOneOfSumType = "bar" +) + +// IsInlineOneOfFoo reports whether InlineDiscriminatorOneOfSum is InlineOneOfFoo. +func (s InlineDiscriminatorOneOfSum) IsInlineOneOfFoo() bool { + return s.Type == InlineOneOfFooInlineDiscriminatorOneOfSum +} + +// IsInlineOneOfBar reports whether InlineDiscriminatorOneOfSum is InlineOneOfBar. +func (s InlineDiscriminatorOneOfSum) IsInlineOneOfBar() bool { + return s.Type == InlineOneOfBarInlineDiscriminatorOneOfSum +} + +// SetInlineOneOfFoo sets InlineDiscriminatorOneOfSum to InlineOneOfFoo. +func (s *InlineDiscriminatorOneOfSum) SetInlineOneOfFoo(v InlineOneOfFoo) { + s.Type = InlineOneOfFooInlineDiscriminatorOneOfSum + s.InlineOneOfFoo = v +} + +// GetInlineOneOfFoo returns InlineOneOfFoo and true boolean if InlineDiscriminatorOneOfSum is InlineOneOfFoo. +func (s InlineDiscriminatorOneOfSum) GetInlineOneOfFoo() (v InlineOneOfFoo, ok bool) { + if !s.IsInlineOneOfFoo() { + return v, false + } + return s.InlineOneOfFoo, true +} + +// NewInlineOneOfFooInlineDiscriminatorOneOfSum returns new InlineDiscriminatorOneOfSum from InlineOneOfFoo. +func NewInlineOneOfFooInlineDiscriminatorOneOfSum(v InlineOneOfFoo) InlineDiscriminatorOneOfSum { + var s InlineDiscriminatorOneOfSum + s.SetInlineOneOfFoo(v) + return s +} + +// SetInlineOneOfBar sets InlineDiscriminatorOneOfSum to InlineOneOfBar. +func (s *InlineDiscriminatorOneOfSum) SetInlineOneOfBar(v InlineOneOfBar) { + s.Type = InlineOneOfBarInlineDiscriminatorOneOfSum + s.InlineOneOfBar = v +} + +// GetInlineOneOfBar returns InlineOneOfBar and true boolean if InlineDiscriminatorOneOfSum is InlineOneOfBar. +func (s InlineDiscriminatorOneOfSum) GetInlineOneOfBar() (v InlineOneOfBar, ok bool) { + if !s.IsInlineOneOfBar() { + return v, false + } + return s.InlineOneOfBar, true +} + +// NewInlineOneOfBarInlineDiscriminatorOneOfSum returns new InlineDiscriminatorOneOfSum from InlineOneOfBar. +func NewInlineOneOfBarInlineDiscriminatorOneOfSum(v InlineOneOfBar) InlineDiscriminatorOneOfSum { + var s InlineDiscriminatorOneOfSum + s.SetInlineOneOfBar(v) + return s +} + +// Ref: #/components/schemas/InlineOneOfBar +type InlineOneOfBar struct { + Bar OptString `json:"bar"` +} + +// GetBar returns the value of Bar. +func (s *InlineOneOfBar) GetBar() OptString { + return s.Bar +} + +// SetBar sets the value of Bar. +func (s *InlineOneOfBar) SetBar(val OptString) { + s.Bar = val +} + +// Ref: #/components/schemas/InlineOneOfFoo +type InlineOneOfFoo struct { + Foo OptString `json:"foo"` +} + +// GetFoo returns the value of Foo. +func (s *InlineOneOfFoo) GetFoo() OptString { + return s.Foo +} + +// SetFoo sets the value of Foo. +func (s *InlineOneOfFoo) SetFoo(val OptString) { + s.Foo = val +} + +// Ref: #/components/schemas/InlineUniqueFieldsOneOf +type InlineUniqueFieldsOneOf struct { + Common string `json:"common"` + OneOf InlineUniqueFieldsOneOfSum +} + +// GetCommon returns the value of Common. +func (s *InlineUniqueFieldsOneOf) GetCommon() string { + return s.Common +} + +// GetOneOf returns the value of OneOf. +func (s *InlineUniqueFieldsOneOf) GetOneOf() InlineUniqueFieldsOneOfSum { + return s.OneOf +} + +// SetCommon sets the value of Common. +func (s *InlineUniqueFieldsOneOf) SetCommon(val string) { + s.Common = val +} + +// SetOneOf sets the value of OneOf. +func (s *InlineUniqueFieldsOneOf) SetOneOf(val InlineUniqueFieldsOneOfSum) { + s.OneOf = val +} + +// InlineUniqueFieldsOneOfSum represents sum type. +type InlineUniqueFieldsOneOfSum struct { + Type InlineUniqueFieldsOneOfSumType // switch on this field + InlineOneOfFoo InlineOneOfFoo + InlineOneOfBar InlineOneOfBar +} + +// InlineUniqueFieldsOneOfSumType is oneOf type of InlineUniqueFieldsOneOfSum. +type InlineUniqueFieldsOneOfSumType string + +// Possible values for InlineUniqueFieldsOneOfSumType. +const ( + InlineOneOfFooInlineUniqueFieldsOneOfSum InlineUniqueFieldsOneOfSumType = "InlineOneOfFoo" + InlineOneOfBarInlineUniqueFieldsOneOfSum InlineUniqueFieldsOneOfSumType = "InlineOneOfBar" +) + +// IsInlineOneOfFoo reports whether InlineUniqueFieldsOneOfSum is InlineOneOfFoo. +func (s InlineUniqueFieldsOneOfSum) IsInlineOneOfFoo() bool { + return s.Type == InlineOneOfFooInlineUniqueFieldsOneOfSum +} + +// IsInlineOneOfBar reports whether InlineUniqueFieldsOneOfSum is InlineOneOfBar. +func (s InlineUniqueFieldsOneOfSum) IsInlineOneOfBar() bool { + return s.Type == InlineOneOfBarInlineUniqueFieldsOneOfSum +} + +// SetInlineOneOfFoo sets InlineUniqueFieldsOneOfSum to InlineOneOfFoo. +func (s *InlineUniqueFieldsOneOfSum) SetInlineOneOfFoo(v InlineOneOfFoo) { + s.Type = InlineOneOfFooInlineUniqueFieldsOneOfSum + s.InlineOneOfFoo = v +} + +// GetInlineOneOfFoo returns InlineOneOfFoo and true boolean if InlineUniqueFieldsOneOfSum is InlineOneOfFoo. +func (s InlineUniqueFieldsOneOfSum) GetInlineOneOfFoo() (v InlineOneOfFoo, ok bool) { + if !s.IsInlineOneOfFoo() { + return v, false + } + return s.InlineOneOfFoo, true +} + +// NewInlineOneOfFooInlineUniqueFieldsOneOfSum returns new InlineUniqueFieldsOneOfSum from InlineOneOfFoo. +func NewInlineOneOfFooInlineUniqueFieldsOneOfSum(v InlineOneOfFoo) InlineUniqueFieldsOneOfSum { + var s InlineUniqueFieldsOneOfSum + s.SetInlineOneOfFoo(v) + return s +} + +// SetInlineOneOfBar sets InlineUniqueFieldsOneOfSum to InlineOneOfBar. +func (s *InlineUniqueFieldsOneOfSum) SetInlineOneOfBar(v InlineOneOfBar) { + s.Type = InlineOneOfBarInlineUniqueFieldsOneOfSum + s.InlineOneOfBar = v +} + +// GetInlineOneOfBar returns InlineOneOfBar and true boolean if InlineUniqueFieldsOneOfSum is InlineOneOfBar. +func (s InlineUniqueFieldsOneOfSum) GetInlineOneOfBar() (v InlineOneOfBar, ok bool) { + if !s.IsInlineOneOfBar() { + return v, false + } + return s.InlineOneOfBar, true +} + +// NewInlineOneOfBarInlineUniqueFieldsOneOfSum returns new InlineUniqueFieldsOneOfSum from InlineOneOfBar. +func NewInlineOneOfBarInlineUniqueFieldsOneOfSum(v InlineOneOfBar) InlineUniqueFieldsOneOfSum { + var s InlineUniqueFieldsOneOfSum + s.SetInlineOneOfBar(v) + return s +} + // Ref: #/components/schemas/Issue143 // Issue143 represents sum type. type Issue143 struct { @@ -1212,6 +1430,196 @@ func (s *MaxPropertiesTest) SetOptionalC(val OptInt) { s.OptionalC = val } +// Merged schema. +// Ref: #/components/schemas/MergeDiscriminatorOneOf +type MergeDiscriminatorOneOf struct { + Common string `json:"common"` + OneOf MergeDiscriminatorOneOfSum +} + +// GetCommon returns the value of Common. +func (s *MergeDiscriminatorOneOf) GetCommon() string { + return s.Common +} + +// GetOneOf returns the value of OneOf. +func (s *MergeDiscriminatorOneOf) GetOneOf() MergeDiscriminatorOneOfSum { + return s.OneOf +} + +// SetCommon sets the value of Common. +func (s *MergeDiscriminatorOneOf) SetCommon(val string) { + s.Common = val +} + +// SetOneOf sets the value of OneOf. +func (s *MergeDiscriminatorOneOf) SetOneOf(val MergeDiscriminatorOneOfSum) { + s.OneOf = val +} + +// MergeDiscriminatorOneOfSum represents sum type. +type MergeDiscriminatorOneOfSum struct { + Type MergeDiscriminatorOneOfSumType // switch on this field + InlineOneOfFoo InlineOneOfFoo + InlineOneOfBar InlineOneOfBar +} + +// MergeDiscriminatorOneOfSumType is oneOf type of MergeDiscriminatorOneOfSum. +type MergeDiscriminatorOneOfSumType string + +// Possible values for MergeDiscriminatorOneOfSumType. +const ( + InlineOneOfFooMergeDiscriminatorOneOfSum MergeDiscriminatorOneOfSumType = "foo" + InlineOneOfBarMergeDiscriminatorOneOfSum MergeDiscriminatorOneOfSumType = "bar" +) + +// IsInlineOneOfFoo reports whether MergeDiscriminatorOneOfSum is InlineOneOfFoo. +func (s MergeDiscriminatorOneOfSum) IsInlineOneOfFoo() bool { + return s.Type == InlineOneOfFooMergeDiscriminatorOneOfSum +} + +// IsInlineOneOfBar reports whether MergeDiscriminatorOneOfSum is InlineOneOfBar. +func (s MergeDiscriminatorOneOfSum) IsInlineOneOfBar() bool { + return s.Type == InlineOneOfBarMergeDiscriminatorOneOfSum +} + +// SetInlineOneOfFoo sets MergeDiscriminatorOneOfSum to InlineOneOfFoo. +func (s *MergeDiscriminatorOneOfSum) SetInlineOneOfFoo(v InlineOneOfFoo) { + s.Type = InlineOneOfFooMergeDiscriminatorOneOfSum + s.InlineOneOfFoo = v +} + +// GetInlineOneOfFoo returns InlineOneOfFoo and true boolean if MergeDiscriminatorOneOfSum is InlineOneOfFoo. +func (s MergeDiscriminatorOneOfSum) GetInlineOneOfFoo() (v InlineOneOfFoo, ok bool) { + if !s.IsInlineOneOfFoo() { + return v, false + } + return s.InlineOneOfFoo, true +} + +// NewInlineOneOfFooMergeDiscriminatorOneOfSum returns new MergeDiscriminatorOneOfSum from InlineOneOfFoo. +func NewInlineOneOfFooMergeDiscriminatorOneOfSum(v InlineOneOfFoo) MergeDiscriminatorOneOfSum { + var s MergeDiscriminatorOneOfSum + s.SetInlineOneOfFoo(v) + return s +} + +// SetInlineOneOfBar sets MergeDiscriminatorOneOfSum to InlineOneOfBar. +func (s *MergeDiscriminatorOneOfSum) SetInlineOneOfBar(v InlineOneOfBar) { + s.Type = InlineOneOfBarMergeDiscriminatorOneOfSum + s.InlineOneOfBar = v +} + +// GetInlineOneOfBar returns InlineOneOfBar and true boolean if MergeDiscriminatorOneOfSum is InlineOneOfBar. +func (s MergeDiscriminatorOneOfSum) GetInlineOneOfBar() (v InlineOneOfBar, ok bool) { + if !s.IsInlineOneOfBar() { + return v, false + } + return s.InlineOneOfBar, true +} + +// NewInlineOneOfBarMergeDiscriminatorOneOfSum returns new MergeDiscriminatorOneOfSum from InlineOneOfBar. +func NewInlineOneOfBarMergeDiscriminatorOneOfSum(v InlineOneOfBar) MergeDiscriminatorOneOfSum { + var s MergeDiscriminatorOneOfSum + s.SetInlineOneOfBar(v) + return s +} + +// Merged schema. +// Ref: #/components/schemas/MergeUniqueFieldsOneOf +type MergeUniqueFieldsOneOf struct { + Common string `json:"common"` + OneOf MergeUniqueFieldsOneOfSum +} + +// GetCommon returns the value of Common. +func (s *MergeUniqueFieldsOneOf) GetCommon() string { + return s.Common +} + +// GetOneOf returns the value of OneOf. +func (s *MergeUniqueFieldsOneOf) GetOneOf() MergeUniqueFieldsOneOfSum { + return s.OneOf +} + +// SetCommon sets the value of Common. +func (s *MergeUniqueFieldsOneOf) SetCommon(val string) { + s.Common = val +} + +// SetOneOf sets the value of OneOf. +func (s *MergeUniqueFieldsOneOf) SetOneOf(val MergeUniqueFieldsOneOfSum) { + s.OneOf = val +} + +// MergeUniqueFieldsOneOfSum represents sum type. +type MergeUniqueFieldsOneOfSum struct { + Type MergeUniqueFieldsOneOfSumType // switch on this field + InlineOneOfFoo InlineOneOfFoo + InlineOneOfBar InlineOneOfBar +} + +// MergeUniqueFieldsOneOfSumType is oneOf type of MergeUniqueFieldsOneOfSum. +type MergeUniqueFieldsOneOfSumType string + +// Possible values for MergeUniqueFieldsOneOfSumType. +const ( + InlineOneOfFooMergeUniqueFieldsOneOfSum MergeUniqueFieldsOneOfSumType = "InlineOneOfFoo" + InlineOneOfBarMergeUniqueFieldsOneOfSum MergeUniqueFieldsOneOfSumType = "InlineOneOfBar" +) + +// IsInlineOneOfFoo reports whether MergeUniqueFieldsOneOfSum is InlineOneOfFoo. +func (s MergeUniqueFieldsOneOfSum) IsInlineOneOfFoo() bool { + return s.Type == InlineOneOfFooMergeUniqueFieldsOneOfSum +} + +// IsInlineOneOfBar reports whether MergeUniqueFieldsOneOfSum is InlineOneOfBar. +func (s MergeUniqueFieldsOneOfSum) IsInlineOneOfBar() bool { + return s.Type == InlineOneOfBarMergeUniqueFieldsOneOfSum +} + +// SetInlineOneOfFoo sets MergeUniqueFieldsOneOfSum to InlineOneOfFoo. +func (s *MergeUniqueFieldsOneOfSum) SetInlineOneOfFoo(v InlineOneOfFoo) { + s.Type = InlineOneOfFooMergeUniqueFieldsOneOfSum + s.InlineOneOfFoo = v +} + +// GetInlineOneOfFoo returns InlineOneOfFoo and true boolean if MergeUniqueFieldsOneOfSum is InlineOneOfFoo. +func (s MergeUniqueFieldsOneOfSum) GetInlineOneOfFoo() (v InlineOneOfFoo, ok bool) { + if !s.IsInlineOneOfFoo() { + return v, false + } + return s.InlineOneOfFoo, true +} + +// NewInlineOneOfFooMergeUniqueFieldsOneOfSum returns new MergeUniqueFieldsOneOfSum from InlineOneOfFoo. +func NewInlineOneOfFooMergeUniqueFieldsOneOfSum(v InlineOneOfFoo) MergeUniqueFieldsOneOfSum { + var s MergeUniqueFieldsOneOfSum + s.SetInlineOneOfFoo(v) + return s +} + +// SetInlineOneOfBar sets MergeUniqueFieldsOneOfSum to InlineOneOfBar. +func (s *MergeUniqueFieldsOneOfSum) SetInlineOneOfBar(v InlineOneOfBar) { + s.Type = InlineOneOfBarMergeUniqueFieldsOneOfSum + s.InlineOneOfBar = v +} + +// GetInlineOneOfBar returns InlineOneOfBar and true boolean if MergeUniqueFieldsOneOfSum is InlineOneOfBar. +func (s MergeUniqueFieldsOneOfSum) GetInlineOneOfBar() (v InlineOneOfBar, ok bool) { + if !s.IsInlineOneOfBar() { + return v, false + } + return s.InlineOneOfBar, true +} + +// NewInlineOneOfBarMergeUniqueFieldsOneOfSum returns new MergeUniqueFieldsOneOfSum from InlineOneOfBar. +func NewInlineOneOfBarMergeUniqueFieldsOneOfSum(v InlineOneOfBar) MergeUniqueFieldsOneOfSum { + var s MergeUniqueFieldsOneOfSum + s.SetInlineOneOfBar(v) + return s +} + // NewNilInt returns new NilInt with value set to v. func NewNilInt(v int) NilInt { return NilInt{ @@ -2995,6 +3403,98 @@ func (o OptIPv6) Or(d netip.Addr) netip.Addr { return d } +// NewOptInlineDiscriminatorOneOf returns new OptInlineDiscriminatorOneOf with value set to v. +func NewOptInlineDiscriminatorOneOf(v InlineDiscriminatorOneOf) OptInlineDiscriminatorOneOf { + return OptInlineDiscriminatorOneOf{ + Value: v, + Set: true, + } +} + +// OptInlineDiscriminatorOneOf is optional InlineDiscriminatorOneOf. +type OptInlineDiscriminatorOneOf struct { + Value InlineDiscriminatorOneOf + Set bool +} + +// IsSet returns true if OptInlineDiscriminatorOneOf was set. +func (o OptInlineDiscriminatorOneOf) IsSet() bool { return o.Set } + +// Reset unsets value. +func (o *OptInlineDiscriminatorOneOf) Reset() { + var v InlineDiscriminatorOneOf + o.Value = v + o.Set = false +} + +// SetTo sets value to v. +func (o *OptInlineDiscriminatorOneOf) SetTo(v InlineDiscriminatorOneOf) { + o.Set = true + o.Value = v +} + +// Get returns value and boolean that denotes whether value was set. +func (o OptInlineDiscriminatorOneOf) Get() (v InlineDiscriminatorOneOf, ok bool) { + if !o.Set { + return v, false + } + return o.Value, true +} + +// Or returns value if set, or given parameter if does not. +func (o OptInlineDiscriminatorOneOf) Or(d InlineDiscriminatorOneOf) InlineDiscriminatorOneOf { + if v, ok := o.Get(); ok { + return v + } + return d +} + +// NewOptInlineUniqueFieldsOneOf returns new OptInlineUniqueFieldsOneOf with value set to v. +func NewOptInlineUniqueFieldsOneOf(v InlineUniqueFieldsOneOf) OptInlineUniqueFieldsOneOf { + return OptInlineUniqueFieldsOneOf{ + Value: v, + Set: true, + } +} + +// OptInlineUniqueFieldsOneOf is optional InlineUniqueFieldsOneOf. +type OptInlineUniqueFieldsOneOf struct { + Value InlineUniqueFieldsOneOf + Set bool +} + +// IsSet returns true if OptInlineUniqueFieldsOneOf was set. +func (o OptInlineUniqueFieldsOneOf) IsSet() bool { return o.Set } + +// Reset unsets value. +func (o *OptInlineUniqueFieldsOneOf) Reset() { + var v InlineUniqueFieldsOneOf + o.Value = v + o.Set = false +} + +// SetTo sets value to v. +func (o *OptInlineUniqueFieldsOneOf) SetTo(v InlineUniqueFieldsOneOf) { + o.Set = true + o.Value = v +} + +// Get returns value and boolean that denotes whether value was set. +func (o OptInlineUniqueFieldsOneOf) Get() (v InlineUniqueFieldsOneOf, ok bool) { + if !o.Set { + return v, false + } + return o.Value, true +} + +// Or returns value if set, or given parameter if does not. +func (o OptInlineUniqueFieldsOneOf) Or(d InlineUniqueFieldsOneOf) InlineUniqueFieldsOneOf { + if v, ok := o.Get(); ok { + return v + } + return d +} + // NewOptInt returns new OptInt with value set to v. func NewOptInt(v int) OptInt { return OptInt{ @@ -3271,6 +3771,98 @@ func (o OptMaxPropertiesTest) Or(d MaxPropertiesTest) MaxPropertiesTest { return d } +// NewOptMergeDiscriminatorOneOf returns new OptMergeDiscriminatorOneOf with value set to v. +func NewOptMergeDiscriminatorOneOf(v MergeDiscriminatorOneOf) OptMergeDiscriminatorOneOf { + return OptMergeDiscriminatorOneOf{ + Value: v, + Set: true, + } +} + +// OptMergeDiscriminatorOneOf is optional MergeDiscriminatorOneOf. +type OptMergeDiscriminatorOneOf struct { + Value MergeDiscriminatorOneOf + Set bool +} + +// IsSet returns true if OptMergeDiscriminatorOneOf was set. +func (o OptMergeDiscriminatorOneOf) IsSet() bool { return o.Set } + +// Reset unsets value. +func (o *OptMergeDiscriminatorOneOf) Reset() { + var v MergeDiscriminatorOneOf + o.Value = v + o.Set = false +} + +// SetTo sets value to v. +func (o *OptMergeDiscriminatorOneOf) SetTo(v MergeDiscriminatorOneOf) { + o.Set = true + o.Value = v +} + +// Get returns value and boolean that denotes whether value was set. +func (o OptMergeDiscriminatorOneOf) Get() (v MergeDiscriminatorOneOf, ok bool) { + if !o.Set { + return v, false + } + return o.Value, true +} + +// Or returns value if set, or given parameter if does not. +func (o OptMergeDiscriminatorOneOf) Or(d MergeDiscriminatorOneOf) MergeDiscriminatorOneOf { + if v, ok := o.Get(); ok { + return v + } + return d +} + +// NewOptMergeUniqueFieldsOneOf returns new OptMergeUniqueFieldsOneOf with value set to v. +func NewOptMergeUniqueFieldsOneOf(v MergeUniqueFieldsOneOf) OptMergeUniqueFieldsOneOf { + return OptMergeUniqueFieldsOneOf{ + Value: v, + Set: true, + } +} + +// OptMergeUniqueFieldsOneOf is optional MergeUniqueFieldsOneOf. +type OptMergeUniqueFieldsOneOf struct { + Value MergeUniqueFieldsOneOf + Set bool +} + +// IsSet returns true if OptMergeUniqueFieldsOneOf was set. +func (o OptMergeUniqueFieldsOneOf) IsSet() bool { return o.Set } + +// Reset unsets value. +func (o *OptMergeUniqueFieldsOneOf) Reset() { + var v MergeUniqueFieldsOneOf + o.Value = v + o.Set = false +} + +// SetTo sets value to v. +func (o *OptMergeUniqueFieldsOneOf) SetTo(v MergeUniqueFieldsOneOf) { + o.Set = true + o.Value = v +} + +// Get returns value and boolean that denotes whether value was set. +func (o OptMergeUniqueFieldsOneOf) Get() (v MergeUniqueFieldsOneOf, ok bool) { + if !o.Set { + return v, false + } + return o.Value, true +} + +// Or returns value if set, or given parameter if does not. +func (o OptMergeUniqueFieldsOneOf) Or(d MergeUniqueFieldsOneOf) MergeUniqueFieldsOneOf { + if v, ok := o.Get(); ok { + return v + } + return d +} + // NewOptNilString returns new OptNilString with value set to v. func NewOptNilString(v string) OptNilString { return OptNilString{ @@ -4827,6 +5419,54 @@ func (s *TestFloatValidation) SetMultipleOf(val float64) { // TestFloatValidationOK is response for TestFloatValidation operation. type TestFloatValidationOK struct{} +// Ref: #/components/schemas/TestInlineOneOf +type TestInlineOneOf struct { + InlineDiscriminator OptInlineDiscriminatorOneOf `json:"inline_discriminator"` + MergeDiscriminator OptMergeDiscriminatorOneOf `json:"merge_discriminator"` + InlineUniqueFields OptInlineUniqueFieldsOneOf `json:"inline_unique_fields"` + MergeUniqueFields OptMergeUniqueFieldsOneOf `json:"merge_unique_fields"` +} + +// GetInlineDiscriminator returns the value of InlineDiscriminator. +func (s *TestInlineOneOf) GetInlineDiscriminator() OptInlineDiscriminatorOneOf { + return s.InlineDiscriminator +} + +// GetMergeDiscriminator returns the value of MergeDiscriminator. +func (s *TestInlineOneOf) GetMergeDiscriminator() OptMergeDiscriminatorOneOf { + return s.MergeDiscriminator +} + +// GetInlineUniqueFields returns the value of InlineUniqueFields. +func (s *TestInlineOneOf) GetInlineUniqueFields() OptInlineUniqueFieldsOneOf { + return s.InlineUniqueFields +} + +// GetMergeUniqueFields returns the value of MergeUniqueFields. +func (s *TestInlineOneOf) GetMergeUniqueFields() OptMergeUniqueFieldsOneOf { + return s.MergeUniqueFields +} + +// SetInlineDiscriminator sets the value of InlineDiscriminator. +func (s *TestInlineOneOf) SetInlineDiscriminator(val OptInlineDiscriminatorOneOf) { + s.InlineDiscriminator = val +} + +// SetMergeDiscriminator sets the value of MergeDiscriminator. +func (s *TestInlineOneOf) SetMergeDiscriminator(val OptMergeDiscriminatorOneOf) { + s.MergeDiscriminator = val +} + +// SetInlineUniqueFields sets the value of InlineUniqueFields. +func (s *TestInlineOneOf) SetInlineUniqueFields(val OptInlineUniqueFieldsOneOf) { + s.InlineUniqueFields = val +} + +// SetMergeUniqueFields sets the value of MergeUniqueFields. +func (s *TestInlineOneOf) SetMergeUniqueFields(val OptMergeUniqueFieldsOneOf) { + s.MergeUniqueFields = val +} + type TestNullableOneofsCreated OneOfWithNullable func (*TestNullableOneofsCreated) testNullableOneofsRes() {} diff --git a/internal/integration/sample_api_nc/oas_server_gen.go b/internal/integration/sample_api_nc/oas_server_gen.go index 7118f5121..0d0f1661f 100644 --- a/internal/integration/sample_api_nc/oas_server_gen.go +++ b/internal/integration/sample_api_nc/oas_server_gen.go @@ -132,6 +132,10 @@ type Handler interface { // // POST /testFloatValidation TestFloatValidation(ctx context.Context, req *TestFloatValidation) error + // TestInlineOneof implements testInlineOneof operation. + // + // GET /testInlineOneof + TestInlineOneof(ctx context.Context) (*TestInlineOneOf, error) // TestNullableOneofs implements testNullableOneofs operation. // // GET /testNullableOneofs diff --git a/internal/integration/sample_api_nc/oas_unimplemented_gen.go b/internal/integration/sample_api_nc/oas_unimplemented_gen.go index 8d098c830..0e76e9b88 100644 --- a/internal/integration/sample_api_nc/oas_unimplemented_gen.go +++ b/internal/integration/sample_api_nc/oas_unimplemented_gen.go @@ -212,6 +212,13 @@ func (UnimplementedHandler) TestFloatValidation(ctx context.Context, req *TestFl return ht.ErrNotImplemented } +// TestInlineOneof implements testInlineOneof operation. +// +// GET /testInlineOneof +func (UnimplementedHandler) TestInlineOneof(ctx context.Context) (r *TestInlineOneOf, _ error) { + return r, ht.ErrNotImplemented +} + // TestNullableOneofs implements testNullableOneofs operation. // // GET /testNullableOneofs diff --git a/internal/integration/sample_api_ns/oas_client_gen.go b/internal/integration/sample_api_ns/oas_client_gen.go index b171c4324..56ce53b16 100644 --- a/internal/integration/sample_api_ns/oas_client_gen.go +++ b/internal/integration/sample_api_ns/oas_client_gen.go @@ -2283,6 +2283,75 @@ func (c *Client) sendTestFloatValidation(ctx context.Context, request *TestFloat return result, nil } +// TestInlineOneof invokes testInlineOneof operation. +// +// GET /testInlineOneof +func (c *Client) TestInlineOneof(ctx context.Context) (*TestInlineOneOf, error) { + res, err := c.sendTestInlineOneof(ctx) + _ = res + return res, err +} + +func (c *Client) sendTestInlineOneof(ctx context.Context) (res *TestInlineOneOf, err error) { + otelAttrs := []attribute.KeyValue{ + otelogen.OperationID("testInlineOneof"), + } + + // Run stopwatch. + startTime := time.Now() + defer func() { + // Use floating point division here for higher precision (instead of Millisecond method). + elapsedDuration := time.Since(startTime) + c.duration.Record(ctx, float64(float64(elapsedDuration)/float64(time.Millisecond)), metric.WithAttributes(otelAttrs...)) + }() + + // Increment request counter. + c.requests.Add(ctx, 1, metric.WithAttributes(otelAttrs...)) + + // Start a span for this request. + ctx, span := c.cfg.Tracer.Start(ctx, "TestInlineOneof", + trace.WithAttributes(otelAttrs...), + clientSpanKind, + ) + // Track stage for error reporting. + var stage string + defer func() { + if err != nil { + span.RecordError(err) + span.SetStatus(codes.Error, stage) + c.errors.Add(ctx, 1, metric.WithAttributes(otelAttrs...)) + } + span.End() + }() + + stage = "BuildURL" + u := uri.Clone(c.requestURL(ctx)) + var pathParts [1]string + pathParts[0] = "/testInlineOneof" + uri.AddPathParts(u, pathParts[:]...) + + stage = "EncodeRequest" + r, err := ht.NewRequest(ctx, "GET", u) + if err != nil { + return res, errors.Wrap(err, "create request") + } + + stage = "SendRequest" + resp, err := c.cfg.Client.Do(r) + if err != nil { + return res, errors.Wrap(err, "do request") + } + defer resp.Body.Close() + + stage = "DecodeResponse" + result, err := decodeTestInlineOneofResponse(resp) + if err != nil { + return res, errors.Wrap(err, "decode response") + } + + return result, nil +} + // TestNullableOneofs invokes testNullableOneofs operation. // // GET /testNullableOneofs diff --git a/internal/integration/sample_api_ns/oas_json_gen.go b/internal/integration/sample_api_ns/oas_json_gen.go index 3cac3c721..e2fb67cfd 100644 --- a/internal/integration/sample_api_ns/oas_json_gen.go +++ b/internal/integration/sample_api_ns/oas_json_gen.go @@ -583,6 +583,15 @@ func (s DataDescription) Encode(e *jx.Encoder) { } } +func (s DataDescription) encodeFields(e *jx.Encoder) { + switch s.Type { + case DescriptionDetailedDataDescription: + s.DescriptionDetailed.encodeFields(e) + case DescriptionSimpleDataDescription: + s.DescriptionSimple.encodeFields(e) + } +} + // Decode decodes DataDescription from json. func (s *DataDescription) Decode(d *jx.Decoder) error { if s == nil { @@ -1403,29 +1412,539 @@ func (s *ID) Decode(d *jx.Decoder) error { if err != nil { return err } - s.Type = IntID - case jx.String: - v, err := d.Str() - s.String = string(v) - if err != nil { + s.Type = IntID + case jx.String: + v, err := d.Str() + s.String = string(v) + if err != nil { + return err + } + s.Type = StringID + default: + return errors.Errorf("unexpected json type %q", t) + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s ID) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *ID) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *InlineDiscriminatorOneOf) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *InlineDiscriminatorOneOf) encodeFields(e *jx.Encoder) { + { + e.FieldStart("common") + e.Str(s.Common) + } + s.OneOf.encodeFields(e) +} + +var jsonFieldsNameOfInlineDiscriminatorOneOf = [1]string{ + 0: "common", +} + +// Decode decodes InlineDiscriminatorOneOf from json. +func (s *InlineDiscriminatorOneOf) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode InlineDiscriminatorOneOf to nil") + } + if err := d.Capture(func(d *jx.Decoder) error { + return s.OneOf.Decode(d) + }); err != nil { + return errors.Wrap(err, "decode field OneOf") + } + var requiredBitSet [1]uint8 + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "common": + requiredBitSet[0] |= 1 << 0 + if err := func() error { + v, err := d.Str() + s.Common = string(v) + if err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"common\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode InlineDiscriminatorOneOf") + } + // Validate required fields. + var failures []validate.FieldError + for i, mask := range [1]uint8{ + 0b00000001, + } { + if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { + // Mask only required fields and check equality to mask using XOR. + // + // If XOR result is not zero, result is not equal to expected, so some fields are missed. + // Bits of fields which would be set are actually bits of missed fields. + missed := bits.OnesCount8(result) + for bitN := 0; bitN < missed; bitN++ { + bitIdx := bits.TrailingZeros8(result) + fieldIdx := i*8 + bitIdx + var name string + if fieldIdx < len(jsonFieldsNameOfInlineDiscriminatorOneOf) { + name = jsonFieldsNameOfInlineDiscriminatorOneOf[fieldIdx] + } else { + name = strconv.Itoa(fieldIdx) + } + failures = append(failures, validate.FieldError{ + Name: name, + Error: validate.ErrFieldRequired, + }) + // Reset bit. + result &^= 1 << bitIdx + } + } + } + if len(failures) > 0 { + return &validate.Error{Fields: failures} + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *InlineDiscriminatorOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *InlineDiscriminatorOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode encodes InlineDiscriminatorOneOfSum as json. +func (s InlineDiscriminatorOneOfSum) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s InlineDiscriminatorOneOfSum) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineOneOfBarInlineDiscriminatorOneOfSum: + e.FieldStart("kind") + e.Str("bar") + s.InlineOneOfBar.encodeFields(e) + case InlineOneOfFooInlineDiscriminatorOneOfSum: + e.FieldStart("kind") + e.Str("foo") + s.InlineOneOfFoo.encodeFields(e) + } +} + +// Decode decodes InlineDiscriminatorOneOfSum from json. +func (s *InlineDiscriminatorOneOfSum) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode InlineDiscriminatorOneOfSum to nil") + } + // Sum type discriminator. + if typ := d.Next(); typ != jx.Object { + return errors.Errorf("unexpected json type %q", typ) + } + + var found bool + if err := d.Capture(func(d *jx.Decoder) error { + return d.ObjBytes(func(d *jx.Decoder, key []byte) error { + if found { + return d.Skip() + } + switch string(key) { + case "kind": + typ, err := d.Str() + if err != nil { + return err + } + switch typ { + case "bar": + s.Type = InlineOneOfBarInlineDiscriminatorOneOfSum + found = true + case "foo": + s.Type = InlineOneOfFooInlineDiscriminatorOneOfSum + found = true + default: + return errors.Errorf("unknown type %s", typ) + } + return nil + } + return d.Skip() + }) + }); err != nil { + return errors.Wrap(err, "capture") + } + if !found { + return errors.New("unable to detect sum type variant") + } + switch s.Type { + case InlineOneOfFooInlineDiscriminatorOneOfSum: + if err := s.InlineOneOfFoo.Decode(d); err != nil { + return err + } + case InlineOneOfBarInlineDiscriminatorOneOfSum: + if err := s.InlineOneOfBar.Decode(d); err != nil { + return err + } + default: + return errors.Errorf("inferred invalid type: %s", s.Type) + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s InlineDiscriminatorOneOfSum) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *InlineDiscriminatorOneOfSum) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *InlineOneOfBar) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *InlineOneOfBar) encodeFields(e *jx.Encoder) { + { + if s.Bar.Set { + e.FieldStart("bar") + s.Bar.Encode(e) + } + } +} + +var jsonFieldsNameOfInlineOneOfBar = [1]string{ + 0: "bar", +} + +// Decode decodes InlineOneOfBar from json. +func (s *InlineOneOfBar) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode InlineOneOfBar to nil") + } + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "bar": + if err := func() error { + s.Bar.Reset() + if err := s.Bar.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"bar\"") + } + case "kind": + return d.Skip() + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode InlineOneOfBar") + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *InlineOneOfBar) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *InlineOneOfBar) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *InlineOneOfFoo) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *InlineOneOfFoo) encodeFields(e *jx.Encoder) { + { + if s.Foo.Set { + e.FieldStart("foo") + s.Foo.Encode(e) + } + } +} + +var jsonFieldsNameOfInlineOneOfFoo = [1]string{ + 0: "foo", +} + +// Decode decodes InlineOneOfFoo from json. +func (s *InlineOneOfFoo) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode InlineOneOfFoo to nil") + } + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "foo": + if err := func() error { + s.Foo.Reset() + if err := s.Foo.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"foo\"") + } + case "kind": + return d.Skip() + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode InlineOneOfFoo") + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *InlineOneOfFoo) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *InlineOneOfFoo) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *InlineUniqueFieldsOneOf) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *InlineUniqueFieldsOneOf) encodeFields(e *jx.Encoder) { + { + e.FieldStart("common") + e.Str(s.Common) + } + s.OneOf.encodeFields(e) +} + +var jsonFieldsNameOfInlineUniqueFieldsOneOf = [1]string{ + 0: "common", +} + +// Decode decodes InlineUniqueFieldsOneOf from json. +func (s *InlineUniqueFieldsOneOf) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode InlineUniqueFieldsOneOf to nil") + } + if err := d.Capture(func(d *jx.Decoder) error { + return s.OneOf.Decode(d) + }); err != nil { + return errors.Wrap(err, "decode field OneOf") + } + var requiredBitSet [1]uint8 + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "common": + requiredBitSet[0] |= 1 << 0 + if err := func() error { + v, err := d.Str() + s.Common = string(v) + if err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"common\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode InlineUniqueFieldsOneOf") + } + // Validate required fields. + var failures []validate.FieldError + for i, mask := range [1]uint8{ + 0b00000001, + } { + if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { + // Mask only required fields and check equality to mask using XOR. + // + // If XOR result is not zero, result is not equal to expected, so some fields are missed. + // Bits of fields which would be set are actually bits of missed fields. + missed := bits.OnesCount8(result) + for bitN := 0; bitN < missed; bitN++ { + bitIdx := bits.TrailingZeros8(result) + fieldIdx := i*8 + bitIdx + var name string + if fieldIdx < len(jsonFieldsNameOfInlineUniqueFieldsOneOf) { + name = jsonFieldsNameOfInlineUniqueFieldsOneOf[fieldIdx] + } else { + name = strconv.Itoa(fieldIdx) + } + failures = append(failures, validate.FieldError{ + Name: name, + Error: validate.ErrFieldRequired, + }) + // Reset bit. + result &^= 1 << bitIdx + } + } + } + if len(failures) > 0 { + return &validate.Error{Fields: failures} + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *InlineUniqueFieldsOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *InlineUniqueFieldsOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode encodes InlineUniqueFieldsOneOfSum as json. +func (s InlineUniqueFieldsOneOfSum) Encode(e *jx.Encoder) { + switch s.Type { + case InlineOneOfFooInlineUniqueFieldsOneOfSum: + s.InlineOneOfFoo.Encode(e) + case InlineOneOfBarInlineUniqueFieldsOneOfSum: + s.InlineOneOfBar.Encode(e) + } +} + +func (s InlineUniqueFieldsOneOfSum) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineOneOfFooInlineUniqueFieldsOneOfSum: + s.InlineOneOfFoo.encodeFields(e) + case InlineOneOfBarInlineUniqueFieldsOneOfSum: + s.InlineOneOfBar.encodeFields(e) + } +} + +// Decode decodes InlineUniqueFieldsOneOfSum from json. +func (s *InlineUniqueFieldsOneOfSum) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode InlineUniqueFieldsOneOfSum to nil") + } + // Sum type fields. + if typ := d.Next(); typ != jx.Object { + return errors.Errorf("unexpected json type %q", typ) + } + + var found bool + if err := d.Capture(func(d *jx.Decoder) error { + return d.ObjBytes(func(d *jx.Decoder, key []byte) error { + switch string(key) { + case "foo": + match := InlineOneOfFooInlineUniqueFieldsOneOfSum + if found && s.Type != match { + s.Type = "" + return errors.Errorf("multiple oneOf matches: (%v, %v)", s.Type, match) + } + found = true + s.Type = match + case "bar": + match := InlineOneOfBarInlineUniqueFieldsOneOfSum + if found && s.Type != match { + s.Type = "" + return errors.Errorf("multiple oneOf matches: (%v, %v)", s.Type, match) + } + found = true + s.Type = match + } + return d.Skip() + }) + }); err != nil { + return errors.Wrap(err, "capture") + } + if !found { + return errors.New("unable to detect sum type variant") + } + switch s.Type { + case InlineOneOfFooInlineUniqueFieldsOneOfSum: + if err := s.InlineOneOfFoo.Decode(d); err != nil { + return err + } + case InlineOneOfBarInlineUniqueFieldsOneOfSum: + if err := s.InlineOneOfBar.Decode(d); err != nil { return err } - s.Type = StringID default: - return errors.Errorf("unexpected json type %q", t) + return errors.Errorf("inferred invalid type: %s", s.Type) } return nil } // MarshalJSON implements stdjson.Marshaler. -func (s ID) MarshalJSON() ([]byte, error) { +func (s InlineUniqueFieldsOneOfSum) MarshalJSON() ([]byte, error) { e := jx.Encoder{} s.Encode(&e) return e.Bytes(), nil } // UnmarshalJSON implements stdjson.Unmarshaler. -func (s *ID) UnmarshalJSON(data []byte) error { +func (s *InlineUniqueFieldsOneOfSum) UnmarshalJSON(data []byte) error { d := jx.DecodeBytes(data) return s.Decode(d) } @@ -1444,6 +1963,19 @@ func (s Issue143) Encode(e *jx.Encoder) { } } +func (s Issue143) encodeFields(e *jx.Encoder) { + switch s.Type { + case Issue1430Issue143: + s.Issue1430.encodeFields(e) + case Issue1431Issue143: + s.Issue1431.encodeFields(e) + case Issue1432Issue143: + s.Issue1432.encodeFields(e) + case Issue1433Issue143: + s.Issue1433.encodeFields(e) + } +} + // Decode decodes Issue143 from json. func (s *Issue143) Decode(d *jx.Decoder) error { if s == nil { @@ -2057,25 +2589,25 @@ func (s *Issue1433) UnmarshalJSON(data []byte) error { // Encode encodes Issue943 as json. func (s Issue943) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s Issue943) encodeFields(e *jx.Encoder) { switch s.Type { case Issue943Variant1Issue943: - e.ObjStart() e.FieldStart("selector") e.Str("variant1") s.Issue943Variant1.encodeFields(e) - e.ObjEnd() case Issue943Variant2Issue943: - e.ObjStart() e.FieldStart("selector") e.Str("variant2") s.Issue943Variant2.encodeFields(e) - e.ObjEnd() case Issue943MapIssue943: - e.ObjStart() e.FieldStart("selector") e.Str("variant3") s.Issue943Map.encodeFields(e) - e.ObjEnd() } } @@ -2917,8 +3449,302 @@ func (s *MaxPropertiesTest) Decode(d *jx.Decoder) error { bitIdx := bits.TrailingZeros8(result) fieldIdx := i*8 + bitIdx var name string - if fieldIdx < len(jsonFieldsNameOfMaxPropertiesTest) { - name = jsonFieldsNameOfMaxPropertiesTest[fieldIdx] + if fieldIdx < len(jsonFieldsNameOfMaxPropertiesTest) { + name = jsonFieldsNameOfMaxPropertiesTest[fieldIdx] + } else { + name = strconv.Itoa(fieldIdx) + } + failures = append(failures, validate.FieldError{ + Name: name, + Error: validate.ErrFieldRequired, + }) + // Reset bit. + result &^= 1 << bitIdx + } + } + } + if len(failures) > 0 { + return &validate.Error{Fields: failures} + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *MaxPropertiesTest) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *MaxPropertiesTest) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *MergeDiscriminatorOneOf) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *MergeDiscriminatorOneOf) encodeFields(e *jx.Encoder) { + { + e.FieldStart("common") + e.Str(s.Common) + } + s.OneOf.encodeFields(e) +} + +var jsonFieldsNameOfMergeDiscriminatorOneOf = [1]string{ + 0: "common", +} + +// Decode decodes MergeDiscriminatorOneOf from json. +func (s *MergeDiscriminatorOneOf) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode MergeDiscriminatorOneOf to nil") + } + if err := d.Capture(func(d *jx.Decoder) error { + return s.OneOf.Decode(d) + }); err != nil { + return errors.Wrap(err, "decode field OneOf") + } + var requiredBitSet [1]uint8 + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "common": + requiredBitSet[0] |= 1 << 0 + if err := func() error { + v, err := d.Str() + s.Common = string(v) + if err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"common\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode MergeDiscriminatorOneOf") + } + // Validate required fields. + var failures []validate.FieldError + for i, mask := range [1]uint8{ + 0b00000001, + } { + if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { + // Mask only required fields and check equality to mask using XOR. + // + // If XOR result is not zero, result is not equal to expected, so some fields are missed. + // Bits of fields which would be set are actually bits of missed fields. + missed := bits.OnesCount8(result) + for bitN := 0; bitN < missed; bitN++ { + bitIdx := bits.TrailingZeros8(result) + fieldIdx := i*8 + bitIdx + var name string + if fieldIdx < len(jsonFieldsNameOfMergeDiscriminatorOneOf) { + name = jsonFieldsNameOfMergeDiscriminatorOneOf[fieldIdx] + } else { + name = strconv.Itoa(fieldIdx) + } + failures = append(failures, validate.FieldError{ + Name: name, + Error: validate.ErrFieldRequired, + }) + // Reset bit. + result &^= 1 << bitIdx + } + } + } + if len(failures) > 0 { + return &validate.Error{Fields: failures} + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *MergeDiscriminatorOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *MergeDiscriminatorOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode encodes MergeDiscriminatorOneOfSum as json. +func (s MergeDiscriminatorOneOfSum) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s MergeDiscriminatorOneOfSum) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineOneOfBarMergeDiscriminatorOneOfSum: + e.FieldStart("kind") + e.Str("bar") + s.InlineOneOfBar.encodeFields(e) + case InlineOneOfFooMergeDiscriminatorOneOfSum: + e.FieldStart("kind") + e.Str("foo") + s.InlineOneOfFoo.encodeFields(e) + } +} + +// Decode decodes MergeDiscriminatorOneOfSum from json. +func (s *MergeDiscriminatorOneOfSum) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode MergeDiscriminatorOneOfSum to nil") + } + // Sum type discriminator. + if typ := d.Next(); typ != jx.Object { + return errors.Errorf("unexpected json type %q", typ) + } + + var found bool + if err := d.Capture(func(d *jx.Decoder) error { + return d.ObjBytes(func(d *jx.Decoder, key []byte) error { + if found { + return d.Skip() + } + switch string(key) { + case "kind": + typ, err := d.Str() + if err != nil { + return err + } + switch typ { + case "bar": + s.Type = InlineOneOfBarMergeDiscriminatorOneOfSum + found = true + case "foo": + s.Type = InlineOneOfFooMergeDiscriminatorOneOfSum + found = true + default: + return errors.Errorf("unknown type %s", typ) + } + return nil + } + return d.Skip() + }) + }); err != nil { + return errors.Wrap(err, "capture") + } + if !found { + return errors.New("unable to detect sum type variant") + } + switch s.Type { + case InlineOneOfFooMergeDiscriminatorOneOfSum: + if err := s.InlineOneOfFoo.Decode(d); err != nil { + return err + } + case InlineOneOfBarMergeDiscriminatorOneOfSum: + if err := s.InlineOneOfBar.Decode(d); err != nil { + return err + } + default: + return errors.Errorf("inferred invalid type: %s", s.Type) + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s MergeDiscriminatorOneOfSum) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *MergeDiscriminatorOneOfSum) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *MergeUniqueFieldsOneOf) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *MergeUniqueFieldsOneOf) encodeFields(e *jx.Encoder) { + { + e.FieldStart("common") + e.Str(s.Common) + } + s.OneOf.encodeFields(e) +} + +var jsonFieldsNameOfMergeUniqueFieldsOneOf = [1]string{ + 0: "common", +} + +// Decode decodes MergeUniqueFieldsOneOf from json. +func (s *MergeUniqueFieldsOneOf) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode MergeUniqueFieldsOneOf to nil") + } + if err := d.Capture(func(d *jx.Decoder) error { + return s.OneOf.Decode(d) + }); err != nil { + return errors.Wrap(err, "decode field OneOf") + } + var requiredBitSet [1]uint8 + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "common": + requiredBitSet[0] |= 1 << 0 + if err := func() error { + v, err := d.Str() + s.Common = string(v) + if err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"common\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode MergeUniqueFieldsOneOf") + } + // Validate required fields. + var failures []validate.FieldError + for i, mask := range [1]uint8{ + 0b00000001, + } { + if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { + // Mask only required fields and check equality to mask using XOR. + // + // If XOR result is not zero, result is not equal to expected, so some fields are missed. + // Bits of fields which would be set are actually bits of missed fields. + missed := bits.OnesCount8(result) + for bitN := 0; bitN < missed; bitN++ { + bitIdx := bits.TrailingZeros8(result) + fieldIdx := i*8 + bitIdx + var name string + if fieldIdx < len(jsonFieldsNameOfMergeUniqueFieldsOneOf) { + name = jsonFieldsNameOfMergeUniqueFieldsOneOf[fieldIdx] } else { name = strconv.Itoa(fieldIdx) } @@ -2939,14 +3765,100 @@ func (s *MaxPropertiesTest) Decode(d *jx.Decoder) error { } // MarshalJSON implements stdjson.Marshaler. -func (s *MaxPropertiesTest) MarshalJSON() ([]byte, error) { +func (s *MergeUniqueFieldsOneOf) MarshalJSON() ([]byte, error) { e := jx.Encoder{} s.Encode(&e) return e.Bytes(), nil } // UnmarshalJSON implements stdjson.Unmarshaler. -func (s *MaxPropertiesTest) UnmarshalJSON(data []byte) error { +func (s *MergeUniqueFieldsOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode encodes MergeUniqueFieldsOneOfSum as json. +func (s MergeUniqueFieldsOneOfSum) Encode(e *jx.Encoder) { + switch s.Type { + case InlineOneOfFooMergeUniqueFieldsOneOfSum: + s.InlineOneOfFoo.Encode(e) + case InlineOneOfBarMergeUniqueFieldsOneOfSum: + s.InlineOneOfBar.Encode(e) + } +} + +func (s MergeUniqueFieldsOneOfSum) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineOneOfFooMergeUniqueFieldsOneOfSum: + s.InlineOneOfFoo.encodeFields(e) + case InlineOneOfBarMergeUniqueFieldsOneOfSum: + s.InlineOneOfBar.encodeFields(e) + } +} + +// Decode decodes MergeUniqueFieldsOneOfSum from json. +func (s *MergeUniqueFieldsOneOfSum) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode MergeUniqueFieldsOneOfSum to nil") + } + // Sum type fields. + if typ := d.Next(); typ != jx.Object { + return errors.Errorf("unexpected json type %q", typ) + } + + var found bool + if err := d.Capture(func(d *jx.Decoder) error { + return d.ObjBytes(func(d *jx.Decoder, key []byte) error { + switch string(key) { + case "foo": + match := InlineOneOfFooMergeUniqueFieldsOneOfSum + if found && s.Type != match { + s.Type = "" + return errors.Errorf("multiple oneOf matches: (%v, %v)", s.Type, match) + } + found = true + s.Type = match + case "bar": + match := InlineOneOfBarMergeUniqueFieldsOneOfSum + if found && s.Type != match { + s.Type = "" + return errors.Errorf("multiple oneOf matches: (%v, %v)", s.Type, match) + } + found = true + s.Type = match + } + return d.Skip() + }) + }); err != nil { + return errors.Wrap(err, "capture") + } + if !found { + return errors.New("unable to detect sum type variant") + } + switch s.Type { + case InlineOneOfFooMergeUniqueFieldsOneOfSum: + if err := s.InlineOneOfFoo.Decode(d); err != nil { + return err + } + case InlineOneOfBarMergeUniqueFieldsOneOfSum: + if err := s.InlineOneOfBar.Decode(d); err != nil { + return err + } + default: + return errors.Errorf("inferred invalid type: %s", s.Type) + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s MergeUniqueFieldsOneOfSum) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *MergeUniqueFieldsOneOfSum) UnmarshalJSON(data []byte) error { d := jx.DecodeBytes(data) return s.Decode(d) } @@ -3767,19 +4679,21 @@ func (s *OneOfBugs) UnmarshalJSON(data []byte) error { // Encode encodes OneOfMappingReference as json. func (s OneOfMappingReference) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s OneOfMappingReference) encodeFields(e *jx.Encoder) { switch s.Type { case OneOfMappingReferenceBOneOfMappingReference: - e.ObjStart() e.FieldStart("infoType") e.Str("extended") s.OneOfMappingReferenceB.encodeFields(e) - e.ObjEnd() case OneOfMappingReferenceAOneOfMappingReference: - e.ObjStart() e.FieldStart("infoType") e.Str("simple") s.OneOfMappingReferenceA.encodeFields(e) - e.ObjEnd() } } @@ -4407,6 +5321,15 @@ func (s OneVariantHasNoUniqueFields) Encode(e *jx.Encoder) { } } +func (s OneVariantHasNoUniqueFields) encodeFields(e *jx.Encoder) { + switch s.Type { + case OneVariantHasNoUniqueFields0OneVariantHasNoUniqueFields: + s.OneVariantHasNoUniqueFields0.encodeFields(e) + case OneVariantHasNoUniqueFields1OneVariantHasNoUniqueFields: + s.OneVariantHasNoUniqueFields1.encodeFields(e) + } +} + // Decode decodes OneVariantHasNoUniqueFields from json. func (s *OneVariantHasNoUniqueFields) Decode(d *jx.Decoder) error { if s == nil { @@ -5297,6 +6220,72 @@ func (s *OptIPv6) UnmarshalJSON(data []byte) error { return s.Decode(d) } +// Encode encodes InlineDiscriminatorOneOf as json. +func (o OptInlineDiscriminatorOneOf) Encode(e *jx.Encoder) { + if !o.Set { + return + } + o.Value.Encode(e) +} + +// Decode decodes InlineDiscriminatorOneOf from json. +func (o *OptInlineDiscriminatorOneOf) Decode(d *jx.Decoder) error { + if o == nil { + return errors.New("invalid: unable to decode OptInlineDiscriminatorOneOf to nil") + } + o.Set = true + if err := o.Value.Decode(d); err != nil { + return err + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s OptInlineDiscriminatorOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *OptInlineDiscriminatorOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode encodes InlineUniqueFieldsOneOf as json. +func (o OptInlineUniqueFieldsOneOf) Encode(e *jx.Encoder) { + if !o.Set { + return + } + o.Value.Encode(e) +} + +// Decode decodes InlineUniqueFieldsOneOf from json. +func (o *OptInlineUniqueFieldsOneOf) Decode(d *jx.Decoder) error { + if o == nil { + return errors.New("invalid: unable to decode OptInlineUniqueFieldsOneOf to nil") + } + o.Set = true + if err := o.Value.Decode(d); err != nil { + return err + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s OptInlineUniqueFieldsOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *OptInlineUniqueFieldsOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + // Encode encodes int as json. func (o OptInt) Encode(e *jx.Encoder) { if !o.Set { @@ -5500,6 +6489,72 @@ func (s *OptMaxPropertiesTest) UnmarshalJSON(data []byte) error { return s.Decode(d) } +// Encode encodes MergeDiscriminatorOneOf as json. +func (o OptMergeDiscriminatorOneOf) Encode(e *jx.Encoder) { + if !o.Set { + return + } + o.Value.Encode(e) +} + +// Decode decodes MergeDiscriminatorOneOf from json. +func (o *OptMergeDiscriminatorOneOf) Decode(d *jx.Decoder) error { + if o == nil { + return errors.New("invalid: unable to decode OptMergeDiscriminatorOneOf to nil") + } + o.Set = true + if err := o.Value.Decode(d); err != nil { + return err + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s OptMergeDiscriminatorOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *OptMergeDiscriminatorOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode encodes MergeUniqueFieldsOneOf as json. +func (o OptMergeUniqueFieldsOneOf) Encode(e *jx.Encoder) { + if !o.Set { + return + } + o.Value.Encode(e) +} + +// Decode decodes MergeUniqueFieldsOneOf from json. +func (o *OptMergeUniqueFieldsOneOf) Decode(d *jx.Decoder) error { + if o == nil { + return errors.New("invalid: unable to decode OptMergeUniqueFieldsOneOf to nil") + } + o.Set = true + if err := o.Value.Decode(d); err != nil { + return err + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s OptMergeUniqueFieldsOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *OptMergeUniqueFieldsOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + // Encode encodes string as json. func (o OptNilString) Encode(e *jx.Encoder) { if !o.Set { @@ -7690,6 +8745,120 @@ func (s *TestFloatValidation) UnmarshalJSON(data []byte) error { return s.Decode(d) } +// Encode implements json.Marshaler. +func (s *TestInlineOneOf) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *TestInlineOneOf) encodeFields(e *jx.Encoder) { + { + if s.InlineDiscriminator.Set { + e.FieldStart("inline_discriminator") + s.InlineDiscriminator.Encode(e) + } + } + { + if s.MergeDiscriminator.Set { + e.FieldStart("merge_discriminator") + s.MergeDiscriminator.Encode(e) + } + } + { + if s.InlineUniqueFields.Set { + e.FieldStart("inline_unique_fields") + s.InlineUniqueFields.Encode(e) + } + } + { + if s.MergeUniqueFields.Set { + e.FieldStart("merge_unique_fields") + s.MergeUniqueFields.Encode(e) + } + } +} + +var jsonFieldsNameOfTestInlineOneOf = [4]string{ + 0: "inline_discriminator", + 1: "merge_discriminator", + 2: "inline_unique_fields", + 3: "merge_unique_fields", +} + +// Decode decodes TestInlineOneOf from json. +func (s *TestInlineOneOf) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode TestInlineOneOf to nil") + } + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "inline_discriminator": + if err := func() error { + s.InlineDiscriminator.Reset() + if err := s.InlineDiscriminator.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"inline_discriminator\"") + } + case "merge_discriminator": + if err := func() error { + s.MergeDiscriminator.Reset() + if err := s.MergeDiscriminator.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"merge_discriminator\"") + } + case "inline_unique_fields": + if err := func() error { + s.InlineUniqueFields.Reset() + if err := s.InlineUniqueFields.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"inline_unique_fields\"") + } + case "merge_unique_fields": + if err := func() error { + s.MergeUniqueFields.Reset() + if err := s.MergeUniqueFields.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"merge_unique_fields\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode TestInlineOneOf") + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *TestInlineOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *TestInlineOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + // Encode encodes TestNullableOneofsCreated as json. func (s TestNullableOneofsCreated) Encode(e *jx.Encoder) { unwrapped := OneOfWithNullable(s) diff --git a/internal/integration/sample_api_ns/oas_response_decoders_gen.go b/internal/integration/sample_api_ns/oas_response_decoders_gen.go index 4eee2b373..5f1108161 100644 --- a/internal/integration/sample_api_ns/oas_response_decoders_gen.go +++ b/internal/integration/sample_api_ns/oas_response_decoders_gen.go @@ -1081,6 +1081,47 @@ func decodeTestFloatValidationResponse(resp *http.Response) (res *TestFloatValid return res, validate.UnexpectedStatusCode(resp.StatusCode) } +func decodeTestInlineOneofResponse(resp *http.Response) (res *TestInlineOneOf, _ error) { + switch resp.StatusCode { + case 200: + // Code 200. + ct, _, err := mime.ParseMediaType(resp.Header.Get("Content-Type")) + if err != nil { + return res, errors.Wrap(err, "parse media type") + } + switch { + case ct == "application/json": + buf, err := io.ReadAll(resp.Body) + if err != nil { + return res, err + } + d := jx.DecodeBytes(buf) + + var response TestInlineOneOf + if err := func() error { + if err := response.Decode(d); err != nil { + return err + } + if err := d.Skip(); err != io.EOF { + return errors.New("unexpected trailing data") + } + return nil + }(); err != nil { + err = &ogenerrors.DecodeBodyError{ + ContentType: ct, + Body: buf, + Err: err, + } + return res, err + } + return &response, nil + default: + return res, validate.InvalidContentType(ct) + } + } + return res, validate.UnexpectedStatusCode(resp.StatusCode) +} + func decodeTestNullableOneofsResponse(resp *http.Response) (res TestNullableOneofsRes, _ error) { switch resp.StatusCode { case 200: diff --git a/internal/integration/sample_api_ns/oas_schemas_gen.go b/internal/integration/sample_api_ns/oas_schemas_gen.go index 3a7f2f4d7..aa148b6aa 100644 --- a/internal/integration/sample_api_ns/oas_schemas_gen.go +++ b/internal/integration/sample_api_ns/oas_schemas_gen.go @@ -663,6 +663,224 @@ func NewIntID(v int) ID { return s } +// Ref: #/components/schemas/InlineDiscriminatorOneOf +type InlineDiscriminatorOneOf struct { + Common string `json:"common"` + OneOf InlineDiscriminatorOneOfSum +} + +// GetCommon returns the value of Common. +func (s *InlineDiscriminatorOneOf) GetCommon() string { + return s.Common +} + +// GetOneOf returns the value of OneOf. +func (s *InlineDiscriminatorOneOf) GetOneOf() InlineDiscriminatorOneOfSum { + return s.OneOf +} + +// SetCommon sets the value of Common. +func (s *InlineDiscriminatorOneOf) SetCommon(val string) { + s.Common = val +} + +// SetOneOf sets the value of OneOf. +func (s *InlineDiscriminatorOneOf) SetOneOf(val InlineDiscriminatorOneOfSum) { + s.OneOf = val +} + +// InlineDiscriminatorOneOfSum represents sum type. +type InlineDiscriminatorOneOfSum struct { + Type InlineDiscriminatorOneOfSumType // switch on this field + InlineOneOfFoo InlineOneOfFoo + InlineOneOfBar InlineOneOfBar +} + +// InlineDiscriminatorOneOfSumType is oneOf type of InlineDiscriminatorOneOfSum. +type InlineDiscriminatorOneOfSumType string + +// Possible values for InlineDiscriminatorOneOfSumType. +const ( + InlineOneOfFooInlineDiscriminatorOneOfSum InlineDiscriminatorOneOfSumType = "foo" + InlineOneOfBarInlineDiscriminatorOneOfSum InlineDiscriminatorOneOfSumType = "bar" +) + +// IsInlineOneOfFoo reports whether InlineDiscriminatorOneOfSum is InlineOneOfFoo. +func (s InlineDiscriminatorOneOfSum) IsInlineOneOfFoo() bool { + return s.Type == InlineOneOfFooInlineDiscriminatorOneOfSum +} + +// IsInlineOneOfBar reports whether InlineDiscriminatorOneOfSum is InlineOneOfBar. +func (s InlineDiscriminatorOneOfSum) IsInlineOneOfBar() bool { + return s.Type == InlineOneOfBarInlineDiscriminatorOneOfSum +} + +// SetInlineOneOfFoo sets InlineDiscriminatorOneOfSum to InlineOneOfFoo. +func (s *InlineDiscriminatorOneOfSum) SetInlineOneOfFoo(v InlineOneOfFoo) { + s.Type = InlineOneOfFooInlineDiscriminatorOneOfSum + s.InlineOneOfFoo = v +} + +// GetInlineOneOfFoo returns InlineOneOfFoo and true boolean if InlineDiscriminatorOneOfSum is InlineOneOfFoo. +func (s InlineDiscriminatorOneOfSum) GetInlineOneOfFoo() (v InlineOneOfFoo, ok bool) { + if !s.IsInlineOneOfFoo() { + return v, false + } + return s.InlineOneOfFoo, true +} + +// NewInlineOneOfFooInlineDiscriminatorOneOfSum returns new InlineDiscriminatorOneOfSum from InlineOneOfFoo. +func NewInlineOneOfFooInlineDiscriminatorOneOfSum(v InlineOneOfFoo) InlineDiscriminatorOneOfSum { + var s InlineDiscriminatorOneOfSum + s.SetInlineOneOfFoo(v) + return s +} + +// SetInlineOneOfBar sets InlineDiscriminatorOneOfSum to InlineOneOfBar. +func (s *InlineDiscriminatorOneOfSum) SetInlineOneOfBar(v InlineOneOfBar) { + s.Type = InlineOneOfBarInlineDiscriminatorOneOfSum + s.InlineOneOfBar = v +} + +// GetInlineOneOfBar returns InlineOneOfBar and true boolean if InlineDiscriminatorOneOfSum is InlineOneOfBar. +func (s InlineDiscriminatorOneOfSum) GetInlineOneOfBar() (v InlineOneOfBar, ok bool) { + if !s.IsInlineOneOfBar() { + return v, false + } + return s.InlineOneOfBar, true +} + +// NewInlineOneOfBarInlineDiscriminatorOneOfSum returns new InlineDiscriminatorOneOfSum from InlineOneOfBar. +func NewInlineOneOfBarInlineDiscriminatorOneOfSum(v InlineOneOfBar) InlineDiscriminatorOneOfSum { + var s InlineDiscriminatorOneOfSum + s.SetInlineOneOfBar(v) + return s +} + +// Ref: #/components/schemas/InlineOneOfBar +type InlineOneOfBar struct { + Bar OptString `json:"bar"` +} + +// GetBar returns the value of Bar. +func (s *InlineOneOfBar) GetBar() OptString { + return s.Bar +} + +// SetBar sets the value of Bar. +func (s *InlineOneOfBar) SetBar(val OptString) { + s.Bar = val +} + +// Ref: #/components/schemas/InlineOneOfFoo +type InlineOneOfFoo struct { + Foo OptString `json:"foo"` +} + +// GetFoo returns the value of Foo. +func (s *InlineOneOfFoo) GetFoo() OptString { + return s.Foo +} + +// SetFoo sets the value of Foo. +func (s *InlineOneOfFoo) SetFoo(val OptString) { + s.Foo = val +} + +// Ref: #/components/schemas/InlineUniqueFieldsOneOf +type InlineUniqueFieldsOneOf struct { + Common string `json:"common"` + OneOf InlineUniqueFieldsOneOfSum +} + +// GetCommon returns the value of Common. +func (s *InlineUniqueFieldsOneOf) GetCommon() string { + return s.Common +} + +// GetOneOf returns the value of OneOf. +func (s *InlineUniqueFieldsOneOf) GetOneOf() InlineUniqueFieldsOneOfSum { + return s.OneOf +} + +// SetCommon sets the value of Common. +func (s *InlineUniqueFieldsOneOf) SetCommon(val string) { + s.Common = val +} + +// SetOneOf sets the value of OneOf. +func (s *InlineUniqueFieldsOneOf) SetOneOf(val InlineUniqueFieldsOneOfSum) { + s.OneOf = val +} + +// InlineUniqueFieldsOneOfSum represents sum type. +type InlineUniqueFieldsOneOfSum struct { + Type InlineUniqueFieldsOneOfSumType // switch on this field + InlineOneOfFoo InlineOneOfFoo + InlineOneOfBar InlineOneOfBar +} + +// InlineUniqueFieldsOneOfSumType is oneOf type of InlineUniqueFieldsOneOfSum. +type InlineUniqueFieldsOneOfSumType string + +// Possible values for InlineUniqueFieldsOneOfSumType. +const ( + InlineOneOfFooInlineUniqueFieldsOneOfSum InlineUniqueFieldsOneOfSumType = "InlineOneOfFoo" + InlineOneOfBarInlineUniqueFieldsOneOfSum InlineUniqueFieldsOneOfSumType = "InlineOneOfBar" +) + +// IsInlineOneOfFoo reports whether InlineUniqueFieldsOneOfSum is InlineOneOfFoo. +func (s InlineUniqueFieldsOneOfSum) IsInlineOneOfFoo() bool { + return s.Type == InlineOneOfFooInlineUniqueFieldsOneOfSum +} + +// IsInlineOneOfBar reports whether InlineUniqueFieldsOneOfSum is InlineOneOfBar. +func (s InlineUniqueFieldsOneOfSum) IsInlineOneOfBar() bool { + return s.Type == InlineOneOfBarInlineUniqueFieldsOneOfSum +} + +// SetInlineOneOfFoo sets InlineUniqueFieldsOneOfSum to InlineOneOfFoo. +func (s *InlineUniqueFieldsOneOfSum) SetInlineOneOfFoo(v InlineOneOfFoo) { + s.Type = InlineOneOfFooInlineUniqueFieldsOneOfSum + s.InlineOneOfFoo = v +} + +// GetInlineOneOfFoo returns InlineOneOfFoo and true boolean if InlineUniqueFieldsOneOfSum is InlineOneOfFoo. +func (s InlineUniqueFieldsOneOfSum) GetInlineOneOfFoo() (v InlineOneOfFoo, ok bool) { + if !s.IsInlineOneOfFoo() { + return v, false + } + return s.InlineOneOfFoo, true +} + +// NewInlineOneOfFooInlineUniqueFieldsOneOfSum returns new InlineUniqueFieldsOneOfSum from InlineOneOfFoo. +func NewInlineOneOfFooInlineUniqueFieldsOneOfSum(v InlineOneOfFoo) InlineUniqueFieldsOneOfSum { + var s InlineUniqueFieldsOneOfSum + s.SetInlineOneOfFoo(v) + return s +} + +// SetInlineOneOfBar sets InlineUniqueFieldsOneOfSum to InlineOneOfBar. +func (s *InlineUniqueFieldsOneOfSum) SetInlineOneOfBar(v InlineOneOfBar) { + s.Type = InlineOneOfBarInlineUniqueFieldsOneOfSum + s.InlineOneOfBar = v +} + +// GetInlineOneOfBar returns InlineOneOfBar and true boolean if InlineUniqueFieldsOneOfSum is InlineOneOfBar. +func (s InlineUniqueFieldsOneOfSum) GetInlineOneOfBar() (v InlineOneOfBar, ok bool) { + if !s.IsInlineOneOfBar() { + return v, false + } + return s.InlineOneOfBar, true +} + +// NewInlineOneOfBarInlineUniqueFieldsOneOfSum returns new InlineUniqueFieldsOneOfSum from InlineOneOfBar. +func NewInlineOneOfBarInlineUniqueFieldsOneOfSum(v InlineOneOfBar) InlineUniqueFieldsOneOfSum { + var s InlineUniqueFieldsOneOfSum + s.SetInlineOneOfBar(v) + return s +} + // Ref: #/components/schemas/Issue143 // Issue143 represents sum type. type Issue143 struct { @@ -1212,6 +1430,196 @@ func (s *MaxPropertiesTest) SetOptionalC(val OptInt) { s.OptionalC = val } +// Merged schema. +// Ref: #/components/schemas/MergeDiscriminatorOneOf +type MergeDiscriminatorOneOf struct { + Common string `json:"common"` + OneOf MergeDiscriminatorOneOfSum +} + +// GetCommon returns the value of Common. +func (s *MergeDiscriminatorOneOf) GetCommon() string { + return s.Common +} + +// GetOneOf returns the value of OneOf. +func (s *MergeDiscriminatorOneOf) GetOneOf() MergeDiscriminatorOneOfSum { + return s.OneOf +} + +// SetCommon sets the value of Common. +func (s *MergeDiscriminatorOneOf) SetCommon(val string) { + s.Common = val +} + +// SetOneOf sets the value of OneOf. +func (s *MergeDiscriminatorOneOf) SetOneOf(val MergeDiscriminatorOneOfSum) { + s.OneOf = val +} + +// MergeDiscriminatorOneOfSum represents sum type. +type MergeDiscriminatorOneOfSum struct { + Type MergeDiscriminatorOneOfSumType // switch on this field + InlineOneOfFoo InlineOneOfFoo + InlineOneOfBar InlineOneOfBar +} + +// MergeDiscriminatorOneOfSumType is oneOf type of MergeDiscriminatorOneOfSum. +type MergeDiscriminatorOneOfSumType string + +// Possible values for MergeDiscriminatorOneOfSumType. +const ( + InlineOneOfFooMergeDiscriminatorOneOfSum MergeDiscriminatorOneOfSumType = "foo" + InlineOneOfBarMergeDiscriminatorOneOfSum MergeDiscriminatorOneOfSumType = "bar" +) + +// IsInlineOneOfFoo reports whether MergeDiscriminatorOneOfSum is InlineOneOfFoo. +func (s MergeDiscriminatorOneOfSum) IsInlineOneOfFoo() bool { + return s.Type == InlineOneOfFooMergeDiscriminatorOneOfSum +} + +// IsInlineOneOfBar reports whether MergeDiscriminatorOneOfSum is InlineOneOfBar. +func (s MergeDiscriminatorOneOfSum) IsInlineOneOfBar() bool { + return s.Type == InlineOneOfBarMergeDiscriminatorOneOfSum +} + +// SetInlineOneOfFoo sets MergeDiscriminatorOneOfSum to InlineOneOfFoo. +func (s *MergeDiscriminatorOneOfSum) SetInlineOneOfFoo(v InlineOneOfFoo) { + s.Type = InlineOneOfFooMergeDiscriminatorOneOfSum + s.InlineOneOfFoo = v +} + +// GetInlineOneOfFoo returns InlineOneOfFoo and true boolean if MergeDiscriminatorOneOfSum is InlineOneOfFoo. +func (s MergeDiscriminatorOneOfSum) GetInlineOneOfFoo() (v InlineOneOfFoo, ok bool) { + if !s.IsInlineOneOfFoo() { + return v, false + } + return s.InlineOneOfFoo, true +} + +// NewInlineOneOfFooMergeDiscriminatorOneOfSum returns new MergeDiscriminatorOneOfSum from InlineOneOfFoo. +func NewInlineOneOfFooMergeDiscriminatorOneOfSum(v InlineOneOfFoo) MergeDiscriminatorOneOfSum { + var s MergeDiscriminatorOneOfSum + s.SetInlineOneOfFoo(v) + return s +} + +// SetInlineOneOfBar sets MergeDiscriminatorOneOfSum to InlineOneOfBar. +func (s *MergeDiscriminatorOneOfSum) SetInlineOneOfBar(v InlineOneOfBar) { + s.Type = InlineOneOfBarMergeDiscriminatorOneOfSum + s.InlineOneOfBar = v +} + +// GetInlineOneOfBar returns InlineOneOfBar and true boolean if MergeDiscriminatorOneOfSum is InlineOneOfBar. +func (s MergeDiscriminatorOneOfSum) GetInlineOneOfBar() (v InlineOneOfBar, ok bool) { + if !s.IsInlineOneOfBar() { + return v, false + } + return s.InlineOneOfBar, true +} + +// NewInlineOneOfBarMergeDiscriminatorOneOfSum returns new MergeDiscriminatorOneOfSum from InlineOneOfBar. +func NewInlineOneOfBarMergeDiscriminatorOneOfSum(v InlineOneOfBar) MergeDiscriminatorOneOfSum { + var s MergeDiscriminatorOneOfSum + s.SetInlineOneOfBar(v) + return s +} + +// Merged schema. +// Ref: #/components/schemas/MergeUniqueFieldsOneOf +type MergeUniqueFieldsOneOf struct { + Common string `json:"common"` + OneOf MergeUniqueFieldsOneOfSum +} + +// GetCommon returns the value of Common. +func (s *MergeUniqueFieldsOneOf) GetCommon() string { + return s.Common +} + +// GetOneOf returns the value of OneOf. +func (s *MergeUniqueFieldsOneOf) GetOneOf() MergeUniqueFieldsOneOfSum { + return s.OneOf +} + +// SetCommon sets the value of Common. +func (s *MergeUniqueFieldsOneOf) SetCommon(val string) { + s.Common = val +} + +// SetOneOf sets the value of OneOf. +func (s *MergeUniqueFieldsOneOf) SetOneOf(val MergeUniqueFieldsOneOfSum) { + s.OneOf = val +} + +// MergeUniqueFieldsOneOfSum represents sum type. +type MergeUniqueFieldsOneOfSum struct { + Type MergeUniqueFieldsOneOfSumType // switch on this field + InlineOneOfFoo InlineOneOfFoo + InlineOneOfBar InlineOneOfBar +} + +// MergeUniqueFieldsOneOfSumType is oneOf type of MergeUniqueFieldsOneOfSum. +type MergeUniqueFieldsOneOfSumType string + +// Possible values for MergeUniqueFieldsOneOfSumType. +const ( + InlineOneOfFooMergeUniqueFieldsOneOfSum MergeUniqueFieldsOneOfSumType = "InlineOneOfFoo" + InlineOneOfBarMergeUniqueFieldsOneOfSum MergeUniqueFieldsOneOfSumType = "InlineOneOfBar" +) + +// IsInlineOneOfFoo reports whether MergeUniqueFieldsOneOfSum is InlineOneOfFoo. +func (s MergeUniqueFieldsOneOfSum) IsInlineOneOfFoo() bool { + return s.Type == InlineOneOfFooMergeUniqueFieldsOneOfSum +} + +// IsInlineOneOfBar reports whether MergeUniqueFieldsOneOfSum is InlineOneOfBar. +func (s MergeUniqueFieldsOneOfSum) IsInlineOneOfBar() bool { + return s.Type == InlineOneOfBarMergeUniqueFieldsOneOfSum +} + +// SetInlineOneOfFoo sets MergeUniqueFieldsOneOfSum to InlineOneOfFoo. +func (s *MergeUniqueFieldsOneOfSum) SetInlineOneOfFoo(v InlineOneOfFoo) { + s.Type = InlineOneOfFooMergeUniqueFieldsOneOfSum + s.InlineOneOfFoo = v +} + +// GetInlineOneOfFoo returns InlineOneOfFoo and true boolean if MergeUniqueFieldsOneOfSum is InlineOneOfFoo. +func (s MergeUniqueFieldsOneOfSum) GetInlineOneOfFoo() (v InlineOneOfFoo, ok bool) { + if !s.IsInlineOneOfFoo() { + return v, false + } + return s.InlineOneOfFoo, true +} + +// NewInlineOneOfFooMergeUniqueFieldsOneOfSum returns new MergeUniqueFieldsOneOfSum from InlineOneOfFoo. +func NewInlineOneOfFooMergeUniqueFieldsOneOfSum(v InlineOneOfFoo) MergeUniqueFieldsOneOfSum { + var s MergeUniqueFieldsOneOfSum + s.SetInlineOneOfFoo(v) + return s +} + +// SetInlineOneOfBar sets MergeUniqueFieldsOneOfSum to InlineOneOfBar. +func (s *MergeUniqueFieldsOneOfSum) SetInlineOneOfBar(v InlineOneOfBar) { + s.Type = InlineOneOfBarMergeUniqueFieldsOneOfSum + s.InlineOneOfBar = v +} + +// GetInlineOneOfBar returns InlineOneOfBar and true boolean if MergeUniqueFieldsOneOfSum is InlineOneOfBar. +func (s MergeUniqueFieldsOneOfSum) GetInlineOneOfBar() (v InlineOneOfBar, ok bool) { + if !s.IsInlineOneOfBar() { + return v, false + } + return s.InlineOneOfBar, true +} + +// NewInlineOneOfBarMergeUniqueFieldsOneOfSum returns new MergeUniqueFieldsOneOfSum from InlineOneOfBar. +func NewInlineOneOfBarMergeUniqueFieldsOneOfSum(v InlineOneOfBar) MergeUniqueFieldsOneOfSum { + var s MergeUniqueFieldsOneOfSum + s.SetInlineOneOfBar(v) + return s +} + // NewNilInt returns new NilInt with value set to v. func NewNilInt(v int) NilInt { return NilInt{ @@ -2995,6 +3403,98 @@ func (o OptIPv6) Or(d netip.Addr) netip.Addr { return d } +// NewOptInlineDiscriminatorOneOf returns new OptInlineDiscriminatorOneOf with value set to v. +func NewOptInlineDiscriminatorOneOf(v InlineDiscriminatorOneOf) OptInlineDiscriminatorOneOf { + return OptInlineDiscriminatorOneOf{ + Value: v, + Set: true, + } +} + +// OptInlineDiscriminatorOneOf is optional InlineDiscriminatorOneOf. +type OptInlineDiscriminatorOneOf struct { + Value InlineDiscriminatorOneOf + Set bool +} + +// IsSet returns true if OptInlineDiscriminatorOneOf was set. +func (o OptInlineDiscriminatorOneOf) IsSet() bool { return o.Set } + +// Reset unsets value. +func (o *OptInlineDiscriminatorOneOf) Reset() { + var v InlineDiscriminatorOneOf + o.Value = v + o.Set = false +} + +// SetTo sets value to v. +func (o *OptInlineDiscriminatorOneOf) SetTo(v InlineDiscriminatorOneOf) { + o.Set = true + o.Value = v +} + +// Get returns value and boolean that denotes whether value was set. +func (o OptInlineDiscriminatorOneOf) Get() (v InlineDiscriminatorOneOf, ok bool) { + if !o.Set { + return v, false + } + return o.Value, true +} + +// Or returns value if set, or given parameter if does not. +func (o OptInlineDiscriminatorOneOf) Or(d InlineDiscriminatorOneOf) InlineDiscriminatorOneOf { + if v, ok := o.Get(); ok { + return v + } + return d +} + +// NewOptInlineUniqueFieldsOneOf returns new OptInlineUniqueFieldsOneOf with value set to v. +func NewOptInlineUniqueFieldsOneOf(v InlineUniqueFieldsOneOf) OptInlineUniqueFieldsOneOf { + return OptInlineUniqueFieldsOneOf{ + Value: v, + Set: true, + } +} + +// OptInlineUniqueFieldsOneOf is optional InlineUniqueFieldsOneOf. +type OptInlineUniqueFieldsOneOf struct { + Value InlineUniqueFieldsOneOf + Set bool +} + +// IsSet returns true if OptInlineUniqueFieldsOneOf was set. +func (o OptInlineUniqueFieldsOneOf) IsSet() bool { return o.Set } + +// Reset unsets value. +func (o *OptInlineUniqueFieldsOneOf) Reset() { + var v InlineUniqueFieldsOneOf + o.Value = v + o.Set = false +} + +// SetTo sets value to v. +func (o *OptInlineUniqueFieldsOneOf) SetTo(v InlineUniqueFieldsOneOf) { + o.Set = true + o.Value = v +} + +// Get returns value and boolean that denotes whether value was set. +func (o OptInlineUniqueFieldsOneOf) Get() (v InlineUniqueFieldsOneOf, ok bool) { + if !o.Set { + return v, false + } + return o.Value, true +} + +// Or returns value if set, or given parameter if does not. +func (o OptInlineUniqueFieldsOneOf) Or(d InlineUniqueFieldsOneOf) InlineUniqueFieldsOneOf { + if v, ok := o.Get(); ok { + return v + } + return d +} + // NewOptInt returns new OptInt with value set to v. func NewOptInt(v int) OptInt { return OptInt{ @@ -3271,6 +3771,98 @@ func (o OptMaxPropertiesTest) Or(d MaxPropertiesTest) MaxPropertiesTest { return d } +// NewOptMergeDiscriminatorOneOf returns new OptMergeDiscriminatorOneOf with value set to v. +func NewOptMergeDiscriminatorOneOf(v MergeDiscriminatorOneOf) OptMergeDiscriminatorOneOf { + return OptMergeDiscriminatorOneOf{ + Value: v, + Set: true, + } +} + +// OptMergeDiscriminatorOneOf is optional MergeDiscriminatorOneOf. +type OptMergeDiscriminatorOneOf struct { + Value MergeDiscriminatorOneOf + Set bool +} + +// IsSet returns true if OptMergeDiscriminatorOneOf was set. +func (o OptMergeDiscriminatorOneOf) IsSet() bool { return o.Set } + +// Reset unsets value. +func (o *OptMergeDiscriminatorOneOf) Reset() { + var v MergeDiscriminatorOneOf + o.Value = v + o.Set = false +} + +// SetTo sets value to v. +func (o *OptMergeDiscriminatorOneOf) SetTo(v MergeDiscriminatorOneOf) { + o.Set = true + o.Value = v +} + +// Get returns value and boolean that denotes whether value was set. +func (o OptMergeDiscriminatorOneOf) Get() (v MergeDiscriminatorOneOf, ok bool) { + if !o.Set { + return v, false + } + return o.Value, true +} + +// Or returns value if set, or given parameter if does not. +func (o OptMergeDiscriminatorOneOf) Or(d MergeDiscriminatorOneOf) MergeDiscriminatorOneOf { + if v, ok := o.Get(); ok { + return v + } + return d +} + +// NewOptMergeUniqueFieldsOneOf returns new OptMergeUniqueFieldsOneOf with value set to v. +func NewOptMergeUniqueFieldsOneOf(v MergeUniqueFieldsOneOf) OptMergeUniqueFieldsOneOf { + return OptMergeUniqueFieldsOneOf{ + Value: v, + Set: true, + } +} + +// OptMergeUniqueFieldsOneOf is optional MergeUniqueFieldsOneOf. +type OptMergeUniqueFieldsOneOf struct { + Value MergeUniqueFieldsOneOf + Set bool +} + +// IsSet returns true if OptMergeUniqueFieldsOneOf was set. +func (o OptMergeUniqueFieldsOneOf) IsSet() bool { return o.Set } + +// Reset unsets value. +func (o *OptMergeUniqueFieldsOneOf) Reset() { + var v MergeUniqueFieldsOneOf + o.Value = v + o.Set = false +} + +// SetTo sets value to v. +func (o *OptMergeUniqueFieldsOneOf) SetTo(v MergeUniqueFieldsOneOf) { + o.Set = true + o.Value = v +} + +// Get returns value and boolean that denotes whether value was set. +func (o OptMergeUniqueFieldsOneOf) Get() (v MergeUniqueFieldsOneOf, ok bool) { + if !o.Set { + return v, false + } + return o.Value, true +} + +// Or returns value if set, or given parameter if does not. +func (o OptMergeUniqueFieldsOneOf) Or(d MergeUniqueFieldsOneOf) MergeUniqueFieldsOneOf { + if v, ok := o.Get(); ok { + return v + } + return d +} + // NewOptNilString returns new OptNilString with value set to v. func NewOptNilString(v string) OptNilString { return OptNilString{ @@ -4827,6 +5419,54 @@ func (s *TestFloatValidation) SetMultipleOf(val float64) { // TestFloatValidationOK is response for TestFloatValidation operation. type TestFloatValidationOK struct{} +// Ref: #/components/schemas/TestInlineOneOf +type TestInlineOneOf struct { + InlineDiscriminator OptInlineDiscriminatorOneOf `json:"inline_discriminator"` + MergeDiscriminator OptMergeDiscriminatorOneOf `json:"merge_discriminator"` + InlineUniqueFields OptInlineUniqueFieldsOneOf `json:"inline_unique_fields"` + MergeUniqueFields OptMergeUniqueFieldsOneOf `json:"merge_unique_fields"` +} + +// GetInlineDiscriminator returns the value of InlineDiscriminator. +func (s *TestInlineOneOf) GetInlineDiscriminator() OptInlineDiscriminatorOneOf { + return s.InlineDiscriminator +} + +// GetMergeDiscriminator returns the value of MergeDiscriminator. +func (s *TestInlineOneOf) GetMergeDiscriminator() OptMergeDiscriminatorOneOf { + return s.MergeDiscriminator +} + +// GetInlineUniqueFields returns the value of InlineUniqueFields. +func (s *TestInlineOneOf) GetInlineUniqueFields() OptInlineUniqueFieldsOneOf { + return s.InlineUniqueFields +} + +// GetMergeUniqueFields returns the value of MergeUniqueFields. +func (s *TestInlineOneOf) GetMergeUniqueFields() OptMergeUniqueFieldsOneOf { + return s.MergeUniqueFields +} + +// SetInlineDiscriminator sets the value of InlineDiscriminator. +func (s *TestInlineOneOf) SetInlineDiscriminator(val OptInlineDiscriminatorOneOf) { + s.InlineDiscriminator = val +} + +// SetMergeDiscriminator sets the value of MergeDiscriminator. +func (s *TestInlineOneOf) SetMergeDiscriminator(val OptMergeDiscriminatorOneOf) { + s.MergeDiscriminator = val +} + +// SetInlineUniqueFields sets the value of InlineUniqueFields. +func (s *TestInlineOneOf) SetInlineUniqueFields(val OptInlineUniqueFieldsOneOf) { + s.InlineUniqueFields = val +} + +// SetMergeUniqueFields sets the value of MergeUniqueFields. +func (s *TestInlineOneOf) SetMergeUniqueFields(val OptMergeUniqueFieldsOneOf) { + s.MergeUniqueFields = val +} + type TestNullableOneofsCreated OneOfWithNullable func (*TestNullableOneofsCreated) testNullableOneofsRes() {} diff --git a/internal/integration/sample_api_nsnc/oas_json_gen.go b/internal/integration/sample_api_nsnc/oas_json_gen.go index 3cac3c721..e2fb67cfd 100644 --- a/internal/integration/sample_api_nsnc/oas_json_gen.go +++ b/internal/integration/sample_api_nsnc/oas_json_gen.go @@ -583,6 +583,15 @@ func (s DataDescription) Encode(e *jx.Encoder) { } } +func (s DataDescription) encodeFields(e *jx.Encoder) { + switch s.Type { + case DescriptionDetailedDataDescription: + s.DescriptionDetailed.encodeFields(e) + case DescriptionSimpleDataDescription: + s.DescriptionSimple.encodeFields(e) + } +} + // Decode decodes DataDescription from json. func (s *DataDescription) Decode(d *jx.Decoder) error { if s == nil { @@ -1403,29 +1412,539 @@ func (s *ID) Decode(d *jx.Decoder) error { if err != nil { return err } - s.Type = IntID - case jx.String: - v, err := d.Str() - s.String = string(v) - if err != nil { + s.Type = IntID + case jx.String: + v, err := d.Str() + s.String = string(v) + if err != nil { + return err + } + s.Type = StringID + default: + return errors.Errorf("unexpected json type %q", t) + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s ID) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *ID) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *InlineDiscriminatorOneOf) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *InlineDiscriminatorOneOf) encodeFields(e *jx.Encoder) { + { + e.FieldStart("common") + e.Str(s.Common) + } + s.OneOf.encodeFields(e) +} + +var jsonFieldsNameOfInlineDiscriminatorOneOf = [1]string{ + 0: "common", +} + +// Decode decodes InlineDiscriminatorOneOf from json. +func (s *InlineDiscriminatorOneOf) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode InlineDiscriminatorOneOf to nil") + } + if err := d.Capture(func(d *jx.Decoder) error { + return s.OneOf.Decode(d) + }); err != nil { + return errors.Wrap(err, "decode field OneOf") + } + var requiredBitSet [1]uint8 + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "common": + requiredBitSet[0] |= 1 << 0 + if err := func() error { + v, err := d.Str() + s.Common = string(v) + if err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"common\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode InlineDiscriminatorOneOf") + } + // Validate required fields. + var failures []validate.FieldError + for i, mask := range [1]uint8{ + 0b00000001, + } { + if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { + // Mask only required fields and check equality to mask using XOR. + // + // If XOR result is not zero, result is not equal to expected, so some fields are missed. + // Bits of fields which would be set are actually bits of missed fields. + missed := bits.OnesCount8(result) + for bitN := 0; bitN < missed; bitN++ { + bitIdx := bits.TrailingZeros8(result) + fieldIdx := i*8 + bitIdx + var name string + if fieldIdx < len(jsonFieldsNameOfInlineDiscriminatorOneOf) { + name = jsonFieldsNameOfInlineDiscriminatorOneOf[fieldIdx] + } else { + name = strconv.Itoa(fieldIdx) + } + failures = append(failures, validate.FieldError{ + Name: name, + Error: validate.ErrFieldRequired, + }) + // Reset bit. + result &^= 1 << bitIdx + } + } + } + if len(failures) > 0 { + return &validate.Error{Fields: failures} + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *InlineDiscriminatorOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *InlineDiscriminatorOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode encodes InlineDiscriminatorOneOfSum as json. +func (s InlineDiscriminatorOneOfSum) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s InlineDiscriminatorOneOfSum) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineOneOfBarInlineDiscriminatorOneOfSum: + e.FieldStart("kind") + e.Str("bar") + s.InlineOneOfBar.encodeFields(e) + case InlineOneOfFooInlineDiscriminatorOneOfSum: + e.FieldStart("kind") + e.Str("foo") + s.InlineOneOfFoo.encodeFields(e) + } +} + +// Decode decodes InlineDiscriminatorOneOfSum from json. +func (s *InlineDiscriminatorOneOfSum) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode InlineDiscriminatorOneOfSum to nil") + } + // Sum type discriminator. + if typ := d.Next(); typ != jx.Object { + return errors.Errorf("unexpected json type %q", typ) + } + + var found bool + if err := d.Capture(func(d *jx.Decoder) error { + return d.ObjBytes(func(d *jx.Decoder, key []byte) error { + if found { + return d.Skip() + } + switch string(key) { + case "kind": + typ, err := d.Str() + if err != nil { + return err + } + switch typ { + case "bar": + s.Type = InlineOneOfBarInlineDiscriminatorOneOfSum + found = true + case "foo": + s.Type = InlineOneOfFooInlineDiscriminatorOneOfSum + found = true + default: + return errors.Errorf("unknown type %s", typ) + } + return nil + } + return d.Skip() + }) + }); err != nil { + return errors.Wrap(err, "capture") + } + if !found { + return errors.New("unable to detect sum type variant") + } + switch s.Type { + case InlineOneOfFooInlineDiscriminatorOneOfSum: + if err := s.InlineOneOfFoo.Decode(d); err != nil { + return err + } + case InlineOneOfBarInlineDiscriminatorOneOfSum: + if err := s.InlineOneOfBar.Decode(d); err != nil { + return err + } + default: + return errors.Errorf("inferred invalid type: %s", s.Type) + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s InlineDiscriminatorOneOfSum) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *InlineDiscriminatorOneOfSum) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *InlineOneOfBar) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *InlineOneOfBar) encodeFields(e *jx.Encoder) { + { + if s.Bar.Set { + e.FieldStart("bar") + s.Bar.Encode(e) + } + } +} + +var jsonFieldsNameOfInlineOneOfBar = [1]string{ + 0: "bar", +} + +// Decode decodes InlineOneOfBar from json. +func (s *InlineOneOfBar) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode InlineOneOfBar to nil") + } + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "bar": + if err := func() error { + s.Bar.Reset() + if err := s.Bar.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"bar\"") + } + case "kind": + return d.Skip() + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode InlineOneOfBar") + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *InlineOneOfBar) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *InlineOneOfBar) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *InlineOneOfFoo) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *InlineOneOfFoo) encodeFields(e *jx.Encoder) { + { + if s.Foo.Set { + e.FieldStart("foo") + s.Foo.Encode(e) + } + } +} + +var jsonFieldsNameOfInlineOneOfFoo = [1]string{ + 0: "foo", +} + +// Decode decodes InlineOneOfFoo from json. +func (s *InlineOneOfFoo) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode InlineOneOfFoo to nil") + } + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "foo": + if err := func() error { + s.Foo.Reset() + if err := s.Foo.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"foo\"") + } + case "kind": + return d.Skip() + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode InlineOneOfFoo") + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *InlineOneOfFoo) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *InlineOneOfFoo) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *InlineUniqueFieldsOneOf) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *InlineUniqueFieldsOneOf) encodeFields(e *jx.Encoder) { + { + e.FieldStart("common") + e.Str(s.Common) + } + s.OneOf.encodeFields(e) +} + +var jsonFieldsNameOfInlineUniqueFieldsOneOf = [1]string{ + 0: "common", +} + +// Decode decodes InlineUniqueFieldsOneOf from json. +func (s *InlineUniqueFieldsOneOf) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode InlineUniqueFieldsOneOf to nil") + } + if err := d.Capture(func(d *jx.Decoder) error { + return s.OneOf.Decode(d) + }); err != nil { + return errors.Wrap(err, "decode field OneOf") + } + var requiredBitSet [1]uint8 + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "common": + requiredBitSet[0] |= 1 << 0 + if err := func() error { + v, err := d.Str() + s.Common = string(v) + if err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"common\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode InlineUniqueFieldsOneOf") + } + // Validate required fields. + var failures []validate.FieldError + for i, mask := range [1]uint8{ + 0b00000001, + } { + if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { + // Mask only required fields and check equality to mask using XOR. + // + // If XOR result is not zero, result is not equal to expected, so some fields are missed. + // Bits of fields which would be set are actually bits of missed fields. + missed := bits.OnesCount8(result) + for bitN := 0; bitN < missed; bitN++ { + bitIdx := bits.TrailingZeros8(result) + fieldIdx := i*8 + bitIdx + var name string + if fieldIdx < len(jsonFieldsNameOfInlineUniqueFieldsOneOf) { + name = jsonFieldsNameOfInlineUniqueFieldsOneOf[fieldIdx] + } else { + name = strconv.Itoa(fieldIdx) + } + failures = append(failures, validate.FieldError{ + Name: name, + Error: validate.ErrFieldRequired, + }) + // Reset bit. + result &^= 1 << bitIdx + } + } + } + if len(failures) > 0 { + return &validate.Error{Fields: failures} + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *InlineUniqueFieldsOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *InlineUniqueFieldsOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode encodes InlineUniqueFieldsOneOfSum as json. +func (s InlineUniqueFieldsOneOfSum) Encode(e *jx.Encoder) { + switch s.Type { + case InlineOneOfFooInlineUniqueFieldsOneOfSum: + s.InlineOneOfFoo.Encode(e) + case InlineOneOfBarInlineUniqueFieldsOneOfSum: + s.InlineOneOfBar.Encode(e) + } +} + +func (s InlineUniqueFieldsOneOfSum) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineOneOfFooInlineUniqueFieldsOneOfSum: + s.InlineOneOfFoo.encodeFields(e) + case InlineOneOfBarInlineUniqueFieldsOneOfSum: + s.InlineOneOfBar.encodeFields(e) + } +} + +// Decode decodes InlineUniqueFieldsOneOfSum from json. +func (s *InlineUniqueFieldsOneOfSum) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode InlineUniqueFieldsOneOfSum to nil") + } + // Sum type fields. + if typ := d.Next(); typ != jx.Object { + return errors.Errorf("unexpected json type %q", typ) + } + + var found bool + if err := d.Capture(func(d *jx.Decoder) error { + return d.ObjBytes(func(d *jx.Decoder, key []byte) error { + switch string(key) { + case "foo": + match := InlineOneOfFooInlineUniqueFieldsOneOfSum + if found && s.Type != match { + s.Type = "" + return errors.Errorf("multiple oneOf matches: (%v, %v)", s.Type, match) + } + found = true + s.Type = match + case "bar": + match := InlineOneOfBarInlineUniqueFieldsOneOfSum + if found && s.Type != match { + s.Type = "" + return errors.Errorf("multiple oneOf matches: (%v, %v)", s.Type, match) + } + found = true + s.Type = match + } + return d.Skip() + }) + }); err != nil { + return errors.Wrap(err, "capture") + } + if !found { + return errors.New("unable to detect sum type variant") + } + switch s.Type { + case InlineOneOfFooInlineUniqueFieldsOneOfSum: + if err := s.InlineOneOfFoo.Decode(d); err != nil { + return err + } + case InlineOneOfBarInlineUniqueFieldsOneOfSum: + if err := s.InlineOneOfBar.Decode(d); err != nil { return err } - s.Type = StringID default: - return errors.Errorf("unexpected json type %q", t) + return errors.Errorf("inferred invalid type: %s", s.Type) } return nil } // MarshalJSON implements stdjson.Marshaler. -func (s ID) MarshalJSON() ([]byte, error) { +func (s InlineUniqueFieldsOneOfSum) MarshalJSON() ([]byte, error) { e := jx.Encoder{} s.Encode(&e) return e.Bytes(), nil } // UnmarshalJSON implements stdjson.Unmarshaler. -func (s *ID) UnmarshalJSON(data []byte) error { +func (s *InlineUniqueFieldsOneOfSum) UnmarshalJSON(data []byte) error { d := jx.DecodeBytes(data) return s.Decode(d) } @@ -1444,6 +1963,19 @@ func (s Issue143) Encode(e *jx.Encoder) { } } +func (s Issue143) encodeFields(e *jx.Encoder) { + switch s.Type { + case Issue1430Issue143: + s.Issue1430.encodeFields(e) + case Issue1431Issue143: + s.Issue1431.encodeFields(e) + case Issue1432Issue143: + s.Issue1432.encodeFields(e) + case Issue1433Issue143: + s.Issue1433.encodeFields(e) + } +} + // Decode decodes Issue143 from json. func (s *Issue143) Decode(d *jx.Decoder) error { if s == nil { @@ -2057,25 +2589,25 @@ func (s *Issue1433) UnmarshalJSON(data []byte) error { // Encode encodes Issue943 as json. func (s Issue943) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s Issue943) encodeFields(e *jx.Encoder) { switch s.Type { case Issue943Variant1Issue943: - e.ObjStart() e.FieldStart("selector") e.Str("variant1") s.Issue943Variant1.encodeFields(e) - e.ObjEnd() case Issue943Variant2Issue943: - e.ObjStart() e.FieldStart("selector") e.Str("variant2") s.Issue943Variant2.encodeFields(e) - e.ObjEnd() case Issue943MapIssue943: - e.ObjStart() e.FieldStart("selector") e.Str("variant3") s.Issue943Map.encodeFields(e) - e.ObjEnd() } } @@ -2917,8 +3449,302 @@ func (s *MaxPropertiesTest) Decode(d *jx.Decoder) error { bitIdx := bits.TrailingZeros8(result) fieldIdx := i*8 + bitIdx var name string - if fieldIdx < len(jsonFieldsNameOfMaxPropertiesTest) { - name = jsonFieldsNameOfMaxPropertiesTest[fieldIdx] + if fieldIdx < len(jsonFieldsNameOfMaxPropertiesTest) { + name = jsonFieldsNameOfMaxPropertiesTest[fieldIdx] + } else { + name = strconv.Itoa(fieldIdx) + } + failures = append(failures, validate.FieldError{ + Name: name, + Error: validate.ErrFieldRequired, + }) + // Reset bit. + result &^= 1 << bitIdx + } + } + } + if len(failures) > 0 { + return &validate.Error{Fields: failures} + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *MaxPropertiesTest) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *MaxPropertiesTest) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *MergeDiscriminatorOneOf) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *MergeDiscriminatorOneOf) encodeFields(e *jx.Encoder) { + { + e.FieldStart("common") + e.Str(s.Common) + } + s.OneOf.encodeFields(e) +} + +var jsonFieldsNameOfMergeDiscriminatorOneOf = [1]string{ + 0: "common", +} + +// Decode decodes MergeDiscriminatorOneOf from json. +func (s *MergeDiscriminatorOneOf) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode MergeDiscriminatorOneOf to nil") + } + if err := d.Capture(func(d *jx.Decoder) error { + return s.OneOf.Decode(d) + }); err != nil { + return errors.Wrap(err, "decode field OneOf") + } + var requiredBitSet [1]uint8 + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "common": + requiredBitSet[0] |= 1 << 0 + if err := func() error { + v, err := d.Str() + s.Common = string(v) + if err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"common\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode MergeDiscriminatorOneOf") + } + // Validate required fields. + var failures []validate.FieldError + for i, mask := range [1]uint8{ + 0b00000001, + } { + if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { + // Mask only required fields and check equality to mask using XOR. + // + // If XOR result is not zero, result is not equal to expected, so some fields are missed. + // Bits of fields which would be set are actually bits of missed fields. + missed := bits.OnesCount8(result) + for bitN := 0; bitN < missed; bitN++ { + bitIdx := bits.TrailingZeros8(result) + fieldIdx := i*8 + bitIdx + var name string + if fieldIdx < len(jsonFieldsNameOfMergeDiscriminatorOneOf) { + name = jsonFieldsNameOfMergeDiscriminatorOneOf[fieldIdx] + } else { + name = strconv.Itoa(fieldIdx) + } + failures = append(failures, validate.FieldError{ + Name: name, + Error: validate.ErrFieldRequired, + }) + // Reset bit. + result &^= 1 << bitIdx + } + } + } + if len(failures) > 0 { + return &validate.Error{Fields: failures} + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *MergeDiscriminatorOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *MergeDiscriminatorOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode encodes MergeDiscriminatorOneOfSum as json. +func (s MergeDiscriminatorOneOfSum) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s MergeDiscriminatorOneOfSum) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineOneOfBarMergeDiscriminatorOneOfSum: + e.FieldStart("kind") + e.Str("bar") + s.InlineOneOfBar.encodeFields(e) + case InlineOneOfFooMergeDiscriminatorOneOfSum: + e.FieldStart("kind") + e.Str("foo") + s.InlineOneOfFoo.encodeFields(e) + } +} + +// Decode decodes MergeDiscriminatorOneOfSum from json. +func (s *MergeDiscriminatorOneOfSum) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode MergeDiscriminatorOneOfSum to nil") + } + // Sum type discriminator. + if typ := d.Next(); typ != jx.Object { + return errors.Errorf("unexpected json type %q", typ) + } + + var found bool + if err := d.Capture(func(d *jx.Decoder) error { + return d.ObjBytes(func(d *jx.Decoder, key []byte) error { + if found { + return d.Skip() + } + switch string(key) { + case "kind": + typ, err := d.Str() + if err != nil { + return err + } + switch typ { + case "bar": + s.Type = InlineOneOfBarMergeDiscriminatorOneOfSum + found = true + case "foo": + s.Type = InlineOneOfFooMergeDiscriminatorOneOfSum + found = true + default: + return errors.Errorf("unknown type %s", typ) + } + return nil + } + return d.Skip() + }) + }); err != nil { + return errors.Wrap(err, "capture") + } + if !found { + return errors.New("unable to detect sum type variant") + } + switch s.Type { + case InlineOneOfFooMergeDiscriminatorOneOfSum: + if err := s.InlineOneOfFoo.Decode(d); err != nil { + return err + } + case InlineOneOfBarMergeDiscriminatorOneOfSum: + if err := s.InlineOneOfBar.Decode(d); err != nil { + return err + } + default: + return errors.Errorf("inferred invalid type: %s", s.Type) + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s MergeDiscriminatorOneOfSum) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *MergeDiscriminatorOneOfSum) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *MergeUniqueFieldsOneOf) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *MergeUniqueFieldsOneOf) encodeFields(e *jx.Encoder) { + { + e.FieldStart("common") + e.Str(s.Common) + } + s.OneOf.encodeFields(e) +} + +var jsonFieldsNameOfMergeUniqueFieldsOneOf = [1]string{ + 0: "common", +} + +// Decode decodes MergeUniqueFieldsOneOf from json. +func (s *MergeUniqueFieldsOneOf) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode MergeUniqueFieldsOneOf to nil") + } + if err := d.Capture(func(d *jx.Decoder) error { + return s.OneOf.Decode(d) + }); err != nil { + return errors.Wrap(err, "decode field OneOf") + } + var requiredBitSet [1]uint8 + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "common": + requiredBitSet[0] |= 1 << 0 + if err := func() error { + v, err := d.Str() + s.Common = string(v) + if err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"common\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode MergeUniqueFieldsOneOf") + } + // Validate required fields. + var failures []validate.FieldError + for i, mask := range [1]uint8{ + 0b00000001, + } { + if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { + // Mask only required fields and check equality to mask using XOR. + // + // If XOR result is not zero, result is not equal to expected, so some fields are missed. + // Bits of fields which would be set are actually bits of missed fields. + missed := bits.OnesCount8(result) + for bitN := 0; bitN < missed; bitN++ { + bitIdx := bits.TrailingZeros8(result) + fieldIdx := i*8 + bitIdx + var name string + if fieldIdx < len(jsonFieldsNameOfMergeUniqueFieldsOneOf) { + name = jsonFieldsNameOfMergeUniqueFieldsOneOf[fieldIdx] } else { name = strconv.Itoa(fieldIdx) } @@ -2939,14 +3765,100 @@ func (s *MaxPropertiesTest) Decode(d *jx.Decoder) error { } // MarshalJSON implements stdjson.Marshaler. -func (s *MaxPropertiesTest) MarshalJSON() ([]byte, error) { +func (s *MergeUniqueFieldsOneOf) MarshalJSON() ([]byte, error) { e := jx.Encoder{} s.Encode(&e) return e.Bytes(), nil } // UnmarshalJSON implements stdjson.Unmarshaler. -func (s *MaxPropertiesTest) UnmarshalJSON(data []byte) error { +func (s *MergeUniqueFieldsOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode encodes MergeUniqueFieldsOneOfSum as json. +func (s MergeUniqueFieldsOneOfSum) Encode(e *jx.Encoder) { + switch s.Type { + case InlineOneOfFooMergeUniqueFieldsOneOfSum: + s.InlineOneOfFoo.Encode(e) + case InlineOneOfBarMergeUniqueFieldsOneOfSum: + s.InlineOneOfBar.Encode(e) + } +} + +func (s MergeUniqueFieldsOneOfSum) encodeFields(e *jx.Encoder) { + switch s.Type { + case InlineOneOfFooMergeUniqueFieldsOneOfSum: + s.InlineOneOfFoo.encodeFields(e) + case InlineOneOfBarMergeUniqueFieldsOneOfSum: + s.InlineOneOfBar.encodeFields(e) + } +} + +// Decode decodes MergeUniqueFieldsOneOfSum from json. +func (s *MergeUniqueFieldsOneOfSum) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode MergeUniqueFieldsOneOfSum to nil") + } + // Sum type fields. + if typ := d.Next(); typ != jx.Object { + return errors.Errorf("unexpected json type %q", typ) + } + + var found bool + if err := d.Capture(func(d *jx.Decoder) error { + return d.ObjBytes(func(d *jx.Decoder, key []byte) error { + switch string(key) { + case "foo": + match := InlineOneOfFooMergeUniqueFieldsOneOfSum + if found && s.Type != match { + s.Type = "" + return errors.Errorf("multiple oneOf matches: (%v, %v)", s.Type, match) + } + found = true + s.Type = match + case "bar": + match := InlineOneOfBarMergeUniqueFieldsOneOfSum + if found && s.Type != match { + s.Type = "" + return errors.Errorf("multiple oneOf matches: (%v, %v)", s.Type, match) + } + found = true + s.Type = match + } + return d.Skip() + }) + }); err != nil { + return errors.Wrap(err, "capture") + } + if !found { + return errors.New("unable to detect sum type variant") + } + switch s.Type { + case InlineOneOfFooMergeUniqueFieldsOneOfSum: + if err := s.InlineOneOfFoo.Decode(d); err != nil { + return err + } + case InlineOneOfBarMergeUniqueFieldsOneOfSum: + if err := s.InlineOneOfBar.Decode(d); err != nil { + return err + } + default: + return errors.Errorf("inferred invalid type: %s", s.Type) + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s MergeUniqueFieldsOneOfSum) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *MergeUniqueFieldsOneOfSum) UnmarshalJSON(data []byte) error { d := jx.DecodeBytes(data) return s.Decode(d) } @@ -3767,19 +4679,21 @@ func (s *OneOfBugs) UnmarshalJSON(data []byte) error { // Encode encodes OneOfMappingReference as json. func (s OneOfMappingReference) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +func (s OneOfMappingReference) encodeFields(e *jx.Encoder) { switch s.Type { case OneOfMappingReferenceBOneOfMappingReference: - e.ObjStart() e.FieldStart("infoType") e.Str("extended") s.OneOfMappingReferenceB.encodeFields(e) - e.ObjEnd() case OneOfMappingReferenceAOneOfMappingReference: - e.ObjStart() e.FieldStart("infoType") e.Str("simple") s.OneOfMappingReferenceA.encodeFields(e) - e.ObjEnd() } } @@ -4407,6 +5321,15 @@ func (s OneVariantHasNoUniqueFields) Encode(e *jx.Encoder) { } } +func (s OneVariantHasNoUniqueFields) encodeFields(e *jx.Encoder) { + switch s.Type { + case OneVariantHasNoUniqueFields0OneVariantHasNoUniqueFields: + s.OneVariantHasNoUniqueFields0.encodeFields(e) + case OneVariantHasNoUniqueFields1OneVariantHasNoUniqueFields: + s.OneVariantHasNoUniqueFields1.encodeFields(e) + } +} + // Decode decodes OneVariantHasNoUniqueFields from json. func (s *OneVariantHasNoUniqueFields) Decode(d *jx.Decoder) error { if s == nil { @@ -5297,6 +6220,72 @@ func (s *OptIPv6) UnmarshalJSON(data []byte) error { return s.Decode(d) } +// Encode encodes InlineDiscriminatorOneOf as json. +func (o OptInlineDiscriminatorOneOf) Encode(e *jx.Encoder) { + if !o.Set { + return + } + o.Value.Encode(e) +} + +// Decode decodes InlineDiscriminatorOneOf from json. +func (o *OptInlineDiscriminatorOneOf) Decode(d *jx.Decoder) error { + if o == nil { + return errors.New("invalid: unable to decode OptInlineDiscriminatorOneOf to nil") + } + o.Set = true + if err := o.Value.Decode(d); err != nil { + return err + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s OptInlineDiscriminatorOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *OptInlineDiscriminatorOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode encodes InlineUniqueFieldsOneOf as json. +func (o OptInlineUniqueFieldsOneOf) Encode(e *jx.Encoder) { + if !o.Set { + return + } + o.Value.Encode(e) +} + +// Decode decodes InlineUniqueFieldsOneOf from json. +func (o *OptInlineUniqueFieldsOneOf) Decode(d *jx.Decoder) error { + if o == nil { + return errors.New("invalid: unable to decode OptInlineUniqueFieldsOneOf to nil") + } + o.Set = true + if err := o.Value.Decode(d); err != nil { + return err + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s OptInlineUniqueFieldsOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *OptInlineUniqueFieldsOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + // Encode encodes int as json. func (o OptInt) Encode(e *jx.Encoder) { if !o.Set { @@ -5500,6 +6489,72 @@ func (s *OptMaxPropertiesTest) UnmarshalJSON(data []byte) error { return s.Decode(d) } +// Encode encodes MergeDiscriminatorOneOf as json. +func (o OptMergeDiscriminatorOneOf) Encode(e *jx.Encoder) { + if !o.Set { + return + } + o.Value.Encode(e) +} + +// Decode decodes MergeDiscriminatorOneOf from json. +func (o *OptMergeDiscriminatorOneOf) Decode(d *jx.Decoder) error { + if o == nil { + return errors.New("invalid: unable to decode OptMergeDiscriminatorOneOf to nil") + } + o.Set = true + if err := o.Value.Decode(d); err != nil { + return err + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s OptMergeDiscriminatorOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *OptMergeDiscriminatorOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode encodes MergeUniqueFieldsOneOf as json. +func (o OptMergeUniqueFieldsOneOf) Encode(e *jx.Encoder) { + if !o.Set { + return + } + o.Value.Encode(e) +} + +// Decode decodes MergeUniqueFieldsOneOf from json. +func (o *OptMergeUniqueFieldsOneOf) Decode(d *jx.Decoder) error { + if o == nil { + return errors.New("invalid: unable to decode OptMergeUniqueFieldsOneOf to nil") + } + o.Set = true + if err := o.Value.Decode(d); err != nil { + return err + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s OptMergeUniqueFieldsOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *OptMergeUniqueFieldsOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + // Encode encodes string as json. func (o OptNilString) Encode(e *jx.Encoder) { if !o.Set { @@ -7690,6 +8745,120 @@ func (s *TestFloatValidation) UnmarshalJSON(data []byte) error { return s.Decode(d) } +// Encode implements json.Marshaler. +func (s *TestInlineOneOf) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *TestInlineOneOf) encodeFields(e *jx.Encoder) { + { + if s.InlineDiscriminator.Set { + e.FieldStart("inline_discriminator") + s.InlineDiscriminator.Encode(e) + } + } + { + if s.MergeDiscriminator.Set { + e.FieldStart("merge_discriminator") + s.MergeDiscriminator.Encode(e) + } + } + { + if s.InlineUniqueFields.Set { + e.FieldStart("inline_unique_fields") + s.InlineUniqueFields.Encode(e) + } + } + { + if s.MergeUniqueFields.Set { + e.FieldStart("merge_unique_fields") + s.MergeUniqueFields.Encode(e) + } + } +} + +var jsonFieldsNameOfTestInlineOneOf = [4]string{ + 0: "inline_discriminator", + 1: "merge_discriminator", + 2: "inline_unique_fields", + 3: "merge_unique_fields", +} + +// Decode decodes TestInlineOneOf from json. +func (s *TestInlineOneOf) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode TestInlineOneOf to nil") + } + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "inline_discriminator": + if err := func() error { + s.InlineDiscriminator.Reset() + if err := s.InlineDiscriminator.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"inline_discriminator\"") + } + case "merge_discriminator": + if err := func() error { + s.MergeDiscriminator.Reset() + if err := s.MergeDiscriminator.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"merge_discriminator\"") + } + case "inline_unique_fields": + if err := func() error { + s.InlineUniqueFields.Reset() + if err := s.InlineUniqueFields.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"inline_unique_fields\"") + } + case "merge_unique_fields": + if err := func() error { + s.MergeUniqueFields.Reset() + if err := s.MergeUniqueFields.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"merge_unique_fields\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode TestInlineOneOf") + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *TestInlineOneOf) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *TestInlineOneOf) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + // Encode encodes TestNullableOneofsCreated as json. func (s TestNullableOneofsCreated) Encode(e *jx.Encoder) { unwrapped := OneOfWithNullable(s) diff --git a/internal/integration/sample_api_nsnc/oas_schemas_gen.go b/internal/integration/sample_api_nsnc/oas_schemas_gen.go index 3a7f2f4d7..aa148b6aa 100644 --- a/internal/integration/sample_api_nsnc/oas_schemas_gen.go +++ b/internal/integration/sample_api_nsnc/oas_schemas_gen.go @@ -663,6 +663,224 @@ func NewIntID(v int) ID { return s } +// Ref: #/components/schemas/InlineDiscriminatorOneOf +type InlineDiscriminatorOneOf struct { + Common string `json:"common"` + OneOf InlineDiscriminatorOneOfSum +} + +// GetCommon returns the value of Common. +func (s *InlineDiscriminatorOneOf) GetCommon() string { + return s.Common +} + +// GetOneOf returns the value of OneOf. +func (s *InlineDiscriminatorOneOf) GetOneOf() InlineDiscriminatorOneOfSum { + return s.OneOf +} + +// SetCommon sets the value of Common. +func (s *InlineDiscriminatorOneOf) SetCommon(val string) { + s.Common = val +} + +// SetOneOf sets the value of OneOf. +func (s *InlineDiscriminatorOneOf) SetOneOf(val InlineDiscriminatorOneOfSum) { + s.OneOf = val +} + +// InlineDiscriminatorOneOfSum represents sum type. +type InlineDiscriminatorOneOfSum struct { + Type InlineDiscriminatorOneOfSumType // switch on this field + InlineOneOfFoo InlineOneOfFoo + InlineOneOfBar InlineOneOfBar +} + +// InlineDiscriminatorOneOfSumType is oneOf type of InlineDiscriminatorOneOfSum. +type InlineDiscriminatorOneOfSumType string + +// Possible values for InlineDiscriminatorOneOfSumType. +const ( + InlineOneOfFooInlineDiscriminatorOneOfSum InlineDiscriminatorOneOfSumType = "foo" + InlineOneOfBarInlineDiscriminatorOneOfSum InlineDiscriminatorOneOfSumType = "bar" +) + +// IsInlineOneOfFoo reports whether InlineDiscriminatorOneOfSum is InlineOneOfFoo. +func (s InlineDiscriminatorOneOfSum) IsInlineOneOfFoo() bool { + return s.Type == InlineOneOfFooInlineDiscriminatorOneOfSum +} + +// IsInlineOneOfBar reports whether InlineDiscriminatorOneOfSum is InlineOneOfBar. +func (s InlineDiscriminatorOneOfSum) IsInlineOneOfBar() bool { + return s.Type == InlineOneOfBarInlineDiscriminatorOneOfSum +} + +// SetInlineOneOfFoo sets InlineDiscriminatorOneOfSum to InlineOneOfFoo. +func (s *InlineDiscriminatorOneOfSum) SetInlineOneOfFoo(v InlineOneOfFoo) { + s.Type = InlineOneOfFooInlineDiscriminatorOneOfSum + s.InlineOneOfFoo = v +} + +// GetInlineOneOfFoo returns InlineOneOfFoo and true boolean if InlineDiscriminatorOneOfSum is InlineOneOfFoo. +func (s InlineDiscriminatorOneOfSum) GetInlineOneOfFoo() (v InlineOneOfFoo, ok bool) { + if !s.IsInlineOneOfFoo() { + return v, false + } + return s.InlineOneOfFoo, true +} + +// NewInlineOneOfFooInlineDiscriminatorOneOfSum returns new InlineDiscriminatorOneOfSum from InlineOneOfFoo. +func NewInlineOneOfFooInlineDiscriminatorOneOfSum(v InlineOneOfFoo) InlineDiscriminatorOneOfSum { + var s InlineDiscriminatorOneOfSum + s.SetInlineOneOfFoo(v) + return s +} + +// SetInlineOneOfBar sets InlineDiscriminatorOneOfSum to InlineOneOfBar. +func (s *InlineDiscriminatorOneOfSum) SetInlineOneOfBar(v InlineOneOfBar) { + s.Type = InlineOneOfBarInlineDiscriminatorOneOfSum + s.InlineOneOfBar = v +} + +// GetInlineOneOfBar returns InlineOneOfBar and true boolean if InlineDiscriminatorOneOfSum is InlineOneOfBar. +func (s InlineDiscriminatorOneOfSum) GetInlineOneOfBar() (v InlineOneOfBar, ok bool) { + if !s.IsInlineOneOfBar() { + return v, false + } + return s.InlineOneOfBar, true +} + +// NewInlineOneOfBarInlineDiscriminatorOneOfSum returns new InlineDiscriminatorOneOfSum from InlineOneOfBar. +func NewInlineOneOfBarInlineDiscriminatorOneOfSum(v InlineOneOfBar) InlineDiscriminatorOneOfSum { + var s InlineDiscriminatorOneOfSum + s.SetInlineOneOfBar(v) + return s +} + +// Ref: #/components/schemas/InlineOneOfBar +type InlineOneOfBar struct { + Bar OptString `json:"bar"` +} + +// GetBar returns the value of Bar. +func (s *InlineOneOfBar) GetBar() OptString { + return s.Bar +} + +// SetBar sets the value of Bar. +func (s *InlineOneOfBar) SetBar(val OptString) { + s.Bar = val +} + +// Ref: #/components/schemas/InlineOneOfFoo +type InlineOneOfFoo struct { + Foo OptString `json:"foo"` +} + +// GetFoo returns the value of Foo. +func (s *InlineOneOfFoo) GetFoo() OptString { + return s.Foo +} + +// SetFoo sets the value of Foo. +func (s *InlineOneOfFoo) SetFoo(val OptString) { + s.Foo = val +} + +// Ref: #/components/schemas/InlineUniqueFieldsOneOf +type InlineUniqueFieldsOneOf struct { + Common string `json:"common"` + OneOf InlineUniqueFieldsOneOfSum +} + +// GetCommon returns the value of Common. +func (s *InlineUniqueFieldsOneOf) GetCommon() string { + return s.Common +} + +// GetOneOf returns the value of OneOf. +func (s *InlineUniqueFieldsOneOf) GetOneOf() InlineUniqueFieldsOneOfSum { + return s.OneOf +} + +// SetCommon sets the value of Common. +func (s *InlineUniqueFieldsOneOf) SetCommon(val string) { + s.Common = val +} + +// SetOneOf sets the value of OneOf. +func (s *InlineUniqueFieldsOneOf) SetOneOf(val InlineUniqueFieldsOneOfSum) { + s.OneOf = val +} + +// InlineUniqueFieldsOneOfSum represents sum type. +type InlineUniqueFieldsOneOfSum struct { + Type InlineUniqueFieldsOneOfSumType // switch on this field + InlineOneOfFoo InlineOneOfFoo + InlineOneOfBar InlineOneOfBar +} + +// InlineUniqueFieldsOneOfSumType is oneOf type of InlineUniqueFieldsOneOfSum. +type InlineUniqueFieldsOneOfSumType string + +// Possible values for InlineUniqueFieldsOneOfSumType. +const ( + InlineOneOfFooInlineUniqueFieldsOneOfSum InlineUniqueFieldsOneOfSumType = "InlineOneOfFoo" + InlineOneOfBarInlineUniqueFieldsOneOfSum InlineUniqueFieldsOneOfSumType = "InlineOneOfBar" +) + +// IsInlineOneOfFoo reports whether InlineUniqueFieldsOneOfSum is InlineOneOfFoo. +func (s InlineUniqueFieldsOneOfSum) IsInlineOneOfFoo() bool { + return s.Type == InlineOneOfFooInlineUniqueFieldsOneOfSum +} + +// IsInlineOneOfBar reports whether InlineUniqueFieldsOneOfSum is InlineOneOfBar. +func (s InlineUniqueFieldsOneOfSum) IsInlineOneOfBar() bool { + return s.Type == InlineOneOfBarInlineUniqueFieldsOneOfSum +} + +// SetInlineOneOfFoo sets InlineUniqueFieldsOneOfSum to InlineOneOfFoo. +func (s *InlineUniqueFieldsOneOfSum) SetInlineOneOfFoo(v InlineOneOfFoo) { + s.Type = InlineOneOfFooInlineUniqueFieldsOneOfSum + s.InlineOneOfFoo = v +} + +// GetInlineOneOfFoo returns InlineOneOfFoo and true boolean if InlineUniqueFieldsOneOfSum is InlineOneOfFoo. +func (s InlineUniqueFieldsOneOfSum) GetInlineOneOfFoo() (v InlineOneOfFoo, ok bool) { + if !s.IsInlineOneOfFoo() { + return v, false + } + return s.InlineOneOfFoo, true +} + +// NewInlineOneOfFooInlineUniqueFieldsOneOfSum returns new InlineUniqueFieldsOneOfSum from InlineOneOfFoo. +func NewInlineOneOfFooInlineUniqueFieldsOneOfSum(v InlineOneOfFoo) InlineUniqueFieldsOneOfSum { + var s InlineUniqueFieldsOneOfSum + s.SetInlineOneOfFoo(v) + return s +} + +// SetInlineOneOfBar sets InlineUniqueFieldsOneOfSum to InlineOneOfBar. +func (s *InlineUniqueFieldsOneOfSum) SetInlineOneOfBar(v InlineOneOfBar) { + s.Type = InlineOneOfBarInlineUniqueFieldsOneOfSum + s.InlineOneOfBar = v +} + +// GetInlineOneOfBar returns InlineOneOfBar and true boolean if InlineUniqueFieldsOneOfSum is InlineOneOfBar. +func (s InlineUniqueFieldsOneOfSum) GetInlineOneOfBar() (v InlineOneOfBar, ok bool) { + if !s.IsInlineOneOfBar() { + return v, false + } + return s.InlineOneOfBar, true +} + +// NewInlineOneOfBarInlineUniqueFieldsOneOfSum returns new InlineUniqueFieldsOneOfSum from InlineOneOfBar. +func NewInlineOneOfBarInlineUniqueFieldsOneOfSum(v InlineOneOfBar) InlineUniqueFieldsOneOfSum { + var s InlineUniqueFieldsOneOfSum + s.SetInlineOneOfBar(v) + return s +} + // Ref: #/components/schemas/Issue143 // Issue143 represents sum type. type Issue143 struct { @@ -1212,6 +1430,196 @@ func (s *MaxPropertiesTest) SetOptionalC(val OptInt) { s.OptionalC = val } +// Merged schema. +// Ref: #/components/schemas/MergeDiscriminatorOneOf +type MergeDiscriminatorOneOf struct { + Common string `json:"common"` + OneOf MergeDiscriminatorOneOfSum +} + +// GetCommon returns the value of Common. +func (s *MergeDiscriminatorOneOf) GetCommon() string { + return s.Common +} + +// GetOneOf returns the value of OneOf. +func (s *MergeDiscriminatorOneOf) GetOneOf() MergeDiscriminatorOneOfSum { + return s.OneOf +} + +// SetCommon sets the value of Common. +func (s *MergeDiscriminatorOneOf) SetCommon(val string) { + s.Common = val +} + +// SetOneOf sets the value of OneOf. +func (s *MergeDiscriminatorOneOf) SetOneOf(val MergeDiscriminatorOneOfSum) { + s.OneOf = val +} + +// MergeDiscriminatorOneOfSum represents sum type. +type MergeDiscriminatorOneOfSum struct { + Type MergeDiscriminatorOneOfSumType // switch on this field + InlineOneOfFoo InlineOneOfFoo + InlineOneOfBar InlineOneOfBar +} + +// MergeDiscriminatorOneOfSumType is oneOf type of MergeDiscriminatorOneOfSum. +type MergeDiscriminatorOneOfSumType string + +// Possible values for MergeDiscriminatorOneOfSumType. +const ( + InlineOneOfFooMergeDiscriminatorOneOfSum MergeDiscriminatorOneOfSumType = "foo" + InlineOneOfBarMergeDiscriminatorOneOfSum MergeDiscriminatorOneOfSumType = "bar" +) + +// IsInlineOneOfFoo reports whether MergeDiscriminatorOneOfSum is InlineOneOfFoo. +func (s MergeDiscriminatorOneOfSum) IsInlineOneOfFoo() bool { + return s.Type == InlineOneOfFooMergeDiscriminatorOneOfSum +} + +// IsInlineOneOfBar reports whether MergeDiscriminatorOneOfSum is InlineOneOfBar. +func (s MergeDiscriminatorOneOfSum) IsInlineOneOfBar() bool { + return s.Type == InlineOneOfBarMergeDiscriminatorOneOfSum +} + +// SetInlineOneOfFoo sets MergeDiscriminatorOneOfSum to InlineOneOfFoo. +func (s *MergeDiscriminatorOneOfSum) SetInlineOneOfFoo(v InlineOneOfFoo) { + s.Type = InlineOneOfFooMergeDiscriminatorOneOfSum + s.InlineOneOfFoo = v +} + +// GetInlineOneOfFoo returns InlineOneOfFoo and true boolean if MergeDiscriminatorOneOfSum is InlineOneOfFoo. +func (s MergeDiscriminatorOneOfSum) GetInlineOneOfFoo() (v InlineOneOfFoo, ok bool) { + if !s.IsInlineOneOfFoo() { + return v, false + } + return s.InlineOneOfFoo, true +} + +// NewInlineOneOfFooMergeDiscriminatorOneOfSum returns new MergeDiscriminatorOneOfSum from InlineOneOfFoo. +func NewInlineOneOfFooMergeDiscriminatorOneOfSum(v InlineOneOfFoo) MergeDiscriminatorOneOfSum { + var s MergeDiscriminatorOneOfSum + s.SetInlineOneOfFoo(v) + return s +} + +// SetInlineOneOfBar sets MergeDiscriminatorOneOfSum to InlineOneOfBar. +func (s *MergeDiscriminatorOneOfSum) SetInlineOneOfBar(v InlineOneOfBar) { + s.Type = InlineOneOfBarMergeDiscriminatorOneOfSum + s.InlineOneOfBar = v +} + +// GetInlineOneOfBar returns InlineOneOfBar and true boolean if MergeDiscriminatorOneOfSum is InlineOneOfBar. +func (s MergeDiscriminatorOneOfSum) GetInlineOneOfBar() (v InlineOneOfBar, ok bool) { + if !s.IsInlineOneOfBar() { + return v, false + } + return s.InlineOneOfBar, true +} + +// NewInlineOneOfBarMergeDiscriminatorOneOfSum returns new MergeDiscriminatorOneOfSum from InlineOneOfBar. +func NewInlineOneOfBarMergeDiscriminatorOneOfSum(v InlineOneOfBar) MergeDiscriminatorOneOfSum { + var s MergeDiscriminatorOneOfSum + s.SetInlineOneOfBar(v) + return s +} + +// Merged schema. +// Ref: #/components/schemas/MergeUniqueFieldsOneOf +type MergeUniqueFieldsOneOf struct { + Common string `json:"common"` + OneOf MergeUniqueFieldsOneOfSum +} + +// GetCommon returns the value of Common. +func (s *MergeUniqueFieldsOneOf) GetCommon() string { + return s.Common +} + +// GetOneOf returns the value of OneOf. +func (s *MergeUniqueFieldsOneOf) GetOneOf() MergeUniqueFieldsOneOfSum { + return s.OneOf +} + +// SetCommon sets the value of Common. +func (s *MergeUniqueFieldsOneOf) SetCommon(val string) { + s.Common = val +} + +// SetOneOf sets the value of OneOf. +func (s *MergeUniqueFieldsOneOf) SetOneOf(val MergeUniqueFieldsOneOfSum) { + s.OneOf = val +} + +// MergeUniqueFieldsOneOfSum represents sum type. +type MergeUniqueFieldsOneOfSum struct { + Type MergeUniqueFieldsOneOfSumType // switch on this field + InlineOneOfFoo InlineOneOfFoo + InlineOneOfBar InlineOneOfBar +} + +// MergeUniqueFieldsOneOfSumType is oneOf type of MergeUniqueFieldsOneOfSum. +type MergeUniqueFieldsOneOfSumType string + +// Possible values for MergeUniqueFieldsOneOfSumType. +const ( + InlineOneOfFooMergeUniqueFieldsOneOfSum MergeUniqueFieldsOneOfSumType = "InlineOneOfFoo" + InlineOneOfBarMergeUniqueFieldsOneOfSum MergeUniqueFieldsOneOfSumType = "InlineOneOfBar" +) + +// IsInlineOneOfFoo reports whether MergeUniqueFieldsOneOfSum is InlineOneOfFoo. +func (s MergeUniqueFieldsOneOfSum) IsInlineOneOfFoo() bool { + return s.Type == InlineOneOfFooMergeUniqueFieldsOneOfSum +} + +// IsInlineOneOfBar reports whether MergeUniqueFieldsOneOfSum is InlineOneOfBar. +func (s MergeUniqueFieldsOneOfSum) IsInlineOneOfBar() bool { + return s.Type == InlineOneOfBarMergeUniqueFieldsOneOfSum +} + +// SetInlineOneOfFoo sets MergeUniqueFieldsOneOfSum to InlineOneOfFoo. +func (s *MergeUniqueFieldsOneOfSum) SetInlineOneOfFoo(v InlineOneOfFoo) { + s.Type = InlineOneOfFooMergeUniqueFieldsOneOfSum + s.InlineOneOfFoo = v +} + +// GetInlineOneOfFoo returns InlineOneOfFoo and true boolean if MergeUniqueFieldsOneOfSum is InlineOneOfFoo. +func (s MergeUniqueFieldsOneOfSum) GetInlineOneOfFoo() (v InlineOneOfFoo, ok bool) { + if !s.IsInlineOneOfFoo() { + return v, false + } + return s.InlineOneOfFoo, true +} + +// NewInlineOneOfFooMergeUniqueFieldsOneOfSum returns new MergeUniqueFieldsOneOfSum from InlineOneOfFoo. +func NewInlineOneOfFooMergeUniqueFieldsOneOfSum(v InlineOneOfFoo) MergeUniqueFieldsOneOfSum { + var s MergeUniqueFieldsOneOfSum + s.SetInlineOneOfFoo(v) + return s +} + +// SetInlineOneOfBar sets MergeUniqueFieldsOneOfSum to InlineOneOfBar. +func (s *MergeUniqueFieldsOneOfSum) SetInlineOneOfBar(v InlineOneOfBar) { + s.Type = InlineOneOfBarMergeUniqueFieldsOneOfSum + s.InlineOneOfBar = v +} + +// GetInlineOneOfBar returns InlineOneOfBar and true boolean if MergeUniqueFieldsOneOfSum is InlineOneOfBar. +func (s MergeUniqueFieldsOneOfSum) GetInlineOneOfBar() (v InlineOneOfBar, ok bool) { + if !s.IsInlineOneOfBar() { + return v, false + } + return s.InlineOneOfBar, true +} + +// NewInlineOneOfBarMergeUniqueFieldsOneOfSum returns new MergeUniqueFieldsOneOfSum from InlineOneOfBar. +func NewInlineOneOfBarMergeUniqueFieldsOneOfSum(v InlineOneOfBar) MergeUniqueFieldsOneOfSum { + var s MergeUniqueFieldsOneOfSum + s.SetInlineOneOfBar(v) + return s +} + // NewNilInt returns new NilInt with value set to v. func NewNilInt(v int) NilInt { return NilInt{ @@ -2995,6 +3403,98 @@ func (o OptIPv6) Or(d netip.Addr) netip.Addr { return d } +// NewOptInlineDiscriminatorOneOf returns new OptInlineDiscriminatorOneOf with value set to v. +func NewOptInlineDiscriminatorOneOf(v InlineDiscriminatorOneOf) OptInlineDiscriminatorOneOf { + return OptInlineDiscriminatorOneOf{ + Value: v, + Set: true, + } +} + +// OptInlineDiscriminatorOneOf is optional InlineDiscriminatorOneOf. +type OptInlineDiscriminatorOneOf struct { + Value InlineDiscriminatorOneOf + Set bool +} + +// IsSet returns true if OptInlineDiscriminatorOneOf was set. +func (o OptInlineDiscriminatorOneOf) IsSet() bool { return o.Set } + +// Reset unsets value. +func (o *OptInlineDiscriminatorOneOf) Reset() { + var v InlineDiscriminatorOneOf + o.Value = v + o.Set = false +} + +// SetTo sets value to v. +func (o *OptInlineDiscriminatorOneOf) SetTo(v InlineDiscriminatorOneOf) { + o.Set = true + o.Value = v +} + +// Get returns value and boolean that denotes whether value was set. +func (o OptInlineDiscriminatorOneOf) Get() (v InlineDiscriminatorOneOf, ok bool) { + if !o.Set { + return v, false + } + return o.Value, true +} + +// Or returns value if set, or given parameter if does not. +func (o OptInlineDiscriminatorOneOf) Or(d InlineDiscriminatorOneOf) InlineDiscriminatorOneOf { + if v, ok := o.Get(); ok { + return v + } + return d +} + +// NewOptInlineUniqueFieldsOneOf returns new OptInlineUniqueFieldsOneOf with value set to v. +func NewOptInlineUniqueFieldsOneOf(v InlineUniqueFieldsOneOf) OptInlineUniqueFieldsOneOf { + return OptInlineUniqueFieldsOneOf{ + Value: v, + Set: true, + } +} + +// OptInlineUniqueFieldsOneOf is optional InlineUniqueFieldsOneOf. +type OptInlineUniqueFieldsOneOf struct { + Value InlineUniqueFieldsOneOf + Set bool +} + +// IsSet returns true if OptInlineUniqueFieldsOneOf was set. +func (o OptInlineUniqueFieldsOneOf) IsSet() bool { return o.Set } + +// Reset unsets value. +func (o *OptInlineUniqueFieldsOneOf) Reset() { + var v InlineUniqueFieldsOneOf + o.Value = v + o.Set = false +} + +// SetTo sets value to v. +func (o *OptInlineUniqueFieldsOneOf) SetTo(v InlineUniqueFieldsOneOf) { + o.Set = true + o.Value = v +} + +// Get returns value and boolean that denotes whether value was set. +func (o OptInlineUniqueFieldsOneOf) Get() (v InlineUniqueFieldsOneOf, ok bool) { + if !o.Set { + return v, false + } + return o.Value, true +} + +// Or returns value if set, or given parameter if does not. +func (o OptInlineUniqueFieldsOneOf) Or(d InlineUniqueFieldsOneOf) InlineUniqueFieldsOneOf { + if v, ok := o.Get(); ok { + return v + } + return d +} + // NewOptInt returns new OptInt with value set to v. func NewOptInt(v int) OptInt { return OptInt{ @@ -3271,6 +3771,98 @@ func (o OptMaxPropertiesTest) Or(d MaxPropertiesTest) MaxPropertiesTest { return d } +// NewOptMergeDiscriminatorOneOf returns new OptMergeDiscriminatorOneOf with value set to v. +func NewOptMergeDiscriminatorOneOf(v MergeDiscriminatorOneOf) OptMergeDiscriminatorOneOf { + return OptMergeDiscriminatorOneOf{ + Value: v, + Set: true, + } +} + +// OptMergeDiscriminatorOneOf is optional MergeDiscriminatorOneOf. +type OptMergeDiscriminatorOneOf struct { + Value MergeDiscriminatorOneOf + Set bool +} + +// IsSet returns true if OptMergeDiscriminatorOneOf was set. +func (o OptMergeDiscriminatorOneOf) IsSet() bool { return o.Set } + +// Reset unsets value. +func (o *OptMergeDiscriminatorOneOf) Reset() { + var v MergeDiscriminatorOneOf + o.Value = v + o.Set = false +} + +// SetTo sets value to v. +func (o *OptMergeDiscriminatorOneOf) SetTo(v MergeDiscriminatorOneOf) { + o.Set = true + o.Value = v +} + +// Get returns value and boolean that denotes whether value was set. +func (o OptMergeDiscriminatorOneOf) Get() (v MergeDiscriminatorOneOf, ok bool) { + if !o.Set { + return v, false + } + return o.Value, true +} + +// Or returns value if set, or given parameter if does not. +func (o OptMergeDiscriminatorOneOf) Or(d MergeDiscriminatorOneOf) MergeDiscriminatorOneOf { + if v, ok := o.Get(); ok { + return v + } + return d +} + +// NewOptMergeUniqueFieldsOneOf returns new OptMergeUniqueFieldsOneOf with value set to v. +func NewOptMergeUniqueFieldsOneOf(v MergeUniqueFieldsOneOf) OptMergeUniqueFieldsOneOf { + return OptMergeUniqueFieldsOneOf{ + Value: v, + Set: true, + } +} + +// OptMergeUniqueFieldsOneOf is optional MergeUniqueFieldsOneOf. +type OptMergeUniqueFieldsOneOf struct { + Value MergeUniqueFieldsOneOf + Set bool +} + +// IsSet returns true if OptMergeUniqueFieldsOneOf was set. +func (o OptMergeUniqueFieldsOneOf) IsSet() bool { return o.Set } + +// Reset unsets value. +func (o *OptMergeUniqueFieldsOneOf) Reset() { + var v MergeUniqueFieldsOneOf + o.Value = v + o.Set = false +} + +// SetTo sets value to v. +func (o *OptMergeUniqueFieldsOneOf) SetTo(v MergeUniqueFieldsOneOf) { + o.Set = true + o.Value = v +} + +// Get returns value and boolean that denotes whether value was set. +func (o OptMergeUniqueFieldsOneOf) Get() (v MergeUniqueFieldsOneOf, ok bool) { + if !o.Set { + return v, false + } + return o.Value, true +} + +// Or returns value if set, or given parameter if does not. +func (o OptMergeUniqueFieldsOneOf) Or(d MergeUniqueFieldsOneOf) MergeUniqueFieldsOneOf { + if v, ok := o.Get(); ok { + return v + } + return d +} + // NewOptNilString returns new OptNilString with value set to v. func NewOptNilString(v string) OptNilString { return OptNilString{ @@ -4827,6 +5419,54 @@ func (s *TestFloatValidation) SetMultipleOf(val float64) { // TestFloatValidationOK is response for TestFloatValidation operation. type TestFloatValidationOK struct{} +// Ref: #/components/schemas/TestInlineOneOf +type TestInlineOneOf struct { + InlineDiscriminator OptInlineDiscriminatorOneOf `json:"inline_discriminator"` + MergeDiscriminator OptMergeDiscriminatorOneOf `json:"merge_discriminator"` + InlineUniqueFields OptInlineUniqueFieldsOneOf `json:"inline_unique_fields"` + MergeUniqueFields OptMergeUniqueFieldsOneOf `json:"merge_unique_fields"` +} + +// GetInlineDiscriminator returns the value of InlineDiscriminator. +func (s *TestInlineOneOf) GetInlineDiscriminator() OptInlineDiscriminatorOneOf { + return s.InlineDiscriminator +} + +// GetMergeDiscriminator returns the value of MergeDiscriminator. +func (s *TestInlineOneOf) GetMergeDiscriminator() OptMergeDiscriminatorOneOf { + return s.MergeDiscriminator +} + +// GetInlineUniqueFields returns the value of InlineUniqueFields. +func (s *TestInlineOneOf) GetInlineUniqueFields() OptInlineUniqueFieldsOneOf { + return s.InlineUniqueFields +} + +// GetMergeUniqueFields returns the value of MergeUniqueFields. +func (s *TestInlineOneOf) GetMergeUniqueFields() OptMergeUniqueFieldsOneOf { + return s.MergeUniqueFields +} + +// SetInlineDiscriminator sets the value of InlineDiscriminator. +func (s *TestInlineOneOf) SetInlineDiscriminator(val OptInlineDiscriminatorOneOf) { + s.InlineDiscriminator = val +} + +// SetMergeDiscriminator sets the value of MergeDiscriminator. +func (s *TestInlineOneOf) SetMergeDiscriminator(val OptMergeDiscriminatorOneOf) { + s.MergeDiscriminator = val +} + +// SetInlineUniqueFields sets the value of InlineUniqueFields. +func (s *TestInlineOneOf) SetInlineUniqueFields(val OptInlineUniqueFieldsOneOf) { + s.InlineUniqueFields = val +} + +// SetMergeUniqueFields sets the value of MergeUniqueFields. +func (s *TestInlineOneOf) SetMergeUniqueFields(val OptMergeUniqueFieldsOneOf) { + s.MergeUniqueFields = val +} + type TestNullableOneofsCreated OneOfWithNullable func (*TestNullableOneofsCreated) testNullableOneofsRes() {}