diff --git a/packages/quicktype-core/src/language/Golang/utils.ts b/packages/quicktype-core/src/language/Golang/utils.ts index 6ba526ed2..6c202655f 100644 --- a/packages/quicktype-core/src/language/Golang/utils.ts +++ b/packages/quicktype-core/src/language/Golang/utils.ts @@ -51,7 +51,8 @@ export function canOmitEmpty( omitEmptyOption: boolean, ): boolean { if (!cp.isOptional) return false; - if (omitEmptyOption) return true; + if (omitEmptyOption) + return !["union", "null", "any"].includes(cp.type.kind); const t = cp.type; return !["union", "null", "any"].includes(t.kind); } diff --git a/test/inputs/json/priority/omit-empty.out.omit-empty.json b/test/inputs/json/priority/omit-empty.out.omit-empty.json index 809e3cfa0..87924cedf 100644 --- a/test/inputs/json/priority/omit-empty.out.omit-empty.json +++ b/test/inputs/json/priority/omit-empty.out.omit-empty.json @@ -1,10 +1,12 @@ { "results": [ { - "age": 52 + "age": 52, + "name": null }, { - "age": 27 + "age": 27, + "name": null } ] } diff --git a/test/inputs/schema/nullable-optional-one-of.1.fail.enum.json b/test/inputs/schema/nullable-optional-one-of.1.fail.enum.json new file mode 100644 index 000000000..fd710347d --- /dev/null +++ b/test/inputs/schema/nullable-optional-one-of.1.fail.enum.json @@ -0,0 +1,4 @@ +{ + "kind": "three", + "b": null +} diff --git a/test/inputs/schema/nullable-optional-one-of.1.json b/test/inputs/schema/nullable-optional-one-of.1.json new file mode 100644 index 000000000..29aeb7de2 --- /dev/null +++ b/test/inputs/schema/nullable-optional-one-of.1.json @@ -0,0 +1,4 @@ +{ + "kind": "one", + "b": null +} diff --git a/test/inputs/schema/nullable-optional-one-of.schema b/test/inputs/schema/nullable-optional-one-of.schema new file mode 100644 index 000000000..1b0759ac3 --- /dev/null +++ b/test/inputs/schema/nullable-optional-one-of.schema @@ -0,0 +1,42 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "bar": { + "type": ["string", "null"] + }, + "one": { + "type": "object", + "properties": { + "kind": { + "type": "string", + "const": "one" + }, + "b": { + "$ref": "#/definitions/bar" + } + }, + "required": ["kind", "b"] + }, + "two": { + "type": "object", + "properties": { + "kind": { + "type": "string", + "const": "two" + }, + "b": { + "$ref": "#/definitions/bar" + } + }, + "required": ["kind"] + } + }, + "oneOf": [ + { + "$ref": "#/definitions/one" + }, + { + "$ref": "#/definitions/two" + } + ] +} diff --git a/test/languages.ts b/test/languages.ts index ecbe724ee..4e2cf4208 100644 --- a/test/languages.ts +++ b/test/languages.ts @@ -25,6 +25,7 @@ const skipsEnumValueValidation = [ "enum-large.schema", "optional-enum.schema", "const-non-string.schema", + "nullable-optional-one-of.schema", "all-of-additional-properties-false.schema", ]; @@ -551,9 +552,10 @@ export const GoLanguage: Language = { rendererOptions: {}, quickTestRendererOptions: [ // Runs against the expected-output file - // `omit-empty.out.omit-empty.json`, which asserts that `omitempty` - // actually drops the null field. + // `omit-empty.out.omit-empty.json`, which asserts that nullable + // fields preserve null instead of being omitted. ["omit-empty.json", { "omit-empty": "true" }], + ["nullable-optional-one-of.schema", { "omit-empty": "true" }], ], sourceFiles: ["src/language/Golang/index.ts"], };