diff --git a/examples/ex_github/oas_json_gen.go b/examples/ex_github/oas_json_gen.go index c05327b37..c617fc31e 100644 --- a/examples/ex_github/oas_json_gen.go +++ b/examples/ex_github/oas_json_gen.go @@ -152811,12 +152811,17 @@ func (s *SearchReposOK) UnmarshalJSON(data []byte) error { // Encode encodes SearchResultTextMatches as json. func (s SearchResultTextMatches) Encode(e *jx.Encoder) { unwrapped := []SearchResultTextMatchesItem(s) - - e.ArrStart() - for _, elem := range unwrapped { - elem.Encode(e) + if unwrapped == nil { + e.ArrEmpty() + return + } + if unwrapped != nil { + e.ArrStart() + for _, elem := range unwrapped { + elem.Encode(e) + } + e.ArrEnd() } - e.ArrEnd() } // Decode decodes SearchResultTextMatches from json. diff --git a/examples/ex_github/oas_validators_gen.go b/examples/ex_github/oas_validators_gen.go index 8320c5a61..cecd8ec99 100644 --- a/examples/ex_github/oas_validators_gen.go +++ b/examples/ex_github/oas_validators_gen.go @@ -4540,17 +4540,6 @@ func (s *CodeSearchResultItem) Validate() error { Error: err, }) } - if err := func() error { - if err := s.TextMatches.Validate(); err != nil { - return err - } - return nil - }(); err != nil { - failures = append(failures, validate.FieldError{ - Name: "text_matches", - Error: err, - }) - } if len(failures) > 0 { return &validate.Error{Fields: failures} } @@ -4821,17 +4810,6 @@ func (s *CommitSearchResultItem) Validate() error { Error: err, }) } - if err := func() error { - if err := s.TextMatches.Validate(); err != nil { - return err - } - return nil - }(); err != nil { - failures = append(failures, validate.FieldError{ - Name: "text_matches", - Error: err, - }) - } if len(failures) > 0 { return &validate.Error{Fields: failures} } @@ -7833,17 +7811,6 @@ func (s *IssueSearchResultItem) Validate() error { Error: err, }) } - if err := func() error { - if err := s.TextMatches.Validate(); err != nil { - return err - } - return nil - }(); err != nil { - failures = append(failures, validate.FieldError{ - Name: "text_matches", - Error: err, - }) - } if err := func() error { if err := (validate.Float{}).Validate(float64(s.Score)); err != nil { return errors.Wrap(err, "float") @@ -8942,17 +8909,6 @@ func (s *LabelSearchResultItem) Validate() error { Error: err, }) } - if err := func() error { - if err := s.TextMatches.Validate(); err != nil { - return err - } - return nil - }(); err != nil { - failures = append(failures, validate.FieldError{ - Name: "text_matches", - Error: err, - }) - } if len(failures) > 0 { return &validate.Error{Fields: failures} } @@ -15024,17 +14980,6 @@ func (s *RepoSearchResultItem) Validate() error { Error: err, }) } - if err := func() error { - if err := s.TextMatches.Validate(); err != nil { - return err - } - return nil - }(); err != nil { - failures = append(failures, validate.FieldError{ - Name: "text_matches", - Error: err, - }) - } if len(failures) > 0 { return &validate.Error{Fields: failures} } @@ -18338,14 +18283,6 @@ func (s SearchReposSort) Validate() error { } } -func (s SearchResultTextMatches) Validate() error { - alias := ([]SearchResultTextMatchesItem)(s) - if alias == nil { - return errors.New("nil is invalid value") - } - return nil -} - func (s *SearchTopicsOK) Validate() error { if s == nil { return validate.ErrNilPointer @@ -19821,17 +19758,6 @@ func (s *TopicSearchResultItem) Validate() error { Error: err, }) } - if err := func() error { - if err := s.TextMatches.Validate(); err != nil { - return err - } - return nil - }(); err != nil { - failures = append(failures, validate.FieldError{ - Name: "text_matches", - Error: err, - }) - } if err := func() error { if value, ok := s.Related.Get(); ok { if err := func() error { @@ -19951,17 +19877,6 @@ func (s *UserSearchResultItem) Validate() error { Error: err, }) } - if err := func() error { - if err := s.TextMatches.Validate(); err != nil { - return err - } - return nil - }(); err != nil { - failures = append(failures, validate.FieldError{ - Name: "text_matches", - Error: err, - }) - } if len(failures) > 0 { return &validate.Error{Fields: failures} } diff --git a/examples/ex_openapi/output.gen.go b/examples/ex_openapi/output.gen.go index 5d4f206f3..4ee2020ac 100644 --- a/examples/ex_openapi/output.gen.go +++ b/examples/ex_openapi/output.gen.go @@ -7245,14 +7245,19 @@ func (s *Encodings) UnmarshalJSON(data []byte) error { // Encode encodes Enum as json. func (s Enum) Encode(e *jx.Encoder) { unwrapped := []jx.Raw(s) - - e.ArrStart() - for _, elem := range unwrapped { - if len(elem) != 0 { - e.Raw(elem) + if unwrapped == nil { + e.ArrEmpty() + return + } + if unwrapped != nil { + e.ArrStart() + for _, elem := range unwrapped { + if len(elem) != 0 { + e.Raw(elem) + } } + e.ArrEnd() } - e.ArrEnd() } // Decode decodes Enum from json. @@ -17240,12 +17245,17 @@ func (s *SpecificationExtension) UnmarshalJSON(data []byte) error { // Encode encodes StringArray as json. func (s StringArray) Encode(e *jx.Encoder) { unwrapped := []string(s) - - e.ArrStart() - for _, elem := range unwrapped { - e.Str(elem) + if unwrapped == nil { + e.ArrEmpty() + return + } + if unwrapped != nil { + e.ArrStart() + for _, elem := range unwrapped { + e.Str(elem) + } + e.ArrEnd() } - e.ArrEnd() } // Decode decodes StringArray from json. @@ -18115,9 +18125,6 @@ func (s Encodings) Validate() error { func (s Enum) Validate() error { alias := ([]jx.Raw)(s) - if alias == nil { - return errors.New("nil is invalid value") - } if err := (validate.Array{ MinLength: 1, MinLengthSet: true, @@ -19594,9 +19601,6 @@ func (s *Spec) Validate() error { func (s StringArray) Validate() error { alias := ([]string)(s) - if alias == nil { - return errors.New("nil is invalid value") - } if err := (validate.Array{ MinLength: 1, MinLengthSet: true, diff --git a/gen/_template/json/encoders_alias.tmpl b/gen/_template/json/encoders_alias.tmpl index ad4d8e35b..d65d30676 100644 --- a/gen/_template/json/encoders_alias.tmpl +++ b/gen/_template/json/encoders_alias.tmpl @@ -1,13 +1,20 @@ {{- /*gotype: github.com/ogen-go/ogen/gen/ir.Type*/ -}} {{- define "json/encoders_alias" }} +{{- $a := $.AliasTo }} // Encode encodes {{ $.Name }} as json. func (s {{ $.ReadOnlyReceiver }}) Encode(e *jx.Encoder) { - {{- if $.AliasTo.DoPassByPointer }} - unwrapped := (*{{ $.AliasTo.Go }})(s) + {{- if $a.DoPassByPointer }} + unwrapped := (*{{ $a.Go }})(s) {{- else }} - unwrapped := {{ $.AliasTo.Go }}(s) + unwrapped := {{ $a.Go }}(s) {{- end }} - {{ template "json/enc" elem $.AliasTo "unwrapped" -}} + {{- if and $a.IsArray $a.NilSemantic.Optional }}{{/* Prevents empty value encoding */}} + if unwrapped == nil { + e.ArrEmpty() + return + } + {{- end }} + {{ template "json/enc" elem $a "unwrapped" -}} } // Decode decodes {{ $.Name }} from json. @@ -15,9 +22,9 @@ func (s *{{ $.Name }}) Decode(d *jx.Decoder) error { if s == nil { return errors.New({{ printf "invalid: unable to decode %s to nil" $.Name | quote }}) } - var unwrapped {{ $.AliasTo.Go }} + var unwrapped {{ $a.Go }} if err := func() error { - {{- template "json/dec" elem $.AliasTo "unwrapped" }} + {{- template "json/dec" elem $a "unwrapped" }} return nil }(); err != nil { return errors.Wrap(err, "alias") diff --git a/gen/generics.go b/gen/generics.go index 48f6e6f4e..cb5b11648 100644 --- a/gen/generics.go +++ b/gen/generics.go @@ -64,6 +64,7 @@ func boxType(t *ir.Type, v ir.GenericVariant) (*ir.Type, error) { if dealiased.IsAlias() { dealiased = dealiased.AliasTo } + // Do not wrap if // * type is Any // * type is Stream @@ -93,6 +94,7 @@ func boxType(t *ir.Type, v ir.GenericVariant) (*ir.Type, error) { } return ir.Generic(postfix, t, v), nil } + dealiased.NilSemantic = t.NilSemantic return t, nil }