-
Notifications
You must be signed in to change notification settings - Fork 6k
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][Spring] Add java8, interfaceOnly, delegatePattern to generated server stubs #6476
Conversation
@david-noel please run spring-related petstore scripts to update petstore samples so that CIs can verify the change:
cc @bbdouglas @JFCote @sreeshas @jfiala @lukoyanov @cbornet @ePaul |
I don't understand the use for this : default methods look more powerful to me ! |
@cbornet SpringMVC annotations placed on method parameters(ex. PathVariable) are not carried over when a RestController class overrides a method from the generated interface. It does not matter if the overridden method is a default method or a regular interface method. This forces the RestController developer to manually duplicate the annotations from the generated interface code onto their RestController. This can be error prone. The code in this PR removes this burden from the developer. At the same time, the developer can still override the default method if needed. |
@david-noel I see now what you're trying to do ! Then you should make the delegate-method also a default one so that the user is not forced to implement it. |
@cbornet I can make both methods be default. I'll have the implementation of the default method return an OK status like the interfaceOnly code generation(feels like these should throw an UnSupportedOperationException, but that is another issue). As for the potential method name confusion, I can add a "do" prefix to the method the user should override. This is a common prefix used by Spring so developers should be familiar with it. How does this look? I'll commit it to the PR if you like it.
|
…hod that returns an OK status code
I'm not sure why the travis-ci build is failing. It looks like it might be environmental? Is there anything I need to do? |
I would use the |
I'll restart it later (Java-related tests are covered by CircleCI) |
@cbornet updated. The code generated will now look like this.
|
I'll merge this on coming weekend if no one has further feedback on this PR. |
@david-noel when you've time, I wonder if you can resolve the merge conflicts. |
…referenced in api.mustache re-ran spring-all-petstore.sh
@wing328 resolved the conflicts and re ran the bin/spring-all-petstore.sh script. Generated code now looks like this. @Api(value = "fake", description = "the fake API")
public interface FakeApi {
Logger log = LoggerFactory.getLogger(FakeApi.class);
default Optional<ObjectMapper> getObjectMapper() {
return Optional.empty();
}
default Optional<HttpServletRequest> getRequest() {
return Optional.empty();
}
default Optional<String> getAcceptHeader() {
return getRequest().map(r -> r.getHeader("Accept"));
}
@ApiOperation(value = "", nickname = "fakeOuterBooleanSerialize", notes = "Test serialization of outer boolean types", response = Boolean.class, tags={ "fake", })
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Output boolean", response = Boolean.class) })
@RequestMapping(value = "/fake/outer/boolean",
method = RequestMethod.POST)
default ResponseEntity<Boolean> _fakeOuterBooleanSerialize(@ApiParam(value = "Input boolean as post body" ) @Valid @RequestBody Boolean body) {
return fakeOuterBooleanSerialize(body);
}
// Override this method
default ResponseEntity<Boolean> fakeOuterBooleanSerialize(Boolean body) {
if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
if (getAcceptHeader().get().contains("application/json")) {
try {
return new ResponseEntity<>(getObjectMapper().get().readValue("{ }", Boolean.class), HttpStatus.NOT_IMPLEMENTED);
} catch (IOException e) {
log.error("Couldn't serialize response for content type application/json", e);
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
} else {
log.warn("ObjectMapper or HttpServletRequest not configured in default FakeApi interface so no example is generated");
}
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
} |
LGTM |
That PR is exactly the option I was searching for ! 👍 |
@mehdijouan please help test it:
I'll take another look over the weekend as well. |
I tested it locally and it worked well! |
This is a very good solution! I had started to Inject a delegate with default methods whish I can override. This solution should do the trick in a better way. In what version it will be available? |
PR checklist
./bin/
to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/{LANG}-petstore.sh
and./bin/security/{LANG}-petstore.sh
if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in.\bin\windows\
.3.0.0
branch for breaking (non-backward compatible) changes.Description of the PR
Add support for java8 interfaces with default methods that delegate to non-default methods
The default methods have the SpringMVC annotations while the non-default methods have no annotations.
This feature is desired so that the RestController implementer does not need to duplicate method level(PathVariable, etc) annotations on the controller class. This also provides some benefits over the class delegation code generation. First it is easier to control how the RestControllers are added to the Spring context. Second, if needed, the implementer has more control in overriding the generated code.
I ran the sample scripts under bin but was unsure if I should add the updated sample files back into the repo under this pull request. There seem to be no interfaceOnly samples in the repo, so I did not add a new one for this pull request. Also, the existing samples do change, but they are whitespace/comment changes only. I could make the samples not change at all, but it would make the mustache template file less pleasant to read.
To close #6471