-
Notifications
You must be signed in to change notification settings - Fork 160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bundle behavior feels illogical #127
Comments
The bundle command replaces one You can structure your |
I appreciate your feedback.
Yep, that's the point. I'm not surprised it's intentional to you - the author 😜. It doesn't feel clean or logical to me because it's not how one would normally structure a Swagger file if you were to write it yourself (you would use the But never mind, I will get used to them.
You mean an extra level of indirection? Like so? paths:
/foo:
get:
summary: Find foos
parameters:
- $ref: '#/parameters/Page'
parameters:
Page:
# how to $ref: 'shared.yaml#/parameters/Page' here? |
Some more feedback after experimenting with The bundler's approach can produce ExampleA simplified example. Original OpenAPI PersonData:
description: A generic person structure...
type: object
properties:
metadata:
type: array
items:
$ref: '../../../../technical-definitions/common-types/v1/common-types-model.yaml#/definitions/Metadata'
AddressData:
description: A postal address.
type: object
properties:
metadata:
type: array
items:
$ref: '../../../../technical-definitions/common-types/v1/common-types-model.yaml#/definitions/Metadata' Bundled OpenAPI PersonData:
description: A generic person structure...
type: object
properties:
metadata:
type: array
items:
description: 'long description here'
type: object
properties:
...
AddressData:
description: A postal address.
type: object
properties:
metadata:
type: array
items:
$ref: '#/definitions/PersonData/properties/metadata/items' Code public class AddressData {
@JsonProperty("metadata")
private List<PersonDatapropertiesmetadataitems> metadata = null; Yet, it doesn't generate a class My conclusion: even if the Update * Update * Update |
@marcelstoer could you please share your solution? The current behavior is indeed not good for generating code. |
@JamesMessinger : This is confusing. It should replace the first Currently, I am facing an issue where the first Also, what happens to I have the following structure:
Because of the issue explained in [A],
In the end, I cannot dereference the How is this going to work? |
We have the same issues here, the first occurence of a reference get's embedded whereas I would like it to be created as a separate model schema and have all references point to this schema. The current approach breaks our code generation tools and means that we can not use the bundle command. |
Maybe I am a bit late, but I would also appreciate if you could share your solution to this, @marcelstoer. Specifically when you said you were |
@lehphyro @rdccosmo here's a gist: https://gist.github.com/marcelstoer/750739c6e3b357872f953469ac7dd7ad Take note of the caveats in its comments section. As stated in earlier comments above I needed all external |
thank you @marcelstoer. I'm still reading and experimenting with and trying to understand your function. If I am getting it right, if I want to stop getting $refs with indirections (~1) I should put the model I want to reference in a shared.yaml and reference the model throught it, eg. $ref: './shared.yaml#/definitions/MyModel'? |
I have also written my own code to fix this. It is huge and cumbersome but it does work for my requirements. Now, it dereferences the first $ref to the full structure. And every subsequent $ref to the same object is replaced by a $ref to the first $ref which has been expanded. |
can you please share your solution @gnongsiej |
To anyone commenting here: seems Stoplight/@philsturgeon recently joined forces with this project and contributed changes to json-schema-ref-parser. I didn't test whether the behavior discussed here is still the same. |
@marcelstoer we have written functionality that creates the sort of bundle you want, it's built into the whole Stoplight ecosystem: Platform, Explorer, Studio, etc. Whilst we do help maintain a few repos for APIDevTools, most of our focus has been on json-schema-ref-parser as we use it, but swagger-parser not so much. I'm doing what I can, but not actively developing complex functionality. Anyway, the functionality for tidy bundling is hidden in a fork we quickly put together as we were struggling to get changes upstream at the time, and I've asked the developers to either a) document the functionality in the fork, or b) merge the changes upstream. You can keep track that over here. stoplightio/json-schema-ref-parser#27 |
@philsturgeon I couldn't figure out how to get the fork to work. I added to yarn resolutions, but it seems that |
The obvious -- if annoying -- workaround is to put components first, and reference everything there. Example inputexample.yml openapi: "3.0.3"
components: # reference everything here
schemas:
a: { $ref: "./schema/a.yml" }
b: { $ref: "./schema/b.yml" }
info:
title: Example
version: 0.0.0
paths:
/a:
get:
responses:
"200":
content:
application/json:
schema: { $ref: "./schema/a.yml" }
description: Success
/b:
get:
responses:
"200":
content:
application/json:
schema: { $ref: "./schema/b.yml" }
description: Success schema/a.yml properties:
aName: { type: string }
b: { $ref: "./b.yml" } schema/b.yml properties:
bName: { type: string } Example outputswagger-cli bundle example.yml yields openapi: 3.0.3
components:
schemas:
a:
properties:
aName:
type: string
b:
$ref: '#/components/schemas/b'
b:
properties:
bName:
type: string
info:
title: Example
version: 0.0.0
paths:
/a:
get:
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/a'
description: Success
/b:
get:
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/b'
description: Success |
@francismijares : Apologies. I do not check this profile as often as I would like. Also, since I did write this solution in a professional capacity, I am not at liberty to share the exact solution. But if you give me some time, I can write a blog post about the algorithm I used. |
I think you could do some preprocessing before bundling to setup @pauldraper' workaround so you don't have to do it manually... It would be amazing if the |
Hey, the bundle method in Redocly looks like this, which I'm pretty sure is what you're looking for?
If so, go grab redocly-cli and we'll let swagger-parser die in peace. |
Agreed, I recently switched to redocly-cli and it works beautifully. |
I only recently learned about the
bundle
command and it promised to be exactly what I was looking for. After taking it for a test ride I'm left with mixed feeling. There's some behavior that feels illogical to me. However, there may be perfectly sensible explanations for what I'm seeing.API file
shared.yaml
The content of the file shared across multiple APIs contains the expected stuff. Snippet:
Bundled result
I run
swagger-cli bundle --outfile bundle.yaml --type yaml api.yaml && swagger-cli validate bundle.yaml
and verify the output.bundle.yaml
is reported to be valid, that's cool.#/responses
section in the output from theshared.yaml#/responses
references the bundler dereferenced the response once and then points from a response in one path to the response in the other$ref: '#/paths/~1bar/get/responses/400'
. It's certainly valid OpenAPI but it doesn't feel clean to me.$ref
ed path parameters are dereferenced inline in thepaths:/foo:get:parameters:
section rather than just bundled. I guess it's actually the same pattern as above. I was expectingshared.yaml#/parameters/Page
to lead to a localparameters:
section.The text was updated successfully, but these errors were encountered: