-
Notifications
You must be signed in to change notification settings - Fork 210
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
Feature: Added support for OpenAPI validations marker comments #435
Conversation
|
Welcome @Schnides123! |
/ok-to-test |
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.
Really like the approach. needs a lot more testing, though.
When we reach a more mature PR state we should open a k/k PR with these changes to show the diff with the generated OpenAPI
We can merge this next release after code thaw
@alexzielenski: GitHub didn't allow me to request PR reviews from the following users: WDYT, is, preferable. Note that only kubernetes members and repo collaborators can review this PR, and authors cannot review their own PRs. In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
I would still add an intergration test. Create a new directory for value validation in https://github.com/kubernetes/kube-openapi/tree/master/test/integration/testdata, add some types in there with various validation markers, carefully follow the README including the notes at the bottom. Thanks! |
I have been told to add the prefix back in, so the prefix is now back in |
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.
Looking really good. Few more tests before I think it is ready to go
pkg/generators/openapi.go
Outdated
"Format:$.type|raw${}.OpenAPISchemaFormat(),\n"+ | ||
"},\n"+ | ||
"Format:$.type|raw${}.OpenAPISchemaFormat(),\n", args) | ||
g.generateValueValidations(&overrides.SchemaProps) |
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.
I see the following new calls to generateValueValidations
- Custom V2 Type/Format and V3 Definition Functions
- Custom V2 Type/Format and V3 oneOf types
- Custom V2 Type/Format (and No V3 Definition or OneOf)
- V3 OneOf Types
- Regular Behavior (no custom types or override functions implemented)
The checked boxes are those that an integration exists already. This is function is pretty complicated. I wonder if we can simplify this function in a future PR. In the meantime, I'd like to see the rest of the cases covered in the integration tests to make sure the output compiles in those cases.
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.
resolved
pkg/generators/markers.go
Outdated
newKey := strings.TrimPrefix(key, prefix) | ||
|
||
// Skip ref markers | ||
if len(value) == 1 && regexp.MustCompile(`^(?:\s*ref\s{0,1}\().*\)\s*`).MatchString(value[0]) { |
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.
Is this possible try "k8s.io/gengo/examples/defaulter-gen/generators".ParseSymbolReference
against the value to see if it is a ref instead? To avoid duplciating this logic
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.
resolved
pkg/generators/markers.go
Outdated
@@ -59,6 +139,11 @@ func ParseCommentTags(comments []string, prefix string) (CommentTags, error) { | |||
return CommentTags{}, fmt.Errorf("failed to unmarshal marker comments: %w", err) | |||
} | |||
|
|||
// Validate the parsed comment tags | |||
if err := commentTags.Validate(*t); err != nil { |
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.
I think id prefer to not validate within the parse function. Id expect the caller to call validate on the return value rather than to make this function take a type
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.
Are you sure? I feel like there really isn't a use-case for generating comment tags that wouldn't pass validation. Splitting it out might be convenient for plumbing the type out of the ParseCommentTags arguments, but I don't foresee us having any situations where we'd want to create a commentTags instance and then not validate it immediately after.
/label tide/merge-method-squash |
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.
/approve
Looks really good! XXL PR but is primarily test code. Great starting point for the first alpha. Thanks for the extensive testing!
I pinned k/k against this branch and tested there are no dep changes (other than kube-openapi) and that running the code generator (without using the markers) does not result in changes
/hold can you squash (tide concats the descriptions which is not useful) edit: i squashed for you |
/ok-to-test |
shelved multiline marker comments for another PR
/hold cancel |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: alexzielenski, Schnides123 The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
This PR adds the ability to annotate API fields with basic OpenAPI schema validations via marker comments, as part of the KEP-4153: Declarative Validation feature.
/assign @alexzielenski