Skip to content

Commit

Permalink
feat: refine additional properties default values, to make them bette…
Browse files Browse the repository at this point in the history
…r match their value type in generated code
  • Loading branch information
omissis committed Jul 11, 2023
1 parent 65a7b01 commit 9967ee6
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
38 changes: 37 additions & 1 deletion pkg/generator/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
30 changes: 30 additions & 0 deletions tests/data/core/objectAdditionalProperties.go.output
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 14 additions & 0 deletions tests/data/core/objectAdditionalProperties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"foo": {
"type": "object",
"additionalProperties": {
"type": "string"
},
"default": {}
}
},
"additionalProperties": false
}

0 comments on commit 9967ee6

Please sign in to comment.