Skip to content

New Feature: OpenAPICustomiser #92

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
davidmelia opened this issue Sep 23, 2019 · 4 comments
Closed

New Feature: OpenAPICustomiser #92

davidmelia opened this issue Sep 23, 2019 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@davidmelia
Copy link

Hi,

I have a requirement to add a common header to every API call so I've come up with the following:

  1. Turn off the default springdoc OpenApiResource:
    springdoc.api-docs.enabled=false
  2. Create a customisable interface
public interface OpenAPICustomiser {
  public void customise(OpenAPI openApi);
}
  1. Extend OpenApiResource and add the optional customiser check.
@RestController
public class CustomisableOpenApiResource extends org.springdoc.api.OpenApiResource {

private final Optional<List<OpenAPICustomiser>> openApiCustomisers;
. . .
  @Override
  protected OpenAPI getOpenApi() {
    final OpenAPI openApi = super.getOpenApi();
    if (openApiCustomisers.isPresent()) {
      openApiCustomisers.get().stream().forEach(openApiCustomiser -> openApiCustomiser.customise(openApi));
    }
    return openApi;
  }

  1. Add one to many customisations - following shows adding a common header to every operation:
  @Bean
  public OpenAPICustomiser consumerTypeHeaderOpenAPICustomiser() {
    return openApi -> openApi.getPaths().values().stream().flatMap(pathItem -> pathItem.readOperations().stream())
        .forEach(operation -> operation.addParametersItem(new HeaderParameter().$ref("#/components/parameters/myConsumerTypeHeader")));
  }

Would it be possible to provide this ability in the default org.springdoc.api.OpenApiResource?

Thanks

@springdoc
Copy link
Collaborator

Hi,

Can you propose a pull request, with this feature ?

@springdoc springdoc added the enhancement New feature or request label Sep 23, 2019
@davidmelia
Copy link
Author

sure will have a go this week. Thanks

davidmelia pushed a commit to davidmelia/springdoc-openapi that referenced this issue Sep 23, 2019
@davidmelia
Copy link
Author

@springdoc have a look at

#93

Thanks

@springdoc
Copy link
Collaborator

Hi,

Thank you for your contribution.
This feature, will be available on v1.1.43 of springdoc-openapi.

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

1 participant