Skip to content

Conversation

@JLLeitschuh
Copy link
Contributor

@JLLeitschuh JLLeitschuh commented Dec 20, 2016

PR checklist

  • Read the contribution guildelines.
  • Ran the shell/batch script under ./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)
  • Filed the PR against the correct branch: master for non-breaking changes and 2.3.0 branch for breaking (non-backward compatible) changes.

Description of the PR

Adds the ability to generate Spring Server code using the decorator pattern.

Because annotations on interface parameters aren't inherited in java and aren't accessible using reflection spring requires you to have annotations like @RequestBody on the concrete class.

This means that as a developer you can not just override the methods in your controller, you must also copy the annotations from the Api interface's methods.

This fixes the problem by allowing you to generate interfaces that can be implemented and don't require you to copy the annotations from the parent.

Note: It seems like someone didn't generate the samples for one of their changes so there is a bit more noise in those files than this PR should create for consumers.

@JLLeitschuh
Copy link
Contributor Author

Some checkstyle things that clearly need to be cleaned up


import io.swagger.annotations.*;
{{#jdk8}}
import org.springframework.http.HttpStatus;
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor suggestion: what about removing the leading spaces?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can do!

@wing328
Copy link
Contributor

wing328 commented Jan 10, 2017

(the Appveyor issue can be ignored)

@JLLeitschuh
Copy link
Contributor Author

Fixed. Other than that is this good?

@wing328
Copy link
Contributor

wing328 commented Jan 12, 2017

@JLLeitschuh I did some tests using the following commands:

java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
  -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml \
  -l spring  -DdecoratorPattern=false \
  -o /var/tmp/spring/decoratorPattern_false/
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
  -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml \
  -l spring  -DdecoratorPattern=true \
  -o /var/tmp/spring/decoratorPattern_true/

Both failed to compile with different error messages (while the generated code without decoratorPattern works fine without issue)

Can you give it a try to see if you can repeat the issue?

@JLLeitschuh
Copy link
Contributor Author

You're right, I had some logic that was wrong.
Good catch.
Those should work now.

@cbornet
Copy link
Contributor

cbornet commented Jan 12, 2017

I think this is not a decorator pattern. It looks more like a delegation.
Also, shouldn't there be an implementation of the delegation interface generated by default ? For instance, doesn't it crash if you don't provide a PetApiDelegator bean/@service ?

@JLLeitschuh
Copy link
Contributor Author

I suppose that you are right, this may be more like delegation.
I was expecting the users to implement the interfaces. (That's what I plan to do). Also, one reason for using interfaces is because now users are free to use one class to implement many controllers or can use one class per controller.
I don't really want to also generate concrete services.

@cbornet
Copy link
Contributor

cbornet commented Jan 13, 2017

Well, I think the generated server should at least start without crashing and provide basic "200 OK" endpoints. After that you can add the implementations to swagger-ignore so that they are not overridden by further regen.

@JLLeitschuh
Copy link
Contributor Author

So you are saying add an additional flag flag with something like -DdecoratorPattern=true -DdecoratorPatternImplementations=false to get what functionality I want and -DdecoratorPattern=true -DdecoratorPatternImplementations=true to get what you want?

I mean, I can understand the desire but I don't really want to implement this as I have no use for it.
You are more than welcome to open a PR against this branch to add that feature if you want and I'll merge it into this one.

@JLLeitschuh
Copy link
Contributor Author

@wing328 Are you looking for anything else on this PR?

@wing328
Copy link
Contributor

wing328 commented Jan 17, 2017

@JLLeitschuh I'll do more tests later today. Will keep you posted.

@cbornet
Copy link
Contributor

cbornet commented Jan 17, 2017

@JLLeitschuh can you rename the class/option xxxDecorator to xxxService ?
As for the option, I wouldn't have too many possibilities and always generate the concrete class. Don't you need at least one concrete class yourself ?

@wing328
Copy link
Contributor

wing328 commented Jan 17, 2017

@JLLeitschuh Performed more tests and didn't find any issues.

Please kindly reply to @cbornet 's suggestions: #4439 (comment)

@JLLeitschuh
Copy link
Contributor Author

Yea, but I want to be free to implement it as I want, perhaps I want to create a class that implements two of the controller interfaces? Maybe 4? I don't want another concrete implementation on the classpath that I have to fight spring to get it to not autowire.
I'm using the generated code as a source set for a larger project.

@JLLeitschuh
Copy link
Contributor Author

I can do the renaming of xxxDecorator to xxxService

@JLLeitschuh
Copy link
Contributor Author

Wouldn't xxxDelegate make more sense @cbornet?

@JLLeitschuh JLLeitschuh changed the title Allows for generation of spring controller code using the decorator pattern Allows for generation of spring controller code using the delegate pattern Jan 17, 2017
@JLLeitschuh
Copy link
Contributor Author

I just changed everything to use the word delegate instead.
I also added a sample with the delegate pattern combined with java 8

@cbornet
Copy link
Contributor

cbornet commented Jan 18, 2017

I feel service would be better in the Spring world since you would probably use a component annotated by @service

@wing328
Copy link
Contributor

wing328 commented Jan 18, 2017

@JLLeitschuh thanks for creating the Petstore sample test with decorator pattern for Java8 but I got compilation errors when running mvn test under samples/server/petstore/springboot-delegate-j8. Here is part of the error message:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project swagger-spring: Compilation failure: Compilation failure:
[ERROR] /private/var/tmp/pr/JLLeitschuh-feat/swagger-codegen/samples/server/petstore/springboot-delegate-j8/src/main/java/io/swagger/api/PetApiController.java:[17,137] cannot find symbol
[ERROR] symbol:   class Pet
[ERROR] location: class io.swagger.api.PetApiController
[ERROR] /private/var/tmp/pr/JLLeitschuh-feat/swagger-codegen/samples/server/petstore/springboot-delegate-j8/src/main/java/io/swagger/api/PetApiController.java:[17,12] cannot find symbol
[ERROR] symbol:   class ResponseEntity
[ERROR] location: class io.swagger.api.PetApiController
[ERROR] /private/var/tmp/pr/JLLeitschuh-feat/swagger-codegen/samples/server/petstore/springboot-delegate-j8/src/main/java/io/swagger/api/PetApiController.java:[22,12] cannot find symbol
[ERROR] symbol:   class ResponseEntity
[ERROR] location: class io.swagger.api.PetApiController
[ERROR] /private/var/tmp/pr/JLLeitschuh-feat/swagger-codegen/samples/server/petstore/springboot-delegate-j8/src/main/java/io/swagger/api/PetApiController.java:[28,240] cannot find symbol
[ERROR] symbol:   class List

Did you get similar error message when running mvn test locally in your machine?




public Callable<ResponseEntity<Client>> testClientModel(@ApiParam(value = "client model" ,required=true ) @RequestBody Client body) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should not be being generated. I need to fix this.

@JLLeitschuh
Copy link
Contributor Author

JLLeitschuh commented Jan 18, 2017

@wing328
You were right. My logic was still wrong.
I didn't understand that even in mustache when a value is false #[value] still evaluates to true because the value exists.
I fixed that issue.
That made the diff against the other samples smaller.

@JLLeitschuh
Copy link
Contributor Author

I just published my changes internally and I'm using the delegatePattern=true and java8=true and everything seems to be compiling nicely.

@wing328
Copy link
Contributor

wing328 commented Jan 19, 2017

@JLLeitschuh I did more tests and no longer found issues. Thanks for your contribution.

@wing328 wing328 merged commit 36c3fa0 into swagger-api:master Jan 19, 2017
@JLLeitschuh JLLeitschuh deleted the feat/JLL/spring-decorator-pattern branch January 19, 2017 15:58
@JLLeitschuh
Copy link
Contributor Author

JLLeitschuh commented Jan 19, 2017

🎉 🎆 Thank you @wing328 Awesome work on this project. 🎆 🎉
Any ETA on the next release?
I'll continue to use my internally published version for now but I'd love to not have to rely on it for too long.

@wing328
Copy link
Contributor

wing328 commented Jan 19, 2017

ETA for 2.2.2 release is Jan/Feb 2017

@cbornet
Copy link
Contributor

cbornet commented Jan 19, 2017

No 2.3 ?

@wing328
Copy link
Contributor

wing328 commented Jan 20, 2017

@cbornet yup, I also want to release 2.3.0 right after 2.2.2.

@JLLeitschuh
Copy link
Contributor Author

I think I may have accidentally forgotten to change the rm lines of the scrips when I copied them.
I'll open another PR later tomorrow if I have time

ags="$@ generate -t modules/swagger-codegen/src/main/resources/JavaSpring -i modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -l spring -o samples/server/petstore/springboot-delegate-j8 -DdelegatePattern=true,hideGenerationTimestamp=true,java8=true"

echo "Removing files and folders under samples/server/petstore/springboot-delegate-j8/src/main"
rm -rf samples/server/petstore/springboot/src/main
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is wrong. Should be the same as the line above.

ags="$@ generate -t modules/swagger-codegen/src/main/resources/JavaSpring -i modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -l spring -o samples/server/petstore/springboot-delegate -DdelegatePattern=true,hideGenerationTimestamp=true"

echo "Removing files and folders under samples/server/petstore/springboot-delegate/src/main"
rm -rf samples/server/petstore/springboot/src/main
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is wrong. Should be the same as the line above.

wing328 pushed a commit that referenced this pull request Jan 25, 2017
@wing328 wing328 changed the title Allows for generation of spring controller code using the delegate pattern [Java][Spring] Allows for generation of spring controller code using the delegate pattern Feb 21, 2017
davidgri pushed a commit to davidgri/swagger-codegen that referenced this pull request May 11, 2017
…ttern (swagger-api#4439)

* Allows for generation of spring conroller code using the decorator pattern

* Change Decorator to Delegate in spring codegen
davidgri pushed a commit to davidgri/swagger-codegen that referenced this pull request May 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants