Redocly/openapi-sampler

Generated examples are not valid when using regular expression patterns

Orest-Yastremskyy opened this issue · 2 comments

Generated examples are not valid when using regular expression patterns.
In this case, the example is simply "string" without honoring the regular expression.

Steps to replicate:

  1. OA as an example:
openapi: 3.0.3
info:
  version: 1.0.0
  title: Test
  description: Test
servers:
  - url: "/api/1"
paths:
  /test:
    get:
      operationId: Test
      summary: Test
      description: Test
      responses:
        "200":
          description: Test
          content:
            application/json:
              schema:
                type: object
                properties:
                  test:
                    type: string
                    pattern: "^[0-9]{5,8}$"

Observe the example rendered as {"test": "string"} that is not valid against the schema.

image

Original issue: Redocly/redoc#2312

Added to RapiDoc just now: https://github.com/rapi-doc/RapiDoc/pull/947/files#diff-82d61daee06cfa4e64b7d6351db4581c12a0b04c8d7f6f5960f970b8d0835c95R268-R274

Should be doable in ReDoc too, just need to pass in the schema.pattern here:

return sampler(schema.minLength | 0, schema.maxLength, propertyName);

Will post PR shortly.

OK, just proposed a solution in #153. BTW if ignoring minLength / maxLength constraints is acceptable then there is a one-liner solution.