diff --git a/pkg/generator/generate.go b/pkg/generator/generate.go index af84ac3c..0aa6cdc5 100644 --- a/pkg/generator/generate.go +++ b/pkg/generator/generate.go @@ -833,7 +833,7 @@ func (g *schemaGenerator) generateStructType( switch { case prop.Default != nil: - structField.DefaultValue = prop.Default + structField.DefaultValue = g.defaultPropertyValue(prop) default: if isRequired { @@ -849,6 +849,42 @@ func (g *schemaGenerator) generateStructType( return &structType, nil } +func (g *schemaGenerator) defaultPropertyValue(prop *schemas.Type) any { + if prop.AdditionalProperties != nil { + if len(prop.AdditionalProperties.Type) == 0 { + return map[string]any{} + } + + if len(prop.AdditionalProperties.Type) != 1 { + g.warner("Additional property has multiple types; will be represented as an empty interface with no validation") + + return map[string]any{} + } + + switch prop.AdditionalProperties.Type[0] { + case schemas.TypeNameString: + return map[string]string{} + + case schemas.TypeNameArray: + return map[string][]any{} + + case schemas.TypeNameNumber: + return map[string]float64{} + + case schemas.TypeNameInteger: + return map[string]int{} + + case schemas.TypeNameBoolean: + return map[string]bool{} + + default: + return map[string]any{} + } + } + + return prop.Default +} + func (g *schemaGenerator) generateTypeInline( t *schemas.Type, scope nameScope, diff --git a/tests/data/core/objectAdditionalProperties.go.output b/tests/data/core/objectAdditionalProperties.go.output new file mode 100644 index 00000000..358312c2 --- /dev/null +++ b/tests/data/core/objectAdditionalProperties.go.output @@ -0,0 +1,30 @@ +// Code generated by github.com/atombender/go-jsonschema, DO NOT EDIT. + +package test + +import "encoding/json" + +type ObjectAdditionalProperties struct { + // Foo corresponds to the JSON schema field "foo". + Foo ObjectAdditionalPropertiesFoo `json:"foo,omitempty" yaml:"foo,omitempty" mapstructure:"foo,omitempty"` +} + +type ObjectAdditionalPropertiesFoo map[string]string + +// UnmarshalJSON implements json.Unmarshaler. +func (j *ObjectAdditionalProperties) UnmarshalJSON(b []byte) error { + var raw map[string]interface{} + if err := json.Unmarshal(b, &raw); err != nil { + return err + } + type Plain ObjectAdditionalProperties + var plain Plain + if err := json.Unmarshal(b, &plain); err != nil { + return err + } + if v, ok := raw["foo"]; !ok || v == nil { + plain.Foo = map[string]string{} + } + *j = ObjectAdditionalProperties(plain) + return nil +} diff --git a/tests/data/core/objectAdditionalProperties.json b/tests/data/core/objectAdditionalProperties.json new file mode 100644 index 00000000..9935ffad --- /dev/null +++ b/tests/data/core/objectAdditionalProperties.json @@ -0,0 +1,14 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "properties": { + "foo": { + "type": "object", + "additionalProperties": { + "type": "string" + }, + "default": {} + } + }, + "additionalProperties": false +}