OpenAPITools / OpenAPITools/openapi-generator
[BUG][Spring] Hateoas with Swagger bean resolution error
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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;
}
*/
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the generated OpenAPI2SpringBoot.java and the Spring mustache template that produces it, using the provided hateoas configuration and OpenAPI Generator 4.2.3 command to reproduce the startup failure. Compare the suggested bean and imports with the generated application, then verify that a generated Spring application using hateoas starts successfully.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100