Why does Swagger UI generate string examples as "StringStri" instead of "string"?

Viewed 804

We are getting "stringstri" instead of "string" type in OpenAPI preview in Swagger UI.

Issue:

  • when minLength is 7 - we get "strings" in preview
  • when minLength is 8 - we get "stringst" in preview
  • when minLength is 9 - we get "stringstr" in preview

Similary it continues.

Expected - only "string".

Sample schema to reproduce the issue:

components:
 schemas:
   Manufacturer:
      required:
        - name
      properties:
        name:
          type: string
          minLength: 10
          maxLength: 2048

Swagger UI displays the following example for this schema:

{
  "id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
  "name": "Widget Adapter",
  "releaseDate": "2021-05-17T06:52:27.542Z",
  "manufacturer": {
    "name": "stringstri",
    "homePage": "string",
    "phone": "string"
  }
}
1 Answers

The name property has minLength: 10 that's why the example value is generated as 10 characters long. The value "string" is only 6 character long so it's not a valid example for a property with minLength >= 7, that's why Swagger UI pads the value in this case.

You can provide your own example value if you want:

      properties:
        name:
          type: string
          minLength: 10
          maxLength: 2048
          example: Acme Corporation  # <-----
Related