-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add NullableFromType
option
#110
Conversation
Reflector.NullableFromType
optionNullableFromType
option
Closes #14027 Blocked by: * cloudquery/codegen#39 * invopop/jsonschema#109 – merged to `cloudquery/jsonschema@cqmain` * invopop/jsonschema#110 – merged to `cloudquery/jsonschema@cqmain` I propose reviewing the annotations along with tests, as the JSON schemas generated are just too long to grasp visually.
@samlown I hope you're doing well in these uneasy days. I've implemented the changes in #109 & #110 & tested them via a fork at The results of using the updated code along with Could you please give an estimate when these PRs (#109 & #110) could be merged? It'll be far easier to pin an upstream version instead of maintaining a fork for these 2 changes. |
Closes cloudquery#14027 Blocked by: * cloudquery/codegen#39 * invopop/jsonschema#109 – merged to `cloudquery/jsonschema@cqmain` * invopop/jsonschema#110 – merged to `cloudquery/jsonschema@cqmain` I propose reviewing the annotations along with tests, as the JSON schemas generated are just too long to grasp visually.
Highly interested in this PR as well. Is there anything I can help with to get this merged? |
@kop I really hope @samlown has time to review/merge that. |
1db8e8d
to
0fe1690
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay there! This looks good to me, thanks! I do think there should be some extra tests for those non-pointer reflection types though.
@samlown thanks for the review! I have an additional question here: currently the library uses {
"oneOf": [
{
"oneOf": [
{},
{
"type": "null"
}
]
},
{
"type": "null"
}
]
} If we use I can raise this as a separate issue & address it in a follow-up PR. |
79bb90b
to
8a38808
Compare
Hi! Echoing interest in this PR -- happy to help if there's anything outstanding to get it in. |
@samlown would you be able to take another look when you have a chance? |
Hi @samlown! Could you please advice if you need any assistance in maintenance of this library? I see that the last commit here was 4 months ago, & I wonder whether this library is actually actively maintained or not. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay, I really don't have much time at the moment. There are quite a few style things here that I'd be worried about, especially regarding public methods which are not tested (just make them private!)
// This will be performed if the schema isn't already nullable (as `oneOf` is used). | ||
// | ||
// {"oneOf":[s,{"type":"null"}]} | ||
func (t *Schema) MakeNullable() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These methods are inconsistent with the rest of the package, I don't think they should be in the schema.go file, nor should they be exposed publicly. Any public public method should be tested.
Also, Make
in Go is typically reserved for constructors that return a new instance of a struct.
To avoid the reflect file getting too big, my suggestion would be to create a reflect_nullable.go
file with these as private methods defined there.
if r.NullableFromType { | ||
nullable = isNullable(f.Type.Kind()) | ||
} else { | ||
nullable = nullableFromJSONSchemaTags(schemaTags) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels like this logic should be inside its own method.
property.structKeywordsFromTags(f, st, name) | ||
if property.Description == "" { | ||
property.Description = r.lookupComment(t, f.Name) | ||
unwrapped := property |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the objective here? I can't see where unwrapped
is consumed, making this section much harder to read as I'd guess everything is happening on the pointer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/invopop/jsonschema/pull/110/files#diff-672d1dd2b9193c8818108f625c29ff25feaea4c1c50cc36cfda18108cba3e1b2R540
We need this to work with pointer fields as well
@samlown I'm closing this PR for 2 reasons:
If you'll be available for review & merging please tag me here & I'll reopen from my own fork. |
Instead of #106.
Allows to define property nullability based on the field type instead of the
jsonschema:"nullable"
tag.Useful when the types included are referenced from 3rd party packages.
Consider the following example:
The following JSON is a perfectly valid value for Go:
The schemas produced for the
TestNullableField
struct are:Old behavior (`NullableFromType:false`)
New behavior (`NullableFromType:true`)
In both cases
a
is required, but the old behavior will fail for{"a":null}
whereas the new one will succeed.