Skip to content

Cannot override OpenApiResource autoconfiguration #608

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

Closed
jryan128 opened this issue Apr 24, 2020 · 2 comments
Closed

Cannot override OpenApiResource autoconfiguration #608

jryan128 opened this issue Apr 24, 2020 · 2 comments
Labels
enhancement New feature or request

Comments

@jryan128
Copy link
Contributor

jryan128 commented Apr 24, 2020

I see that SpringDocWebFluxConfiguration has @ConditionalOnMissingBean and @ConditionalOnProperty set, but when I set the property to false, or provide a bean spring still complains:

@RestController("openApiResource")
public class MyOpenApiResource {    
}

The bean 'openApiResource', defined in class path resource [org/springdoc/webflux/core/SpringDocWebFluxConfiguration.class], could not be registered. A bean with that name has already been defined in file [/Users//Desktop//services//target/classes/com////controllers/MyOpenApiResource.class] and overriding is disabled.

Using Spring Boot 2.2.6
Spring Doc 1.3.4

Thank you!

@jryan128
Copy link
Contributor Author

I see that OpenApiResource has @ConditionalOnBean directly annotated on it, and I see this in Spring Boot ConditionalOnBean java docs

You need to be very careful about the order in which bean definitions are added, as these conditions are evaluated based on what has been processed so far. For this reason, we recommend using only @ConditionalOnBean and @ConditionalOnMissingBean annotations on auto-configuration classes (since these are guaranteed to load after any user-defined bean definitions have been added).

Maybe we need to remove it from the class itself?

@bnasslahsen
Copy link
Collaborator

bnasslahsen commented Apr 24, 2020

Hi @jryan128,

In fact you can override by type. But not using bean name.
In order to override, you should extend OpenApiResource.
Here is a sample code:

@RestController("openApiResource")
public class MyOpenApiResource extends OpenApiResource {


	public MyOpenApiResource(OpenAPIBuilder openAPIBuilder, AbstractRequestBuilder requestBuilder, GenericResponseBuilder responseBuilder, OperationBuilder operationParser, RequestMappingInfoHandlerMapping requestMappingHandlerMapping, Optional<List<OpenApiCustomiser>> openApiCustomisers, SpringDocConfigProperties springDocConfigProperties) {
		super(Constants.DEFAULT_GROUP_NAME, openAPIBuilder, requestBuilder, responseBuilder, operationParser, requestMappingHandlerMapping, openApiCustomisers, springDocConfigProperties);
	}

	@Operation(hidden = true)
	@GetMapping(value = API_DOCS_URL, produces = MediaType.APPLICATION_JSON_VALUE)
	public Mono<String> openapiJson(ServerHttpRequest serverHttpRequest, @Value(API_DOCS_URL) String apiDocsUrl)
			throws JsonProcessingException {
		return Mono.just("ok");
	}

	@Operation(hidden = true)
	@GetMapping(value = DEFAULT_API_DOCS_URL_YAML, produces = APPLICATION_OPENAPI_YAML)
	public Mono<String> openapiYaml(ServerHttpRequest serverHttpRequest,
			@Value(DEFAULT_API_DOCS_URL_YAML) String apiDocsUrl) throws JsonProcessingException {

		return Mono.just("ok");
	}

}

And with the next release, you can just use @RestController

@bnasslahsen bnasslahsen added the enhancement New feature or request label Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants