quicktype
                                
                                
                                
                                    quicktype copied to clipboard
                            
                            
                            
                        Optional fields incorrectly appear in Go output
When using a JSON schema to generate output for Go, the omitempty tag is not applied to embedded objects that are not required by the schema. This results in the optional fields being rendered as "key":null instead of being omitted from the output which causes issues with pedantic JSON parsers.
Example JSON schema:
{
  "id": "http://json-schema.org/geo",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "basic_required": {
        "type": "string"
    },
    "basic_not_required": {
        "type": "string"
    },
    "required": {
        "oneOf": [ { "type": "string" }, { "type": "number" } ]
    },
    "not_required": {
        "oneOf": [ { "type": "string" }, { "type": "number" } ]
    }
  },
  "required": ["basic_required", "required"]
}
quicktype generated types:
type Test struct {
	BasicNotRequired *string   `json:"basic_not_required,omitempty"`
	BasicRequired    string    `json:"basic_required"`              
	NotRequired      *Required `json:"not_required"`                
	Required         *Required `json:"required"`                    
}
type Required struct {
	Double *float64
	String *string
}
Minimal Reproduction code:
package main
import (
	"encoding/json"
	"fmt"
)
type Test struct {
	BasicNotRequired *string   `json:"basic_not_required,omitempty"`
	BasicRequired    string    `json:"basic_required"`
	NotRequired      *Required `json:"not_required"`
	Required         *Required `json:"required"`
}
type Required struct {
	Double *float64
	String *string
}
func main() {
	t := Test{}
	b, _ := json.MarshalIndent(t, "", "  ")
	fmt.Println(string(b))
}
Expected output:
{
  "basic_required": "",
  "required": null
}
Actual Output:
{
  "basic_required": "",
  "not_required": null,
  "required": null
}
Playground using full code generation and generated Marshal():
https://go.dev/play/p/abGx9rplr03
Also being bitten by this.
We have a required field that has type string or null in an object in a group of oneOf.
It's missing the omitempty tag in the generated struct.
Resolved as of #2449