You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Java "step" builders are not future-compatible #60
Brought up by @kiiadi - our beautiful Java "step" builder, which enforces required fields before optional fields will cause backwards compatibility issues when a field is changed from required to optional (which is not supposed to be a breaking change).
Furthermore, the current approach will also cause a breaking change if the order of properties change across revisions, which is also unacceptable.
Now, if any of the properties are changed from required to optional, the only thing that has to change are the interfaces - we need to add the setters from the subsequent interfaces, up to the first required interface, into the interfaces for the optional properties.
This might sound a little abstract, so concrete examples:
a) Let's say first is changed to be optional (second is still required). The interfaces will now be:
This maintains the relative ordering between the properties, and so is backwards compatible.
I've actually implemented this pattern in a library I own, which is an annotation processor for generating "step" Builders, called Jilt - it's the TYPE_SAFE_UNGROUPED_OPTIONALS style.
@skinny85 thanks! Makes total sense. However, I think that won't work in the jsii case, since we are generating these builders from the jsii spec. We don't have historical context about the API, so we can't tell that this was once a required field, only that it's now optional. Does that make sense?
@skinny85 how do you know which 'additional' properties to generate on the interfaces based on what was previously required? To put it another way - how do you know what was previously required?
In your scenario a) when I create the initial builder - what interface gets returned?
In scenario a) where second is required but first is optional - if this were the first iteration of that object (ie - we'd never seen it before), one would have to create it like this:
@skinny85 how do you know which 'additional' properties to generate on the interfaces based on what was previously required? To put it another way - how do you know what was previously required?
It doesn't matter what was previously required - it only matters what is required currently.
In your scenario a) when I create the initial builder - what interface gets returned?
TargetBuilder.target() returns FirstB - always, in all 3 variants, regardless of whether first is required or not (because first is the first property).
In scenario a) where second is required but first is optional - if this were the first iteration of that object (ie - we'd never seen it before), one would have to create it like this:
I guess the crux of it is, that the "step" approach programmatically enforces an order that customers can call methods in. (Which is a great customer experience, it makes it easier to use the API if it's telling you what is legal to call next) However in order to do it well we need to ensure that the order remains stable - otherwise we'll introduce a backwards compatibility issue.
I guess the crux of it is, that the "step" approach programmatically enforces an order that customers can call methods in. (Which is a great customer experience, it makes it easier to use the API if it's telling you what is legal to call next) However in order to do it well we need to ensure that the order remains stable - otherwise we'll introduce a backwards compatibility issue.
The way I've shown above indeed fixes the order of the properties, and changing it is a backwards-incompatible change. That's usually not a huge deal (the order doesn't matter that much), but if we're worried about this being too rigid, there is a slight variation that allows you to code-generate your way out of even this problem :).
The basic idea is to generate interfaces for each possible ordering of assigning properties to your Builder (and, by extension, your target class). Let me show you what that looks like, again on the same Target class example shown above (I'll only be showing the interfaces, the rest is the same - in particular, TargetBuilder.target() always returns FirstB):
Starting situation (both first and second are required)
TargetBuilder.target().first(1).second("").build(); // still fineTargetBuilder.target().second("").first(1).build(); // also still fine
but now you can also do:
TargetBuilder.target().second("").build(); // no first given
Variants b) and c) are very similar - I won't bother showing them.
This requires generating on the order of n! interfaces, where n is the number of properties of the target class, but it does make the "step" Builder immune to property order changes.
My question was more when we get a model how will we know what the 'first' property is? Are we relying on the order of the properties in the collection? Are we doing it alphabetically?
The above works OK when we have 1 or two properties - but what about when we have 20? Are we going to generate an interface for every permutation of every property? I think that's going to lead to very complex looking, large classes.
My question was more when we get a model how will we know what the 'first' property is? Are we relying in the order of the properties in the collection? Are we doing it alphabetically?
I believe we would be relying on the order the properties are declared in in TypeScript code.
The above works OK when we have 1 or two properties - but what about when we have 20? Are we going to generate an interface for every permutation of every property? I think that's going to lead to very complex looking, large classes.
Yes, like I said, for a class with 20 properties, we would generate 20! interfaces (note however that the Builder class will still be simple).
I believe we would be relying on the order the properties are declared in in TypeScript code.
Whilst it looks to be modeled as an Array (which should be ordered) I'm not sure how this array is populated in JSII. This ordering requirement may add additional complexity at several layers of Typescript code. Where does it come from?
@eladb that's kinda what I suspected. I think you're right. I love the idea of compile-time validation and getting the IDE to help me "do it right" but I'm just not sure how we can do it in a backwards compatible way.
Unless @rix0rrr's generic magic comes to the rescue! I'm also keen to take a look at that!
@skinny85 I love the step-builders too for all your same reasons - I would have loved to have done something similar in the Java SDK. However due to the problems with backwards compatibility it creates (and we have no way to enforce service teams to introduce new structure members in a specific order) we didn't go that route.
I don't think guidance around keeping the order is a sustainable approach. We can't expect that to be preserved. I think we will have to not use step builders 😢
I've given this topic a lot of thought, and I believe I came up with a possible solution.
I won't detail on how I arrived at it (it will make a good blog post, or possibly a made-for-TV movie), but it looks something like the following.
Imagine the target class is called Example. It has 4 properties, named first, second, third and fourth. Only the first 2 (first and second) are required, third and fourth are optional:
publicfinalclassExampleBuilderimplementsFinalBuilderInterf {
publicstaticBuilderInterf<
FinalBuilderInterf
> example() {
returnnewExampleBuilder();
}
// rest of the class exactly the same as above...
}
All of the previous usages of ExampleBuilder (when first was still required) will remain valid after first becomes optional, and now this will compile:
ExampleBuilder.example().second(3).build() // fine now
A constant (2 to be exact) number of interfaces required, regardless of the number of properties the target class has
Only one Builder class, regardless of the number of properties the target class has
Handles providing the properties in an arbitrary order
Handles evolving the API (changing a property from required to optional)
Cons:
The signature of the builder static factory method (ExampleBuilder.example() above) is complex (and becomes more complex as the number of required properties grow), and it's visible to our customers
If a customer provides a required property twice before providing all of the required properties (so: ExampleBuilder.example().first("").first("")), they will not be able to proceed from there (that last first returns java.lang.Object).
@eladb and @kiiadi , I'd love your take on this - does any of this make sense, or am I just going crazy here?
I've worked on this some more. Turns out, there are 2 ways of achieving what we want here, that is a Builder that:
Guarantees, at compile time, that all of the required properties of the class are provided before constructing an instance.
Uses a single Builder class, in-place (does not require nested classes, or extra allocations).
Does not care about the order of the properties.
Maintains backwards compatibility when changing a property from required to optional (without "historic" context).
The method I outlined above, which I call the 'static factory method' variant (OK, I agree it's not the greatest name, but it's not like there's a wealth of existing vocabulary I can draw from in this case), is one way. The one big pro it has is that it generates a constant number of interfaces (just 2), regardless of how many properties does the built class have. However, it has 2 downsides: the complicated type of the client-facing static factory method, and the weird behavior when providing the same required property twice, before all of the required properties have been provided.
I've tried to correct these faults, but I was not able to do it. What I did do, however, is discover an alternative way of formulating the Builder. I call it the 'interfaces' variant. It doesn't have any of the faults of the 'static factory method' variant - the types are all simple, and you can specify any property multiple times, and nothing weird happens. It has one disadvantage though - it requires generating 2^m interfaces, where m is the number of required properties of the built class. However, I don't think I recall a CDK type with more than 3 required properties (the number of optional properties does not increase the interface count at all), so I think it's acceptable for our use case.
I wish the 2-interface approach wouldn’t have that issue with double-required... I don’t think the complexity of the returned type is a blocker, but the double-required issue is.
I do think that you should at least add another required property to your prototypes: two is a tricky number for tests... especially due to the exponential implications of these solutions (perhaps you can generate the code...).
The CDK does and will have types with a large number of required properties, so the 2^m number of interfaces in the 2nd approach does worry me...
I added an example for a 3 required property class here - right now, only in the 'interfaces' variant, I'll push the 'static factory method' variant soon.
I'm banging my head against the wall trying to fix the double required property problem in the 'static factory method' variant. The best I could come up with right now was a little cheat: we can "balloon" the returned type to accommodate specifying the same field i times, for any i > 0. For example, if we take the return type above:
our Builder will now handle specifying the same property twice, but will fail in the same way as before if specifying it 3 times. We could then change it in a similar way to accommodate the 3 case, but would fail for 4, etc.
I'm yet to figure out a solution that would handle repeating the same property an arbitrary number of times.
After thinking about this problem even more, I'm starting to doubt whether we'll achieve what we want here.
Even assuming we will solve the above problem with the 'static factory method' variant, there are 2 things that concern me.
The first is the client experience of using these Builders. The main advantage of the Step builders was always the fact that the required properties were encoded by the type system - so, the clients of the class always knew the minimum set of properties they had to provide in order to construct an instance of the class. With this approach, this advantage is gone: if a class has 10 properties, but only 2 required ones, the client has no way of knowing which 2 of the 10 are required, short of looking at the documentation - and if we're making clients rely on the documentation, instead of the type system, we might as well provide a "dumb" Builder at that point.
The second concern is that the intermediate expressions in the Builder change types as we make required properties optional. This is true for both the 'static factory method' variant as well as the 'interfaces' one. What I mean is, if we have code like this:
The type returned from ExampleBuilde.example().first("") will change if we release a new version of the CDK with first changed from required to optional - it will no longer be SomeType, and the above code will no longer compile.
Of course, that code is kind of weird, but we can't guarantee our Builders will not be used in weird ways.
Because of these concerns, even if we did have a future-proof Step Builder implementation that required generating a small number of extra interfaces, I'm not sure if we should use it.
I like where the thinking is going, and the implementation is a lot cleaner with only two interfaces to generate.
I share some of your concerns around changes being compilable in future - we need to ensure that our builders are backwards compatible - we have examples in the Java SDK of customers passing builders around (so that they can build up mutations over time).
I also have concerns about the generic typing leaking out - especially for large classes. Sure it's not a problem in languages (e.g. like Kotlin / Scala) which do type inference, and even in Java 10 it's less of a problem because of the introduction of var but the vast majority of customers are going to be using Java 8 or lower, that means their assignment of the builder variable includes all of the generics:
The other comment I'd make is about compatibility - we need to ensure not just compile-time backwards compatibility but also binary compatibility. These constructs are going to be used in a hierarchy of dependencies - with 3rd party libraries also depending on the CDK/JSII. We can't assume that each intermediate dependency is going to be recompiled against a new version of the library.
That means things like changing return types (i.e. introducing/removing generic parameters), even if the methods available remains the same - cannot be permitted.
Hah. Interesting. I've actually solved the "required property provided multiple times before all required properties" problem of the 'static factory method' variant. It actually turned out to be much simpler than I thought. And the resulting type of the static factory method is much cleaner and shorter than what was there before.
I've pushed the examples to the test repo ([example with 2 properties], [example with 3 properties]), both with some tests confirming the behavior. I'm repeating the return type for the 3-variant below, as it's probably the craziest Java method signature I will write in my career:
It doesn't invalidate any of the concerns I voiced above, but at the very least it's interesting that it was possible to express in Java using only 2 interfaces.
Activity
skinny85 commentedon Jul 10, 2018
Hey @eladb ,
there is actually a way to do this with "step" Builders.
Example: we have a class
Targetwith 2 properties,firstandsecond, both of which are required:So, the interfaces that the Builders implement look something like:
and the Builder itself:
used like so:
Now, if any of the properties are changed from required to optional, the only thing that has to change are the interfaces - we need to add the setters from the subsequent interfaces, up to the first required interface, into the interfaces for the optional properties.
This might sound a little abstract, so concrete examples:
a) Let's say
firstis changed to be optional (secondis still required). The interfaces will now be:b) The reverse -
secondis now optional (firstis still required):c) Both are now optional:
This maintains the relative ordering between the properties, and so is backwards compatible.
I've actually implemented this pattern in a library I own, which is an annotation processor for generating "step" Builders, called Jilt - it's the
TYPE_SAFE_UNGROUPED_OPTIONALSstyle.eladb commentedon Jul 10, 2018
@skinny85 thanks! Makes total sense. However, I think that won't work in the jsii case, since we are generating these builders from the jsii spec. We don't have historical context about the API, so we can't tell that this was once a required field, only that it's now optional. Does that make sense?
skinny85 commentedon Jul 10, 2018
Sorry, but it doesn't ;p.
Why would you need historical context for this generation?
kiiadi commentedon Jul 10, 2018
@skinny85 how do you know which 'additional' properties to generate on the interfaces based on what was previously required? To put it another way - how do you know what was previously required?
In your scenario a) when I create the initial builder - what interface gets returned?
In scenario a) where second is required but first is optional - if this were the first iteration of that object (ie - we'd never seen it before), one would have to create it like this:
So without keeping historical information about how the model has mutated over time, how will we know to allow the first code sample?
skinny85 commentedon Jul 10, 2018
It doesn't matter what was previously required - it only matters what is required currently.
TargetBuilder.target()returnsFirstB- always, in all 3 variants, regardless of whetherfirstis required or not (becausefirstis the first property).Nope. The creation would still be the same as before (when both
firstandsecondwere optional):In variant a), if you want to construct a
Targetwithoutfirst, it looks like this:Does this make it more clear?
kiiadi commentedon Jul 10, 2018
I think that makes sense - how do we know what is the "first" property?
eladb commentedon Jul 10, 2018
🤕my head is exploding... I need to read this a few more times...
eladb commentedon Jul 10, 2018
By the way, code that emits the builders under jsii-pacmak/lib/generators/java.ts#L332.
kiiadi commentedon Jul 10, 2018
I guess the crux of it is, that the "step" approach programmatically enforces an order that customers can call methods in. (Which is a great customer experience, it makes it easier to use the API if it's telling you what is legal to call next) However in order to do it well we need to ensure that the order remains stable - otherwise we'll introduce a backwards compatibility issue.
skinny85 commentedon Jul 10, 2018
The way I've shown above indeed fixes the order of the properties, and changing it is a backwards-incompatible change. That's usually not a huge deal (the order doesn't matter that much), but if we're worried about this being too rigid, there is a slight variation that allows you to code-generate your way out of even this problem :).
The basic idea is to generate interfaces for each possible ordering of assigning properties to your Builder (and, by extension, your target class). Let me show you what that looks like, again on the same
Targetclass example shown above (I'll only be showing the interfaces, the rest is the same - in particular,TargetBuilder.target()always returnsFirstB):Starting situation (both
firstandsecondare required)So, you can do:
but not:
Variant a) (
firstturned from required to optional,secondis still required)So, this is still fine:
but now you can also do:
Variants b) and c) are very similar - I won't bother showing them.
This requires generating on the order of
n!interfaces, wherenis the number of properties of the target class, but it does make the "step" Builder immune to property order changes.kiiadi commentedon Jul 10, 2018
My question was more when we get a model how will we know what the 'first' property is? Are we relying on the order of the properties in the collection? Are we doing it alphabetically?
The above works OK when we have 1 or two properties - but what about when we have 20? Are we going to generate an interface for every permutation of every property? I think that's going to lead to very complex looking, large classes.
skinny85 commentedon Jul 10, 2018
I believe we would be relying on the order the properties are declared in in TypeScript code.
Yes, like I said, for a class with 20 properties, we would generate 20! interfaces (note however that the Builder class will still be simple).
kiiadi commentedon Jul 10, 2018
Whilst it looks to be modeled as an
Array(which should be ordered) I'm not sure how this array is populated in JSII. This ordering requirement may add additional complexity at several layers of Typescript code. Where does it come from?eladb commentedon Jul 10, 2018
I don't think we can rely on ordering since it can arbitrarily be changed by authors, and they don't expect that to be a breaking change.
I also don't think it's scalable to generate N! interfaces for every builder (we have many builders with many properties).
Back to the drawing board? (or just do flat builders with runtime validation)
@rix0rrr had some magical generic incantation, can you share?
kiiadi commentedon Jul 10, 2018
@eladb that's kinda what I suspected. I think you're right. I love the idea of compile-time validation and getting the IDE to help me "do it right" but I'm just not sure how we can do it in a backwards compatible way.
Unless @rix0rrr's generic magic comes to the rescue! I'm also keen to take a look at that!
5 remaining items
kiiadi commentedon Jul 12, 2018
@skinny85 I love the step-builders too for all your same reasons - I would have loved to have done something similar in the Java SDK. However due to the problems with backwards compatibility it creates (and we have no way to enforce service teams to introduce new structure members in a specific order) we didn't go that route.
eladb commentedon Jul 15, 2018
I don't think guidance around keeping the order is a sustainable approach. We can't expect that to be preserved. I think we will have to not use step builders 😢
skinny85 commentedon Jul 17, 2018
😭
skinny85 commentedon Aug 4, 2018
I've given this topic a lot of thought, and I believe I came up with a possible solution.
I won't detail on how I arrived at it (it will make a good blog post, or possibly a made-for-TV movie), but it looks something like the following.
Imagine the target class is called
Example. It has 4 properties, namedfirst,second,thirdandfourth. Only the first 2 (firstandsecond) are required,thirdandfourthare optional:We need the following 2 interfaces for type safety:
And finally the Builder itself:
Now, you can use it like this:
and it's not possible to skip providing a required property:
If, in the future,
firstchanges from required to optional, the newly-generated interfaces for type-safety will be:and the Builder itself:
All of the previous usages of
ExampleBuilder(whenfirstwas still required) will remain valid afterfirstbecomes optional, and now this will compile:I understand this is a ton of code to look at, so I put it in a GitHub repo, feel free to take a look here: https://github.com/skinny85/future-proof-step-builder
Pros of this solution:
Cons:
ExampleBuilder.example()above) is complex (and becomes more complex as the number of required properties grow), and it's visible to our customersExampleBuilder.example().first("").first("")), they will not be able to proceed from there (that lastfirstreturnsjava.lang.Object).@eladb and @kiiadi , I'd love your take on this - does any of this make sense, or am I just going crazy here?
skinny85 commentedon Aug 5, 2018
I've worked on this some more. Turns out, there are 2 ways of achieving what we want here, that is a Builder that:
The method I outlined above, which I call the 'static factory method' variant (OK, I agree it's not the greatest name, but it's not like there's a wealth of existing vocabulary I can draw from in this case), is one way. The one big pro it has is that it generates a constant number of interfaces (just 2), regardless of how many properties does the built class have. However, it has 2 downsides: the complicated type of the client-facing static factory method, and the weird behavior when providing the same required property twice, before all of the required properties have been provided.
I've tried to correct these faults, but I was not able to do it. What I did do, however, is discover an alternative way of formulating the Builder. I call it the 'interfaces' variant. It doesn't have any of the faults of the 'static factory method' variant - the types are all simple, and you can specify any property multiple times, and nothing weird happens. It has one disadvantage though - it requires generating 2^m interfaces, where m is the number of required properties of the built class. However, I don't think I recall a CDK type with more than 3 required properties (the number of optional properties does not increase the interface count at all), so I think it's acceptable for our use case.
I won't paste the 'interfaces' variant code here, but it's all in the example repo: https://github.com/skinny85/future-proof-step-builder
eladb commentedon Aug 5, 2018
Wow 😮 impressive solutions!
I wish the 2-interface approach wouldn’t have that issue with double-required... I don’t think the complexity of the returned type is a blocker, but the double-required issue is.
I do think that you should at least add another required property to your prototypes: two is a tricky number for tests... especially due to the exponential implications of these solutions (perhaps you can generate the code...).
The CDK does and will have types with a large number of required properties, so the 2^m number of interfaces in the 2nd approach does worry me...
skinny85 commentedon Aug 6, 2018
I added an example for a 3 required property class here - right now, only in the 'interfaces' variant, I'll push the 'static factory method' variant soon.
I'm banging my head against the wall trying to fix the double required property problem in the 'static factory method' variant. The best I could come up with right now was a little cheat: we can "balloon" the returned type to accommodate specifying the same field i times, for any i > 0. For example, if we take the return type above:
and change it to this:
our Builder will now handle specifying the same property twice, but will fail in the same way as before if specifying it 3 times. We could then change it in a similar way to accommodate the 3 case, but would fail for 4, etc.
I'm yet to figure out a solution that would handle repeating the same property an arbitrary number of times.
eladb commentedon Aug 7, 2018
@kiiadi what do you think?
skinny85 commentedon Aug 7, 2018
After thinking about this problem even more, I'm starting to doubt whether we'll achieve what we want here.
Even assuming we will solve the above problem with the 'static factory method' variant, there are 2 things that concern me.
The first is the client experience of using these Builders. The main advantage of the Step builders was always the fact that the required properties were encoded by the type system - so, the clients of the class always knew the minimum set of properties they had to provide in order to construct an instance of the class. With this approach, this advantage is gone: if a class has 10 properties, but only 2 required ones, the client has no way of knowing which 2 of the 10 are required, short of looking at the documentation - and if we're making clients rely on the documentation, instead of the type system, we might as well provide a "dumb" Builder at that point.
The second concern is that the intermediate expressions in the Builder change types as we make required properties optional. This is true for both the 'static factory method' variant as well as the 'interfaces' one. What I mean is, if we have code like this:
The type returned from
ExampleBuilde.example().first("")will change if we release a new version of the CDK withfirstchanged from required to optional - it will no longer beSomeType, and the above code will no longer compile.Of course, that code is kind of weird, but we can't guarantee our Builders will not be used in weird ways.
Because of these concerns, even if we did have a future-proof Step Builder implementation that required generating a small number of extra interfaces, I'm not sure if we should use it.
kiiadi commentedon Aug 7, 2018
I like where the thinking is going, and the implementation is a lot cleaner with only two interfaces to generate.
I share some of your concerns around changes being compilable in future - we need to ensure that our builders are backwards compatible - we have examples in the Java SDK of customers passing builders around (so that they can build up mutations over time).
I also have concerns about the generic typing leaking out - especially for large classes. Sure it's not a problem in languages (e.g. like Kotlin / Scala) which do type inference, and even in Java 10 it's less of a problem because of the introduction of
varbut the vast majority of customers are going to be using Java 8 or lower, that means their assignment of the builder variable includes all of the generics:This gets pretty ugly.
kiiadi commentedon Aug 7, 2018
The other comment I'd make is about compatibility - we need to ensure not just compile-time backwards compatibility but also binary compatibility. These constructs are going to be used in a hierarchy of dependencies - with 3rd party libraries also depending on the CDK/JSII. We can't assume that each intermediate dependency is going to be recompiled against a new version of the library.
That means things like changing return types (i.e. introducing/removing generic parameters), even if the methods available remains the same - cannot be permitted.
skinny85 commentedon Aug 8, 2018
Hah. Interesting. I've actually solved the "required property provided multiple times before all required properties" problem of the 'static factory method' variant. It actually turned out to be much simpler than I thought. And the resulting type of the static factory method is much cleaner and shorter than what was there before.
I've pushed the examples to the test repo ([example with 2 properties], [example with 3 properties]), both with some tests confirming the behavior. I'm repeating the return type for the 3-variant below, as it's probably the craziest Java method signature I will write in my career:
It doesn't invalidate any of the concerns I voiced above, but at the very least it's interesting that it was possible to express in Java using only 2 interfaces.
Stop emitting Java step builders for data types
feat(jsii): Further normalize assembly outputs (#177)