github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/generator/templates/serializers/additionalpropertiesserializer.gotmpl (about) 1 {{ define "additionalPropertiesSerializer" }} 2 // UnmarshalJSON unmarshals this object with additional properties from JSON 3 func ({{.ReceiverName}} *{{ pascalize .Name }}) UnmarshalJSON(data []byte) error { 4 // stage 1, bind the properties 5 var stage1 {{ template "withoutAdditionalBody" . }} 6 if err := json.Unmarshal(data, &stage1); err != nil { 7 return err 8 } 9 var rcv {{ pascalize .Name }} 10 {{ range .Properties }} 11 rcv.{{ pascalize .Name }} = stage1.{{ pascalize .Name }} 12 {{- end }} 13 *{{ .ReceiverName }} = rcv 14 15 // stage 2, remove properties and add to map 16 stage2 := make(map[string]{{ if .AdditionalProperties }}json.RawMessage{{ else }}interface{}{{ end }}) 17 if err := json.Unmarshal(data, &stage2); err != nil { 18 return err 19 } 20 21 {{ range .Properties }} 22 delete(stage2, {{ printf "%q" .Name }}) 23 {{- end }} 24 25 {{- if .AdditionalProperties }} 26 // stage 3, add additional properties values 27 if len(stage2) > 0 { 28 result := make(map[string]{{ template "schemaType" .AdditionalProperties }}) 29 for k, v := range stage2 { 30 var toadd {{ template "schemaType" .AdditionalProperties }} 31 if err := json.Unmarshal(v, {{if not .AdditionalProperties.IsNullable }}&{{ end }}toadd); err != nil { 32 return err 33 } 34 result[k] = toadd 35 } 36 {{ .ValueExpression }} = result 37 } 38 {{- else }} 39 {{ .ValueExpression }} = stage2 40 {{- end }} 41 42 return nil 43 } 44 45 // MarshalJSON marshals this object with additional properties into a JSON object 46 func ({{.ReceiverName}} {{ pascalize .Name }}) MarshalJSON() ([]byte, error) { 47 var stage1 {{ template "withoutAdditionalBody" . }} 48 {{ range .Properties }} 49 stage1.{{ pascalize .Name }} = {{ .ValueExpression }} 50 {{- end }} 51 52 // make JSON object for known properties 53 props, err := json.Marshal(stage1) 54 if err != nil { 55 return nil, err 56 } 57 58 if len({{ .ValueExpression }}) == 0 { // no additional properties 59 return props, nil 60 } 61 62 // make JSON object for the additional properties 63 additional, err := json.Marshal({{ .ValueExpression }}) 64 if err != nil { 65 return nil, err 66 } 67 68 if len(props) < 3 { // "{}": only additional properties 69 return additional, nil 70 } 71 72 // concatenate the 2 objects 73 return swag.ConcatJSON(props, additional), nil 74 } 75 {{- end }} 76 77 {{ define "noAdditionalPropertiesSerializer" }} 78 // UnmarshalJSON unmarshals this object while disallowing additional properties from JSON 79 func ({{.ReceiverName}} *{{ pascalize .Name }}) UnmarshalJSON(data []byte) error { 80 var props {{ template "withoutAdditionalBody" . }} 81 82 dec := json.NewDecoder(bytes.NewReader(data)) 83 dec.DisallowUnknownFields() 84 if err := dec.Decode(&props); err != nil { 85 return err 86 } 87 88 {{- $rcv := .ReceiverName }} 89 {{ range .Properties }} 90 {{ .ReceiverName }}.{{ pascalize .Name }} = props.{{ pascalize .Name }} 91 {{- end }} 92 return nil 93 } 94 {{- end }}