swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.07k stars 6.03k forks source link

[SpringCloud/OpenFeign] Support is broken out of the box #10343

Open aaronjwhiteside opened 4 years ago

aaronjwhiteside commented 4 years ago
Description
  1. OpenFeign does not override default methods.
  2. The default behaviour with <library>spring-cloud</library> is to generate default methods.
  3. Even when manually forcing defaultInterfaces=false, if you have java8=true, there is still left over crud from the defaultInterfaces support.
Swagger-codegen version

swagger-codegen-maven-plugin/3.0.20

Steps to reproduce

Note: other standard configuration options have been omitted..

                        <configuration>
                            <language>spring</language>
                            <library>spring-cloud</library>

                            <configOptions>
                                <java8>true</java8>
                                <defaultInterfaces>false</defaultInterfaces>
                                <generateForOpenFeign>true</generateForOpenFeign>
                            </configOptions>
                        </configuration>

results in the generated code:

    Logger log = LoggerFactory.getLogger(AccountBalancesApi.class);

    Optional<ObjectMapper> getObjectMapper();

    Optional<HttpServletRequest> getRequest();

being added to every API class.

Which fails when trying to load the API via OpenFeign...

Caused by: java.lang.IllegalStateException: Method getObjectMapper not annotated with HTTP method type (ex. GET, POST)
    at feign.Util.checkState(Util.java:130) ~[feign-core-10.4.0.jar:na]
    at feign.Contract$BaseContract.parseAndValidateMetadata(Contract.java:99) ~[feign-core-10.4.0.jar:na]
    at org.springframework.cloud.openfeign.support.SpringMvcContract.parseAndValidateMetadata(SpringMvcContract.java:188) ~[spring-cloud-openfeign-core-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at feign.Contract$BaseContract.parseAndValidatateMetadata(Contract.java:66) ~[feign-core-10.4.0.jar:na]
    at feign.ReflectiveFeign$ParseHandlersByName.apply(ReflectiveFeign.java:154) ~[feign-core-10.4.0.jar:na]
    at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:52) ~[feign-core-10.4.0.jar:na]
    at feign.Feign$Builder.target(Feign.java:251) ~[feign-core-10.4.0.jar:na]
Suggest a fix/enhancement
  1. defaultInterfaces=true should be the default when <library>spring-cloud</library> is used.
  2. Fix api.mustache to not include the following when using <library>spring-cloud</library>
    Logger log = LoggerFactory.getLogger(AccountBalancesApi.class);

    Optional<ObjectMapper> getObjectMapper();

    Optional<HttpServletRequest> getRequest();
bertdewolf commented 4 years ago

Is there a planning for this issue?

bitsal commented 1 year ago

is it repo still alive? I see that nobody answers long ago