[BUG][Spring] Hateoas with Swagger bean resolution error
Created by: redturtlepower
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
What's the version of OpenAPI Generator used? -
Have you search for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Bounty to sponsor the fix (example)
Description
If using hateoas
config option this error occurs.
A comment on SO here states there is an issue with swagger and hateos when used together.
I found this springfox issue that highlights a solution. I tried this solution and it worked. The solution requires adding a bean resolution strategy (5 lines) + required imports (4 lines).
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method linkDiscoverers in org.springframework.hateoas.config.HateoasConfiguration required a single bean, but 16 were found:
- modelBuilderPluginRegistry: defined in null
- modelPropertyBuilderPluginRegistry: defined in null
- typeNameProviderPluginRegistry: defined in null
- documentationPluginRegistry: defined in null
- apiListingBuilderPluginRegistry: defined in null
- operationBuilderPluginRegistry: defined in null
- parameterBuilderPluginRegistry: defined in null
- expandedParameterBuilderPluginRegistry: defined in null
- resourceGroupingStrategyRegistry: defined in null
- operationModelsProviderPluginRegistry: defined in null
- defaultsProviderPluginRegistry: defined in null
- pathDecoratorRegistry: defined in null
- apiListingScannerPluginRegistry: defined in null
- relProviderPluginRegistry: defined by method 'relProviderPluginRegistry' in class path resource [org/springframework/hateoas/config/HateoasConfiguration.class]
- linkDiscovererRegistry: defined in null
- entityLinksPluginRegistry: defined by method 'entityLinksPluginRegistry' in class path resource [org/springframework/hateoas/config/WebMvcEntityLinksConfiguration.class]
Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
openapi-generator version
4.2.3
OpenAPI declaration file content or url
Command line used for generation
rmdir /q /s server\springboot\openapi
REM generate the server source code from the OpenAPI spec petstore.yaml:
java -jar openapi-generator-cli-4.2.3.jar generate -g spring -i petstore.yaml -o server\springboot\openapi -c springboot-options.json
file "springboot-options.json"
{
"basePackage":"io.swagger",
"configPackage":"io.swagger.config",
"parentGroupId":"org.springframework.boot",
"parentArtifactId":"spring-boot-starter-parent",
"parentVersion":"2.2.4.RELEASE",
"serverPort":9230,
"hateoas":true,
"delegatePattern":true
}
Steps to reproduce
Related issues/PRs
I didn't find one
Suggest a fix
The soluting code was manually added to generated file "OpenAPI2SpringBoot.java". Someone needs to update the mustache template accordingly.
import org.springframework.hateoas.client.LinkDiscoverer;
import org.springframework.http.MediaType;
import org.springframework.plugin.core.OrderAwarePluginRegistry;
import org.springframework.plugin.core.PluginRegistry;
@SpringBootApplication
@ComponentScan(basePackages = {"io.swagger", "org.openapitools.api" , "io.swagger.config"})
public class OpenAPI2SpringBoot implements CommandLineRunner {
@Override
public void run(String... arg0) throws Exception {
if (arg0.length > 0 && arg0[0].equals("exitcode")) {
throw new ExitException();
}
}
// Uncomment to fix the issue with swagger and hateoas
/*
@Bean
public PluginRegistry<LinkDiscoverer, MediaType> discoverers(
OrderAwarePluginRegistry<LinkDiscoverer, MediaType> relProviderPluginRegistry) {
return relProviderPluginRegistry;
}
*/