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

[OpenAPI 3.1] Example value is not generated for nested objects that are referenced via allOf

Open hkosova opened this issue 1 year ago • 6 comments

Q&A (please complete the following information)

  • OS: macOS 13.6.7
  • Browser: Chrome latest
  • Method of installation: standalone Swagger UI (5.17.9), also https://editor-next.swagger.io (UI v. 5.17.3)
  • Swagger-UI version: 5.17.9, 5.17.3
  • Swagger/OpenAPI version: OpenAPI 3.1 only

Ref https://github.com/swagger-api/swagger-js/issues/3520

Content & configuration

Example Swagger/OpenAPI definition:

openapi: 3.1.0
info:
  title: Example generation test
  version: 1.0.0
paths:
  /foo:
    get:
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Foo'

components:
  schemas:
    Foo:
      type: object
      properties:
        bar:
          allOf:
            - $ref: '#/components/schemas/Bar'
    
    Bar:
      type: object
      properties:
        a:
          type: integer
          example: 123
        b:
          type: string
          example: hello

Describe the bug you're encountering

The issue may (or may not) be related to #8374.

On the "Example Value" tab, the bar value appears empty object "bar": {} instead of an actual object generated from its schema "bar": {"a": 123, "b": "hello"}. This happens for properties that reference their schema using allOf+$ref instead of just $ref.

The property schema on the "Schema" tab appears correctly though, and shows all fields defined in the referenced schema.

The issue exists for OpenAPI 3.1 definitions and does NOT exist for OpenAPI 3.0 and 2.0.

To reproduce...

Steps to reproduce the behavior:

  1. Go to https://editor-next.swagger.io
  2. Paste the definition provided above.
  3. Expand the GET /foo operation and examine the "Example Value".

Actual result:

{
  "bar": {}
}

Expected result:

{
  "bar": {
    "a": 123,
    "b": "hello"
  }
}
  1. Replace openapi: 3.1.0 with openapi: 3.0.0.
  2. Example "Example Value" again.

=> Now the example value is correct and includes the nested fields.

Screenshots

Additional context or thoughts

Looks like the correctness of the Example Value depends on whether the user expands the operation or schema first? If I first fully expand the schema under "Schemas" at the bottom, then the Example Value inside the operation is generated correctly.

weird

hkosova avatar May 15 '24 13:05 hkosova

Hi @hkosova,

Nice to see you here again ;]

We're looking into the issue. Thanks for the report.

char0n avatar May 16 '24 07:05 char0n

I can confirm this is a regression in Swagger Client.

This works properly in Swagger UI version 5.15.2 and stops working in 5.16.0. We updated the Swagger Client version from 3.26.8 to 3.27.0 and there were changes to dereferencing: https://github.com/swagger-api/swagger-js/pull/3472, so it looks like the regression was caused by that.

glowcloud avatar May 16 '24 07:05 glowcloud

@glowcloud can you constructor minimal reproducibe fixture with expected result and current result?

char0n avatar May 16 '24 07:05 char0n

Looks like it can be reproduced without the reference inside of allOf:

openapi: 3.1.0
info:
  title: Example generation test
  version: 1.0.0
paths:
  /foo:
    get:
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Foo'
components:
  schemas:
    Foo:
      type: object
      properties:
        bar:
          allOf:
            - type: object
              properties:
                a:
                  type: integer

Here's the schema inside of response with the allOf keyword: Screenshot 2024-05-16 at 10 01 44

and under schemas: Screenshot 2024-05-16 at 09 59 45

glowcloud avatar May 16 '24 08:05 glowcloud

Associated upstream swagger-js issue has been created in https://github.com/swagger-api/swagger-js/issues/3520

char0n avatar May 17 '24 10:05 char0n

We're encountering the same issue with our documentation. This is a high priority matter as a significant portion of our documentation is affected.

dominikforal avatar May 17 '24 10:05 dominikforal

We already have an upstream regression remediation in https://github.com/swagger-api/swagger-js/pull/3521

ETA in SwaggerUI during today.

char0n avatar May 21 '24 06:05 char0n

Upstream fix released in [email protected]

char0n avatar May 21 '24 10:05 char0n

Addressed for SwaggerUI in https://github.com/swagger-api/swagger-ui/pull/9950

char0n avatar May 21 '24 10:05 char0n

Released as https://github.com/swagger-api/swagger-ui/releases/tag/v5.17.12

char0n avatar May 21 '24 13:05 char0n

Thanks for the quick fix @char0n, @glowcloud!

hkosova avatar May 21 '24 14:05 hkosova