Repository: ogen-go/ogen
Pull request #197 (2022-02-15 19:36:37)

fix(ir): set default even if it is zero


Patch:
diff --git a/examples/ex_firecracker/oas_defaults_gen.go b/examples/ex_firecracker/oas_defaults_gen.go
index 6e72b974..aa8ba1c3 100644
--- a/examples/ex_firecracker/oas_defaults_gen.go
+++ b/examples/ex_firecracker/oas_defaults_gen.go
@@ -82,6 +82,16 @@ func (s *Logger) setDefaults() {
 
 		s.Level.SetTo(val)
 	}
+	{
+		val := bool(false)
+
+		s.ShowLevel.SetTo(val)
+	}
+	{
+		val := bool(false)
+
+		s.ShowLogOrigin.SetTo(val)
+	}
 }
 
 // setDefaults set default value of fields.
diff --git a/examples/ex_github/oas_defaults_gen.go b/examples/ex_github/oas_defaults_gen.go
index 38fdaf5c..e6298ef0 100644
--- a/examples/ex_github/oas_defaults_gen.go
+++ b/examples/ex_github/oas_defaults_gen.go
@@ -75,6 +75,15 @@ func (s *ActionsCreateSelfHostedRunnerGroupForOrgReq) setDefaults() {
 	}
 }
 
+// setDefaults set default value of fields.
+func (s *ActivitySetThreadSubscriptionReq) setDefaults() {
+	{
+		val := bool(false)
+
+		s.Ignored.SetTo(val)
+	}
+}
+
 // setDefaults set default value of fields.
 func (s *ChecksSetSuitesPreferencesReqAutoTriggerChecksItem) setDefaults() {
 	{
@@ -84,6 +93,35 @@ func (s *ChecksSetSuitesPreferencesReqAutoTriggerChecksItem) setDefaults() {
 	}
 }
 
+// setDefaults set default value of fields.
+func (s *DeploymentStatus) setDefaults() {
+	{
+		val := string("")
+
+		s.Description = val
+	}
+	{
+		val := string("")
+
+		s.Environment.SetTo(val)
+	}
+	{
+		val, _ := json.DecodeURI(jx.DecodeStr("\"\""))
+
+		s.TargetURL = val
+	}
+	{
+		val, _ := json.DecodeURI(jx.DecodeStr("\"\""))
+
+		s.EnvironmentURL.SetTo(val)
+	}
+	{
+		val, _ := json.DecodeURI(jx.DecodeStr("\"\""))
+
+		s.LogURL.SetTo(val)
+	}
+}
+
 // setDefaults set default value of fields.
 func (s *EnterpriseAdminUpdateSelfHostedRunnerGroupForEnterpriseReq) setDefaults() {
 	{
@@ -111,6 +149,15 @@ func (s *GitCreateBlobReq) setDefaults() {
 	}
 }
 
+// setDefaults set default value of fields.
+func (s *GitUpdateRefReq) setDefaults() {
+	{
+		val := bool(false)
+
+		s.Force.SetTo(val)
+	}
+}
+
 // setDefaults set default value of fields.
 func (s *IssuesCreateMilestoneReq) setDefaults() {
 	{
@@ -129,6 +176,30 @@ func (s *IssuesUpdateMilestoneReq) setDefaults() {
 	}
 }
 
+// setDefaults set default value of fields.
+func (s *MigrationsStartForOrgReq) setDefaults() {
+	{
+		val := bool(false)
+
+		s.LockRepositories.SetTo(val)
+	}
+	{
+		val := bool(false)
+
+		s.ExcludeAttachments.SetTo(val)
+	}
+	{
+		val := bool(false)
+
+		s.ExcludeReleases.SetTo(val)
+	}
+	{
+		val := bool(false)
+
+		s.ExcludeOwnerProjects.SetTo(val)
+	}
+}
+
 // setDefaults set default value of fields.
 func (s *Milestone) setDefaults() {
 	{
@@ -149,6 +220,16 @@ func (s *NullableMilestone) setDefaults() {
 
 // setDefaults set default value of fields.
 func (s *NullableRepository) setDefaults() {
+	{
+		val := bool(false)
+
+		s.Private = val
+	}
+	{
+		val := bool(false)
+
+		s.IsTemplate.SetTo(val)
+	}
 	{
 		val := bool(true)
 
@@ -169,6 +250,11 @@ func (s *NullableRepository) setDefaults() {
 
 		s.HasDownloads = val
 	}
+	{
+		val := bool(false)
+
+		s.Archived = val
+	}
 	{
 		val := string("public")
 
@@ -184,6 +270,16 @@ func (s *NullableRepository) setDefaults() {
 
 		s.AllowSquashMerge.SetTo(val)
 	}
+	{
+		val := bool(false)
+
+		s.AllowAutoMerge.SetTo(val)
+	}
+	{
+		val := bool(false)
+
+		s.DeleteBranchOnMerge.SetTo(val)
+	}
 	{
 		val := bool(true)
 
@@ -243,6 +339,15 @@ func (s *OrgsUpdateWebhookReq) setDefaults() {
 	}
 }
 
+// setDefaults set default value of fields.
+func (s *Page) setDefaults() {
+	{
+		val := bool(false)
+
+		s.Custom404 = val
+	}
+}
+
 // setDefaults set default value of fields.
 func (s *ProjectsAddCollaboratorReq) setDefaults() {
 	{
@@ -301,10 +406,49 @@ func (s *ReposCreateDeploymentReq) setDefaults() {
 
 		s.Environment.SetTo(val)
 	}
+	{
+		val := string("")
+
+		s.Description.SetTo(val)
+	}
+	{
+		val := bool(false)
+
+		s.TransientEnvironment.SetTo(val)
+	}
+}
+
+// setDefaults set default value of fields.
+func (s *ReposCreateDeploymentStatusReq) setDefaults() {
+	{
+		val := string("")
+
+		s.TargetURL.SetTo(val)
+	}
+	{
+		val := string("")
+
+		s.LogURL.SetTo(val)
+	}
+	{
+		val := string("")
+
+		s.Description.SetTo(val)
+	}
+	{
+		val := string("")
+
+		s.EnvironmentURL.SetTo(val)
+	}
 }
 
 // setDefaults set default value of fields.
 func (s *ReposCreateForAuthenticatedUserReq) setDefaults() {
+	{
+		val := bool(false)
+
+		s.Private.SetTo(val)
+	}
 	{
 		val := bool(true)
 
@@ -320,6 +464,11 @@ func (s *ReposCreateForAuthenticatedUserReq) setDefaults() {
 
 		s.HasWiki.SetTo(val)
 	}
+	{
+		val := bool(false)
+
+		s.AutoInit.SetTo(val)
+	}
 	{
 		val := bool(true)
 
@@ -335,15 +484,35 @@ func (s *ReposCreateForAuthenticatedUserReq) setDefaults() {
 
 		s.AllowRebaseMerge.SetTo(val)
 	}
+	{
+		val := bool(false)
+
+		s.AllowAutoMerge.SetTo(val)
+	}
+	{
+		val := bool(false)
+
+		s.DeleteBranchOnMerge.SetTo(val)
+	}
 	{
 		val := bool(true)
 
 		s.HasDownloads.SetTo(val)
 	}
+	{
+		val := bool(false)
+
+		s.IsTemplate.SetTo(val)
+	}
 }
 
 // setDefaults set default value of fields.
 func (s *ReposCreateInOrgReq) setDefaults() {
+	{
+		val := bool(false)
+
+		s.Private.SetTo(val)
+	}
 	{
 		val := bool(true)
 
@@ -359,6 +528,16 @@ func (s *ReposCreateInOrgReq) setDefaults() {
 
 		s.HasWiki.SetTo(val)
 	}
+	{
+		val := bool(false)
+
+		s.IsTemplate.SetTo(val)
+	}
+	{
+		val := bool(false)
+
+		s.AutoInit.SetTo(val)
+	}
 	{
 		val := bool(true)
 
@@ -374,6 +553,16 @@ func (s *ReposCreateInOrgReq) setDefaults() {
 
 		s.AllowRebaseMerge.SetTo(val)
 	}
+	{
+		val := bool(false)
+
+		s.AllowAutoMerge.SetTo(val)
+	}
+	{
+		val := bool(false)
+
+		s.DeleteBranchOnMerge.SetTo(val)
+	}
 }
 
 // setDefaults set default value of fields.
@@ -385,6 +574,34 @@ func (s *ReposCreatePagesSiteReqSource) setDefaults() {
 	}
 }
 
+// setDefaults set default value of fields.
+func (s *ReposCreateReleaseReq) setDefaults() {
+	{
+		val := bool(false)
+
+		s.Draft.SetTo(val)
+	}
+	{
+		val := bool(false)
+
+		s.Prerelease.SetTo(val)
+	}
+}
+
+// setDefaults set default value of fields.
+func (s *ReposCreateUsingTemplateReq) setDefaults() {
+	{
+		val := bool(false)
+
+		s.IncludeAllBranches.SetTo(val)
+	}
+	{
+		val := bool(false)
+
+		s.Private.SetTo(val)
+	}
+}
+
 // setDefaults set default value of fields.
 func (s *ReposCreateWebhookReq) setDefaults() {
 	{
@@ -404,6 +621,11 @@ func (s *ReposCreateWebhookReq) setDefaults() {
 
 // setDefaults set default value of fields.
 func (s *ReposUpdateReq) setDefaults() {
+	{
+		val := bool(false)
+
+		s.Private.SetTo(val)
+	}
 	{
 		val := bool(true)
 
@@ -419,6 +641,11 @@ func (s *ReposUpdateReq) setDefaults() {
 
 		s.HasWiki.SetTo(val)
 	}
+	{
+		val := bool(false)
+
+		s.IsTemplate.SetTo(val)
+	}
 	{
 		val := bool(true)
 
@@ -434,6 +661,26 @@ func (s *ReposUpdateReq) setDefaults() {
 
 		s.AllowRebaseMerge.SetTo(val)
 	}
+	{
+		val := bool(false)
+
+		s.AllowAutoMerge.SetTo(val)
+	}
+	{
+		val := bool(false)
+
+		s.DeleteBranchOnMerge.SetTo(val)
+	}
+	{
+		val := bool(false)
+
+		s.Archived.SetTo(val)
+	}
+	{
+		val := bool(false)
+
+		s.AllowForking.SetTo(val)
+	}
 }
 
 // setDefaults set default value of fields.
@@ -455,6 +702,16 @@ func (s *ReposUpdateWebhookReq) setDefaults() {
 
 // setDefaults set default value of fields.
 func (s *Repository) setDefaults() {
+	{
+		val := bool(false)
+
+		s.Private = val
+	}
+	{
+		val := bool(false)
+
+		s.IsTemplate.SetTo(val)
+	}
 	{
 		val := bool(true)
 
@@ -475,6 +732,11 @@ func (s *Repository) setDefaults() {
 
 		s.HasDownloads = val
 	}
+	{
+		val := bool(false)
+
+		s.Archived = val
+	}
 	{
 		val := string("public")
 
@@ -490,6 +752,16 @@ func (s *Repository) setDefaults() {
 
 		s.AllowSquashMerge.SetTo(val)
 	}
+	{
+		val := bool(false)
+
+		s.AllowAutoMerge.SetTo(val)
+	}
+	{
+		val := bool(false)
+
+		s.DeleteBranchOnMerge.SetTo(val)
+	}
 	{
 		val := bool(true)
 
@@ -522,6 +794,16 @@ func (s *TeamMembership) setDefaults() {
 
 // setDefaults set default value of fields.
 func (s *TeamRepository) setDefaults() {
+	{
+		val := bool(false)
+
+		s.Private = val
+	}
+	{
+		val := bool(false)
+
+		s.IsTemplate.SetTo(val)
+	}
 	{
 		val := bool(true)
 
@@ -542,6 +824,11 @@ func (s *TeamRepository) setDefaults() {
 
 		s.HasDownloads = val
 	}
+	{
+		val := bool(false)
+
+		s.Archived = val
+	}
 	{
 		val := string("public")
 
@@ -557,11 +844,26 @@ func (s *TeamRepository) setDefaults() {
 
 		s.AllowSquashMerge.SetTo(val)
 	}
+	{
+		val := bool(false)
+
+		s.AllowAutoMerge.SetTo(val)
+	}
+	{
+		val := bool(false)
+
+		s.DeleteBranchOnMerge.SetTo(val)
+	}
 	{
 		val := bool(true)
 
 		s.AllowMergeCommit.SetTo(val)
 	}
+	{
+		val := bool(false)
+
+		s.AllowForking.SetTo(val)
+	}
 }
 
 // setDefaults set default value of fields.
@@ -582,6 +884,24 @@ func (s *TeamsAddOrUpdateMembershipForUserLegacyReq) setDefaults() {
 	}
 }
 
+// setDefaults set default value of fields.
+func (s *TeamsCreateDiscussionInOrgReq) setDefaults() {
+	{
+		val := bool(false)
+
+		s.Private.SetTo(val)
+	}
+}
+
+// setDefaults set default value of fields.
+func (s *TeamsCreateDiscussionLegacyReq) setDefaults() {
+	{
+		val := bool(false)
+
+		s.Private.SetTo(val)
+	}
+}
+
 // setDefaults set default value of fields.
 func (s *TeamsCreateReq) setDefaults() {
 	{
diff --git a/examples/ex_github/oas_json_gen.go b/examples/ex_github/oas_json_gen.go
index 11a0e21e..b4fd93ce 100644
--- a/examples/ex_github/oas_json_gen.go
+++ b/examples/ex_github/oas_json_gen.go
@@ -6504,6 +6504,7 @@ func (s *ActivitySetThreadSubscriptionReq) Decode(d *jx.Decoder) error {
 	if s == nil {
 		return errors.New("invalid: unable to decode ActivitySetThreadSubscriptionReq to nil")
 	}
+	s.setDefaults()
 
 	if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
 		switch string(k) {
@@ -32261,6 +32262,7 @@ func (s *DeploymentStatus) Decode(d *jx.Decoder) error {
 		return errors.New("invalid: unable to decode DeploymentStatus to nil")
 	}
 	var requiredBitSet [2]uint8
+	s.setDefaults()
 
 	if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
 		switch string(k) {
@@ -49107,6 +49109,7 @@ func (s *GitUpdateRefReq) Decode(d *jx.Decoder) error {
 		return errors.New("invalid: unable to decode GitUpdateRefReq to nil")
 	}
 	var requiredBitSet [1]uint8
+	s.setDefaults()
 
 	if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
 		switch string(k) {
@@ -67762,6 +67765,7 @@ func (s *MigrationsStartForOrgReq) Decode(d *jx.Decoder) error {
 		return errors.New("invalid: unable to decode MigrationsStartForOrgReq to nil")
 	}
 	var requiredBitSet [1]uint8
+	s.setDefaults()
 
 	if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
 		switch string(k) {
@@ -99495,6 +99499,7 @@ func (s *Page) Decode(d *jx.Decoder) error {
 		return errors.New("invalid: unable to decode Page to nil")
 	}
 	var requiredBitSet [2]uint8
+	s.setDefaults()
 
 	if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
 		switch string(k) {
@@ -131996,6 +132001,7 @@ func (s *ReposCreateDeploymentStatusReq) Decode(d *jx.Decoder) error {
 		return errors.New("invalid: unable to decode ReposCreateDeploymentStatusReq to nil")
 	}
 	var requiredBitSet [1]uint8
+	s.setDefaults()
 
 	if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
 		switch string(k) {
@@ -134308,6 +134314,7 @@ func (s *ReposCreateReleaseReq) Decode(d *jx.Decoder) error {
 		return errors.New("invalid: unable to decode ReposCreateReleaseReq to nil")
 	}
 	var requiredBitSet [1]uint8
+	s.setDefaults()
 
 	if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
 		switch string(k) {
@@ -134498,6 +134505,7 @@ func (s *ReposCreateUsingTemplateReq) Decode(d *jx.Decoder) error {
 		return errors.New("invalid: unable to decode ReposCreateUsingTemplateReq to nil")
 	}
 	var requiredBitSet [1]uint8
+	s.setDefaults()
 
 	if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
 		switch string(k) {
@@ -161675,6 +161683,7 @@ func (s *TeamsCreateDiscussionInOrgReq) Decode(d *jx.Decoder) error {
 		return errors.New("invalid: unable to decode TeamsCreateDiscussionInOrgReq to nil")
 	}
 	var requiredBitSet [1]uint8
+	s.setDefaults()
 
 	if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
 		switch string(k) {
@@ -161801,6 +161810,7 @@ func (s *TeamsCreateDiscussionLegacyReq) Decode(d *jx.Decoder) error {
 		return errors.New("invalid: unable to decode TeamsCreateDiscussionLegacyReq to nil")
 	}
 	var requiredBitSet [1]uint8
+	s.setDefaults()
 
 	if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
 		switch string(k) {
diff --git a/examples/ex_gotd/oas_defaults_gen.go b/examples/ex_gotd/oas_defaults_gen.go
index 054666bf..883858dc 100644
--- a/examples/ex_gotd/oas_defaults_gen.go
+++ b/examples/ex_gotd/oas_defaults_gen.go
@@ -129,6 +129,33 @@ func (s *BotCommandScopeDefault) setDefaults() {
 	}
 }
 
+// setDefaults set default value of fields.
+func (s *Error) setDefaults() {
+	{
+		val := bool(false)
+
+		s.Ok = val
+	}
+}
+
+// setDefaults set default value of fields.
+func (s *GetUpdates) setDefaults() {
+	{
+		val := int(0)
+
+		s.Offset.SetTo(val)
+	}
+}
+
+// setDefaults set default value of fields.
+func (s *GetUserProfilePhotos) setDefaults() {
+	{
+		val := int(0)
+
+		s.Offset.SetTo(val)
+	}
+}
+
 // setDefaults set default value of fields.
 func (s *InlineQueryResultArticle) setDefaults() {
 	{
@@ -354,6 +381,15 @@ func (s *InputMediaVideo) setDefaults() {
 	}
 }
 
+// setDefaults set default value of fields.
+func (s *MessageEntity) setDefaults() {
+	{
+		val := int(0)
+
+		s.Offset = val
+	}
+}
+
 // setDefaults set default value of fields.
 func (s *PassportElementErrorDataField) setDefaults() {
 	{
diff --git a/examples/ex_gotd/oas_json_gen.go b/examples/ex_gotd/oas_json_gen.go
index 00823263..0307feb9 100644
--- a/examples/ex_gotd/oas_json_gen.go
+++ b/examples/ex_gotd/oas_json_gen.go
@@ -9387,6 +9387,7 @@ func (s *Error) Decode(d *jx.Decoder) error {
 		return errors.New("invalid: unable to decode Error to nil")
 	}
 	var requiredBitSet [1]uint8
+	s.setDefaults()
 
 	if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
 		switch string(k) {
@@ -11183,6 +11184,7 @@ func (s *GetUpdates) Decode(d *jx.Decoder) error {
 	if s == nil {
 		return errors.New("invalid: unable to decode GetUpdates to nil")
 	}
+	s.setDefaults()
 
 	if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
 		switch string(k) {
@@ -11295,6 +11297,7 @@ func (s *GetUserProfilePhotos) Decode(d *jx.Decoder) error {
 		return errors.New("invalid: unable to decode GetUserProfilePhotos to nil")
 	}
 	var requiredBitSet [1]uint8
+	s.setDefaults()
 
 	if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
 		switch string(k) {
@@ -23448,6 +23451,7 @@ func (s *MessageEntity) Decode(d *jx.Decoder) error {
 		return errors.New("invalid: unable to decode MessageEntity to nil")
 	}
 	var requiredBitSet [1]uint8
+	s.setDefaults()
 
 	if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
 		switch string(k) {
diff --git a/examples/ex_telegram/oas_defaults_gen.go b/examples/ex_telegram/oas_defaults_gen.go
index ae575895..00d2e811 100644
--- a/examples/ex_telegram/oas_defaults_gen.go
+++ b/examples/ex_telegram/oas_defaults_gen.go
@@ -129,6 +129,33 @@ func (s *BotCommandScopeDefault) setDefaults() {
 	}
 }
 
+// setDefaults set default value of fields.
+func (s *Error) setDefaults() {
+	{
+		val := bool(false)
+
+		s.Ok = val
+	}
+}
+
+// setDefaults set default value of fields.
+func (s *GetUpdates) setDefaults() {
+	{
+		val := int(0)
+
+		s.Offset.SetTo(val)
+	}
+}
+
+// setDefaults set default value of fields.
+func (s *GetUserProfilePhotos) setDefaults() {
+	{
+		val := int(0)
+
+		s.Offset.SetTo(val)
+	}
+}
+
 // setDefaults set default value of fields.
 func (s *InlineQueryResultArticle) setDefaults() {
 	{
@@ -354,6 +381,15 @@ func (s *InputMediaVideo) setDefaults() {
 	}
 }
 
+// setDefaults set default value of fields.
+func (s *MessageEntity) setDefaults() {
+	{
+		val := int(0)
+
+		s.Offset = val
+	}
+}
+
 // setDefaults set default value of fields.
 func (s *PassportElementErrorDataField) setDefaults() {
 	{
diff --git a/examples/ex_telegram/oas_json_gen.go b/examples/ex_telegram/oas_json_gen.go
index 15b20ee2..242975a2 100644
--- a/examples/ex_telegram/oas_json_gen.go
+++ b/examples/ex_telegram/oas_json_gen.go
@@ -9387,6 +9387,7 @@ func (s *Error) Decode(d *jx.Decoder) error {
 		return errors.New("invalid: unable to decode Error to nil")
 	}
 	var requiredBitSet [1]uint8
+	s.setDefaults()
 
 	if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
 		switch string(k) {
@@ -11163,6 +11164,7 @@ func (s *GetUpdates) Decode(d *jx.Decoder) error {
 	if s == nil {
 		return errors.New("invalid: unable to decode GetUpdates to nil")
 	}
+	s.setDefaults()
 
 	if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
 		switch string(k) {
@@ -11275,6 +11277,7 @@ func (s *GetUserProfilePhotos) Decode(d *jx.Decoder) error {
 		return errors.New("invalid: unable to decode GetUserProfilePhotos to nil")
 	}
 	var requiredBitSet [1]uint8
+	s.setDefaults()
 
 	if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
 		switch string(k) {
@@ -23368,6 +23371,7 @@ func (s *MessageEntity) Decode(d *jx.Decoder) error {
 		return errors.New("invalid: unable to decode MessageEntity to nil")
 	}
 	var requiredBitSet [1]uint8
+	s.setDefaults()
 
 	if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
 		switch string(k) {
diff --git a/internal/ir/default.go b/internal/ir/default.go
index 802ce5e3..74ce3e6e 100644
--- a/internal/ir/default.go
+++ b/internal/ir/default.go
@@ -8,6 +8,9 @@ type Default struct {
 
 // IsNil whether value is set, but null.
 func (d Default) IsNil() bool {
+	if !d.Set {
+		return false
+	}
 	switch d.Value.(type) {
 	case nil:
 		return true
diff --git a/internal/ir/field.go b/internal/ir/field.go
index 37c68067..392567e7 100644
--- a/internal/ir/field.go
+++ b/internal/ir/field.go
@@ -1,7 +1,6 @@
 package ir
 
 import (
-	"reflect"
 	"strconv"
 
 	"github.com/ogen-go/ogen/jsonschema"
@@ -63,9 +62,7 @@ func (f Field) GoDoc() []string {
 func (t Type) DefaultFields() (r []*Field) {
 	for _, f := range t.Fields {
 		if val := f.Default(); val.Set {
-			if val.Value == nil || !reflect.ValueOf(val.Value).IsZero() {
-				r = append(r, f)
-			}
+			r = append(r, f)
 		}
 	}
 	return r
@@ -74,7 +71,7 @@ func (t Type) DefaultFields() (r []*Field) {
 // HasDefaultFields whether type has fields with default values.
 func (t Type) HasDefaultFields() bool {
 	for _, f := range t.Fields {
-		if val := f.Default(); val.Set && !reflect.ValueOf(val.Value).IsZero() {
+		if val := f.Default(); val.Set {
 			return true
 		}
 	}
diff --git a/jsonschema/parser.go b/jsonschema/parser.go
index 3fa6f5fb..d5e9cf13 100644
--- a/jsonschema/parser.go
+++ b/jsonschema/parser.go
@@ -55,6 +55,9 @@ func (p *Parser) parse1(schema *RawSchema, ctx resolveCtx, hook func(*Schema) *S
 			if err != nil {
 				return nil, errors.Wrap(err, "parse default")
 			}
+			if v == nil && !s.Nullable {
+				return nil, errors.New("unexpected default \"null\" value")
+			}
 			s.Default = v
 			s.DefaultSet = true
 		}
diff --git a/jsonschema/parser_enum.go b/jsonschema/parser_enum.go
index e7a7b134..3b84fcf2 100644
--- a/jsonschema/parser_enum.go
+++ b/jsonschema/parser_enum.go
@@ -49,6 +49,8 @@ func parseJSONValue(root *Schema, v json.RawMessage) (interface{}, error) {
 	parse = func(s *Schema, d *jx.Decoder) (interface{}, error) {
 		next := d.Next()
 		if next == jx.Null {
+			// We do not check nullability here because enum with null value is completely valid
+			// even if it is not nullable.
 			return nil, nil
 		}
 		switch typ := s.Type; typ {


Test patch:
diff --git a/_testdata/negative/null_default_value.json b/_testdata/negative/null_default_value.json
new file mode 100644
index 00000000..ede2e900
--- /dev/null
+++ b/_testdata/negative/null_default_value.json
@@ -0,0 +1,72 @@
+{
+  "openapi": "3.0.3",
+  "info": {
+    "title": "title",
+    "version": "v0.1.0"
+  },
+  "paths": {
+    "/healthz": {
+      "get": {
+        "operationId": "probeLiveness",
+        "description": "Liveness probe for kubernetes",
+        "responses": {
+          "200": {
+            "description": "User info",
+            "content": {
+              "application/json": {
+                "schema": {
+                  "type": "object",
+                  "required": [
+                    "not_nullable"
+                  ],
+                  "properties": {
+                    "not_nullable": {
+                      "type": "integer",
+                      "default": null
+                    }
+                  }
+                }
+              }
+            }
+          },
+          "default": {
+            "$ref": "#/components/responses/Error"
+          }
+        }
+      }
+    }
+  },
+  "components": {
+    "schemas": {
+      "Error": {
+        "description": "Represents error object",
+        "type": "object",
+        "properties": {
+          "code": {
+            "type": "integer",
+            "format": "int64"
+          },
+          "message": {
+            "type": "string"
+          }
+        },
+        "required": [
+          "code",
+          "message"
+        ]
+      }
+    },
+    "responses": {
+      "Error": {
+        "description": "General Error",
+        "content": {
+          "application/json": {
+            "schema": {
+              "$ref": "#/components/schemas/Error"
+            }
+          }
+        }
+      }
+    }
+  }
+}