question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Programmatic customization of the OpenAPI, problem with GroupedOpenApi?!

See original GitHub issue

The docs state that programmatic customization of the OpenAPI object is supported:

@Bean
public OpenAPI customOpenAPI(@Value("${springdoc.version}") String appVersion) {
  return new OpenAPI()
  .components(new Components().addSecuritySchemes("basicScheme",
          new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("basic")))
  .info(new Info().title("SpringShop API").version(appVersion)
          .license(new License().name("Apache 2.0").url("http://springdoc.org")));
}

Is it supported / allowed to add parts of the API spec here programmatically (paths)? Something like this:

    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .path("/foo",
                        new PathItem().get(new Operation().operationId("foo").responses(new ApiResponses()
                                .addApiResponse("default",
                                        new ApiResponse()
                                                .description("")
                                                .content(new Content().addMediaType("fatz", new MediaType()))))));
    }

Use case might be if some generic endpoints exists and someone prefers to add the spec programmatically instead of adding hunderts of swagger annotations to the controller. Or also someone has written the Open API spec by hand and wants to include it even before he actually implemented it (load from file, parse, add to object).

If it is supported / allowed, it currently causes a bug when using it at the same time with the GroupedOpenApi feature. The definitions added to the OpenAPI object appear within every group-endpoint-response, no matter if they fullfill the conditions specified on the GroupedOpenApi or not.

If it is NOT supported / allowed, maybe there should be a comment in the docs about it?!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
bnasslahsencommented, Jan 22, 2020

Hi @kaibra,

Maybe we need to add more explanation on the documentation about this behaviour your are describing.

If you need the defintions to appear globally (withing every group), no matter if the group fulfills the conditions specified on the GroupedOpenApi , you can use OpenAPI Bean.

@Bean
public OpenAPI customOpenAPI() {
	return new OpenAPI().path("/foo",
	new PathItem().get(new Operation().operationId("foo").responses(new ApiResponses()
	.addApiResponse("default",new ApiResponse()description("")
	content(new Content().addMediaType("fatz", new MediaType()))))));
}

If you need the defintions to appear withing a specific group, and respect the conditions specified on the GroupedOpenApi, you can add OpenApiCustomiser to your GroupedOpenApi definition.


GroupedOpenApi.builder().setGroup("users").pathsToMatch(paths).packagesToScan(packagedToMatch).addOpenApiCustomiser(customerGlobalHeaderOpenApiCustomiser())
                .build()

@Bean
public OpenApiCustomiser customerGlobalHeaderOpenApiCustomiser() {
	return openApi -> openApi.path("/foo",
	new PathItem().get(new Operation().operationId("foo").responses(new ApiResponses()
	.addApiResponse("default",new ApiResponse().description("")
	.content(new Content().addMediaType("fatz", new MediaType()))))));
}

This should answer to boths usages. Please let me know if its ok for you.

0reactions
bnasslahsencommented, Feb 20, 2020

Hi @kaibra,

Sorry I haven’t seen your message. You can of course propose a PR, which integrates this functionality, so we can enhance the library. It would have more sense in separate maven module: springdoc-openapi-swagger-parser. We can add on the class GroupConfig, the property staticSpecLocation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

F.A.Q - Springdoc-openapi
Can I customize OpenAPI object programmatically? You can Define your own OpenAPI Bean: If you need the definitions to appear globally (within every...
Read more >
Schemas disappear from components when programmatically ...
I am setting up my configuration programmatically, and have 2 groups. I'm using Spring Boot v2.4.0 with springdoc-openapi-ui v1.5.1. Snippet of ...
Read more >
Bringing together OpenAPI 3 and Spring Boot by Badr ...
Spring I/O Bridge (online conference) - 15 May 2020GitHub repo: https://github.com/springdoc/springdoc- openapi -demos.
Read more >
Documenting a Spring REST API Using OpenAPI 3.0 - Baeldung
Learn how to generate OpenAPI 3.0 specifications for a Spring REST API ... For example, let's customize the path of our API documentation....
Read more >
spring-projects/spring-boot - Gitter
Is there any way to configure this programmatically: springdoc.swagger-ui.path=/swagger-ui-custom.html ? With a Java bean I mean. Davide Pugliese. @Deviad.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found