-
-
Notifications
You must be signed in to change notification settings - Fork 689
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
Java optional codegen #1858
Java optional codegen #1858
Conversation
Is this required? I see |
Oh nice! I didn't see it in the on the website and I also found joelittlejohn/jsonschema2pojo#1057 which led me to believe it wasn't supported. But here it is! I'll take it for a spin. |
…to java-optional-codegen
gherkin/java/src/test/java/io/cucumber/gherkin/GherkinTest.java
Outdated
Show resolved
Hide resolved
The custom code generator I implemented would do I could add a Worthwhile restoring the custom code generator for those purposes? I think so! |
Done! Been doing too much TypeScript lately... |
I think it would be cleaner to wrap List constructor arguments in unmodifiable lists, to make the objects truly immutable. Agree? |
I made them unmodifiable :-) |
Great. It's not the same as immutable though. Nice little gotcha from way back then. |
Good to merge @mpkorstanje ? |
<%- end -%> | ||
<%- end -%> | ||
|
||
private void validate() { |
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.
Don't forget to remove this.
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.
Why @mpkorstanje? We're calling validate()
in the constructors and the static from
methods.
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.
If the constructors already have the requireNonNull
for each required argument then what is there left to validate?
I'll have to take a look at the generated code. But hard to say right now just from the git diff. |
Ah! Thanks that helps a lot! Consider:
The combination of the first two make it easy to read the code and see what are the required and optional fields |
If we make all fields public static Envelope from(Source source) {
return new Envelope(
null,
null,
null,
null,
null,
null,
null,
Objects.requireNonNull(source, "Envelope.source cannot be null"),
null,
null,
null,
null,
null,
null,
null,
null,
);
} That's fine with me - is this what you're suggesting? With this we can also remove the private empty constructors, as well as the |
No major objects. Though what was the reason we wanted to use |
So we don't have to create return new Envelope(
null,
null,
null,
null,
null,
null,
null,
Objects.requireNonNull(source, "Envelope.source cannot be null"),
null,
null,
null,
null,
null,
null,
null,
null,
) |
Oh I see what you mean - might as well be a 1-arg constructor. I'll change to that in #1879 |
Closing in favour of #1879 |
Summary
Use custom code generation for Java
Details
Switch from using
jsonschema2pojo-maven-plugin
to using a custom code generator.Motivation and Context
In order to have better type safety for non-required fields, we want the generated Java code to use
Optional
in getters. See cucumber/ci-environment#51 for context.Types of changes
Checklist: