Mask input (example - password) in Swagger UI?

8.8k Views Asked by At

I have three path variables for an API. I want to mask one input on Swagger UI with *****.

How can I do this when using Springdoc OpenAPI?

3

There are 3 best solutions below

0
On

You just use the swagger annotations:

@Parameter(schema = @Schema(type = "string", format = "password")) 
5
On

As already shown by jenkinsme in their answer, set the format to password. Also, the type field is not needed as it defaults to string (hopefully all passwords are strings).

@Parameter(schema = @Schema(format = "password"))

The above will show up as shown in the below image

enter image description here

Refer the OpenAPI specification page on Data Types for all the supported types

0
On

Using YAML for generating code from OpenAPI 3 documentation:

components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: string
          format: uuid
        username:
          type: string
        password:
          type: string
          format: password
        givenName:
          type: string
        familyName:
          type: string
        address:
          type: string
      required:
        - id
        - username
        - password
        - givenName
        - familyName
        - address

Note, that type: string is not necessary for password, since format: password already implies it, but I like to keep there just the same.