feat: Add NullableFromJSONSchemaTags option#106
feat: Add NullableFromJSONSchemaTags option#106candiduslynx wants to merge 9 commits intoinvopop:mainfrom
NullableFromJSONSchemaTags option#106Conversation
|
@samlown this changes the default behavior to be closer to default The other option could be make the default behavior to look for tags & only explicitly use field type for nullability (when some option is set). |
|
@candiduslynx thanks for this, but I'm trying to understand what exact problem you're trying to solve with this PR. For me, the nullable type is implied when there is no value, I'm really not sure why you'd want to define it explicitly. |
|
@samlown consider json Consider the example added in 26eed64: type TestNullableField struct {
A *string `json:"a"`
}Old behavior (`NullableFromJSONSchemaTags:true`)
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/invopop/jsonschema/test-nullable-field",
"$ref": "#/$defs/TestNullableField",
"$defs": {
"TestNullableField": {
"properties": {
"a": {
"type": "string"
}
},
"additionalProperties": false,
"type": "object",
"required": [
"a"
]
}
}
}New behavior (`NullableFromJSONSchemaTags:false`)
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/invopop/jsonschema/test-nullable-field",
"$ref": "#/$defs/TestNullableField",
"$defs": {
"TestNullableField": {
"properties": {
"a": {
"oneOf": [
{
"type": "string"
},
{
"type": "null"
}
]
}
},
"additionalProperties": false,
"type": "object",
"required": [
"a"
]
}
}
}In both cases |
c3d819f to
26eed64
Compare
|
I'm going to reopen a fresh version that preserves default behavior + add README.md doc for this |
|
Replaced by #110 |
This changes default behavior to look for the
jsonschema:"nullable"tag to mark field optional onlyReflector.NullableFromJSONSchemaTagsis set totrue.In Go it's perfectly fine to unmarshal the following types from
null:This PR also includes the changes from #107, so it might be better to merge that one first.