Skip to content
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

Add generation of back-compat methods for optional query parameters #21

Merged
merged 7 commits into from
Jun 27, 2018

Conversation

markelliot
Copy link
Contributor

No description provided.

@markelliot
Copy link
Contributor Author

@bavardage

}
};

private interface DefaultTypeVisitor<T> extends Type.Visitor<T> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for ergonomics, I'd love derive4j style visitors, so I could write:

foo.visitor()
    .optional(x -> blargh())
    .elseThrow()
    .visit()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#22 reduces a lot of the union code/might make it simpler to add friendlier visit patterns

@@ -117,4 +121,42 @@ public interface TestService {
@GET
@Path("catalog/integer")
int testInteger(@HeaderParam("Authorization") AuthHeader authHeader);

@Deprecated
default int testQueryParams(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note to self: works fine in feign as described in https://github.com/OpenFeign/feign#static-and-default-methods

}
};

private static final Type.Visitor<Boolean> TYPE_BACKFILL_PREDICATE = new DefaultTypeVisitor<Boolean>() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be worth (minimal) javadoc here too, given this is a bit more opaque than the above two visitors - I have to scan up to understand what this is

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This visitor and the one above are kinda coupled (ie we'd never want them to every be out of sync). Instead of a whole visitor here with DefaultTypeVisitor inheritance, can we just use an if statement with a fallback?

private static boolean canBeCoercedFromAbsent(Type type) {
  return type.accept(TypeVisitor.IS_OPTIONAL) || type.accept(TypeVisitor.IS_LIST) || type.accept(TypeVisitor.IS_SET) || type.accept(TypeVisitor.IS_MAP);
}

private static int typeSortOrder(Type type) {
  return canBeCoercedFromAbsent(type) ? 1 : 0;
}

We might then be able to delete the DefaultTypeVisitor entirely (as I think we've got another of these in the TypeVisitor class already)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@iamdanfox
Copy link
Contributor

Got some context behind this PR from slack:

Every time an API author adds something to their API endpoints (even if it's backwards compatible), all their consumers suffer a compilation break as they now have to supply Optional.empty() or something as the last parameter.

This PR tries to reduce these compilation failures by generating default interface methods which try to keep old signatures and just pass Optional.empty() or Collection.emptySet().

CreateDatasetRequest request,
@HeaderParam("Test-Header") String testHeaderArg);
@HeaderParam("Test-Header") String testHeaderArg,
CreateDatasetRequest request);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be a one-off compilation break, but I like that it'll be stable from now onwards :)

@@ -236,4 +343,188 @@ private static ClassName httpMethodToClassName(String method) {
throw new IllegalArgumentException("Unrecognized HTTP method: " + method);
}
}

private static final ParameterType.Visitor<Boolean> QUERY_PARAM_PREDICATE = new ParameterType.Visitor<Boolean>() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use ParameterTypeVisitor.IS_QUERY instead of this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor

@iamdanfox iamdanfox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR only affects jersey services but not retrofit ones. Even so, I think it's worth merging now as it's a strict improvement over the current state of the world (and is worth the one-off compilation break)!

@markelliot
Copy link
Contributor Author

One tricky thing about the sort order stuff is that a parameter that used to be required but is now optional will move in position. So this doesn’t fix all kinds of compile breaks due to Conjure changes, but it should fix a large class of practical changes we observe.

@iamdanfox iamdanfox merged commit dd7e4fe into develop Jun 27, 2018
@iamdanfox iamdanfox deleted the me/better-java-backcompat branch June 27, 2018 17:35
@iamdanfox iamdanfox mentioned this pull request Jul 5, 2018
carterkozak pushed a commit to carterkozak/conjure-java that referenced this pull request Dec 24, 2018
* update conjure to 4.0.0-rc*

* use cli

* checkstyle

* update test

* remove unneeded test resources

* fix test

* rebase and fix conflicts

* address comments

* bump to rc3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants