Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

springdoc-openapi-ui add header parameter to generated swagger

In my spring boot app I have endpoints which are validated by header parameter in my springboot app. Current swagger json looks like this:

// part of current swagger.json
...
  "paths": {
    "/path1/{param1}": {
      "get": {
        "parameters": [
          {
            "name": "param1",
            "in": "path",
            "type": "string",
            "required": true
          }
        ]
      }
    }
  }
...

I want to add missing parameter using springdoc-openapi-ui configuration so it would look like this:

// I want to achieve swagger.json which contains additional parameter
...
  "paths": {
    "/path1/{param1}": {
      "get": {
        "parameters": [
          {
            "name": "param1",
            "in": "path",
            "type": "string",
            "required": true
          },
          {
            "name": "missingParam",
            "in": "header",
            "type": "string",
            "required": true
          }
        ]
      }
    }
  }
...

I tried achieving that by adding to my appplication.yml solution from Common Parameters for Various Paths

#application.yml
...
components:
  parameters:
    hiddenParam:
      in: header
      name: missingParam
      required: true
      schema:
        type: string
paths:
  /path1:
    get:
      parameters:
        - $ref: '#/components/parameters/hiddenParam'

But it doesn't work.

My questions:

  1. Is there a way to modify my swagger result using application configuration?
  2. I want to define parameter template and add it to all endpoints, how can I achieve that?
like image 535
makozaki Avatar asked Aug 31 '20 13:08

makozaki


2 Answers

You can add the global parameters like header using OperationCustomizer as shown below. This will add your parameter to every service

@Configuration
public class SwaggerConfiguration {

    @Bean
    public OperationCustomizer customGlobalHeaders() {

        return (Operation operation, HandlerMethod handlerMethod) -> {

            Parameter missingParam1 = new Parameter()
                    .in(ParameterIn.HEADER.toString())
                    .schema(new StringSchema())
                    .name("missingParam1")
                    .description("header description2")
                    .required(true);
                    
            Parameter missingParam2 = new Parameter()
                    .in(ParameterIn.HEADER.toString())
                    .schema(new StringSchema())
                    .name("missingParam2")
                    .description("header description2")
                    .required(true);

            operation.addParametersItem(missingParam1);
            operation.addParametersItem(missingParam2);

            return operation;
        };
    }
}
like image 88
SSK Avatar answered Sep 29 '22 05:09

SSK


Finally I decided to use different approach. I defined security scheme and applied it globally as authorization header.

    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(new Info().title("My App").version("1.0.0"))
                // Components section defines Security Scheme "mySecretHeader"
                .components(new Components()
                        .addSecuritySchemes("mySecretHeader", new SecurityScheme()
                                .type(SecurityScheme.Type.APIKEY)
                                .in(SecurityScheme.In.HEADER)
                                .name("missingParam")))
                // AddSecurityItem section applies created scheme globally
                .addSecurityItem(new SecurityRequirement().addList("mySecretHeader"));
    }

Now swagger-ui.html allows testing endpoints with authorization header or without it according to tester requirements. enter image description here

Cheers!

like image 36
makozaki Avatar answered Sep 29 '22 05:09

makozaki