redocly-cli icon indicating copy to clipboard operation
redocly-cli copied to clipboard

Bundling leads to self-referencing component schemas

Open glen-84 opened this issue 4 years ago • 6 comments

Describe the bug

When using the bundle command, the schemas in components.schemas reference themselves, instead of being dereferenced.

Example from output:

components:
  schemas:
    User:
      $ref: '#/components/schemas/User'
    Country:
      $ref: '#/components/schemas/Country'
    Game:
      $ref: '#/components/schemas/Game'

To Reproduce

I don't have a small reproduction, as our API is large (28k lines when bundled using another tool).

I can send the spec on request, via a private channel.

Expected behavior

I expected the component schemas to be dereferenced.

Logs

n/a

OpenAPI definition

(see above)

openapi-cli Version(s)

1.0.0-beta.44

Node.js Version(s)

14.16.1

Additional context

n/a

glen-84 avatar May 05 '21 09:05 glen-84

@glen-84 did you try the --dereferenced option? Besides that, this seems like a bug anyway.

https://redoc.ly/docs/cli/commands/#bundle

openapi bundle --dereferenced

It may be helpful if you can share your definition -- our email is team at redoc.ly (and please include a link to this issue).

adamaltman avatar May 13 '21 11:05 adamaltman

With --dereferenced I get:

Detected circular reference which can't be converted to JSON.

When using YAML, it completes without error, but the schemas seem to reference request and response values. This might be because of the way that our spec is structured: Paths reference separate files, the separate files reference separate files for schemas, and components.schemas references those same schemas. It may be the case that things would work better if components.schemas were bundled first, before paths, but I'm not sure.

I'm using swagger-parser for now, but I've emailed the spec in case it's useful.

Thanks.

glen-84 avatar May 13 '21 18:05 glen-84

I had the same issue. We currently try different ways to split and structure our YAML file.

In our case the problem was that we referenced a file with a list of definitions that also contains references. For example in the main yaml the parameters section contains a reference to the file _index.yaml.

Snippet of main yaml

components:
  schemas:
    $ref: ./models/_index.yaml
  responses:
    $ref: ../common/models/responses.v1.yaml
  parameters:
    $ref: ./parameters/_index.yaml

The _index.yaml contains the list to all parameters with references to the respective parameter definition. In the path definitions we reference to the parameter file e.g. /query-parameters.yaml#/modelId

Snippet of parameters/_index.yaml

modelId
  $ref: './query-parameters.yaml#/modelPKs'
startDate:
  $ref: './query-parameters.yaml#/startDate'
endDate:
  $ref: './query-parameters.yaml#/endDate'

Maybe this helps.

SusanneKrause avatar May 14 '21 07:05 SusanneKrause

@tatomyr I reassigned it to me as I have some progress on it already.

RomanHotsiy avatar Feb 17 '22 09:02 RomanHotsiy

@RomanHotsiy what's the progress so far on this? and how can I help?

mauris avatar Apr 28 '22 07:04 mauris

@mauris I think the issue is with the invalid way to split the YAML file:

components:
  schemas:
    $ref: ./models/_index.yaml
  responses:
    $ref: ../common/models/responses.v1.yaml
  parameters:
    $ref: ./parameters/_index.yaml

See this issue for more details: #658

adamaltman avatar Apr 28 '22 12:04 adamaltman

Should be fixed in v1.0.0-beta.127.

tatomyr avatar May 25 '23 14:05 tatomyr