Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/quicktype-core/src/language/Golang/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
6 changes: 4 additions & 2 deletions test/inputs/json/priority/omit-empty.out.omit-empty.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"results": [
{
"age": 52
"age": 52,
"name": null
},
{
"age": 27
"age": 27,
"name": null
}
]
}
4 changes: 4 additions & 0 deletions test/inputs/schema/nullable-optional-one-of.1.fail.enum.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"kind": "three",
"b": null
}
4 changes: 4 additions & 0 deletions test/inputs/schema/nullable-optional-one-of.1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"kind": "one",
"b": null
}
42 changes: 42 additions & 0 deletions test/inputs/schema/nullable-optional-one-of.schema
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
6 changes: 4 additions & 2 deletions test/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
];

Expand Down Expand Up @@ -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"],
};
Expand Down
Loading