swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

Gibberish shown in example when pattern is used on a field and no defaultValue is provided

Open ZIRAKrezovic opened this issue 2 years ago • 1 comments

Q&A (please complete the following information)

  • OS: Windows
  • Browser: Chrome
  • Version: 119
  • Method of installation: WebJar (*)
  • Swagger-UI version: 5.10.3
  • Swagger/OpenAPI version: OpenAPI 3

(*) WebJar downloads a distribution from github and copies over your dist folder, see https://github.com/webjars/swagger-ui/blob/master/pom.xml#L22 and https://github.com/webjars/swagger-ui/blob/master/pom.xml#L110

Content & configuration

Example Swagger/OpenAPI definition:

openapi: 3.0.1
info:
  title: OpenAPI definition
  version: v0
servers:
- url: http://localhost:8080
  description: Generated server url
tags:
- name: user-v1
  description: User v1 API
paths:
  /users/v1:
    get:
      tags:
      - user-v1
      summary: Find Users based on filter criteria
      operationId: get
      responses:
        "200":
          description: OK
          content:
            '*/*':
              schema:
                $ref: '#/components/schemas/User'
components:
  schemas:
    User:
      required:
      - email
      - username
      type: object
      properties:
        username:
          pattern: "^(?=.{4,256}$)(?:[a-zA-Z\\d]+(?:[.\\-_@][a-zA-Z\\d])*)+$"
          type: string
          description: User name of a given user
        email:
          pattern: "^[\\w-.]+@([\\w-]+\\.)+[\\w-]{2,4}$"
          type: string
          description: E-mail of a given user
      description: Properties for creation of a user

Swagger-UI configuration options:

window.onload = function() {
  //<editor-fold desc="Changeable Configuration Block">

  // the following lines will be replaced by docker/configurator, when it runs in a docker-container
  window.ui = SwaggerUIBundle({
    url: "https://petstore.swagger.io/v2/swagger.json",
    dom_id: '#swagger-ui',
    deepLinking: true,
    presets: [
      SwaggerUIBundle.presets.apis,
      SwaggerUIStandalonePreset
    ],
    plugins: [
      SwaggerUIBundle.plugins.DownloadUrl
    ],
    layout: "StandaloneLayout" ,

  "configUrl" : "/v3/api-docs/swagger-config",
  "validatorUrl" : ""

  });

  //</editor-fold>
};

Not sure what this is, but I have:

http://localhost:8080/swagger-ui/index.html#/user-v1/get

Describe the bug you're encountering

When a pattern is added to a schema property, and no default value is provided, the example shown in swagger ui is literal gibberish.

To reproduce...

Steps to reproduce the behavior: The schema is provided as-is. I have first encountered this using a spring-boot application that I put at

https://github.com/ZIRAKrezovic/openapi-report

Requires Java 17

Build and run with:

./mvnw clean install spring-boot:run

Navigate to http://localhost:8080/swagger-ui/index.html#/user-v1/get

Expected behavior

I would expect a sane example value is shown on the UI

Screenshots

image

Additional context or thoughts

ZIRAKrezovic avatar Dec 04 '23 09:12 ZIRAKrezovic

Related: #8297

glennmatthews avatar Dec 21 '23 14:12 glennmatthews