-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Feature : merge multiple specification files #1375
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
Comments
Comments welcome! Perhaps this simply becomes a talking point and should really be considered as part of the swagger parser instead. |
It was pointed out in the forum that this messes with IDEs and editors. To address that, one could separate the API into swagger-compliant modules, and build an "import only" file to perform the preprocessing. Perhaps this feature should be moved entirely out of the swagger file? There could be a pure |
Alternative idea: instead of putting the
This would trigger the pre-processor to merge the input files to a temp file, which becomes the effective |
Right, I agree it should be done by the Swagger Parser instead of OpenAPI Generator but good to start the discussion. Have you tried https://github.com/maxdome/swagger-combine (which does not seem to support OAS v3 at the moment)? Is that what you're looking for? |
swagger-combine requires that each input be a complete API. In particular, it does not support one input defining schemas that another input needs, which is the primary use-case of this issue. |
This is really quite a simple transformation -- much simpler in fact than swagger-combine because it operates entirely at the JSON/YAML level and does not need to parse swagger. Perhaps I should make a new project for this? |
json-merger actually comes close, but it requires that operations be injected deep into one of the source documents. |
Actually... json-merger may do everything I need. It doesn't concatenate arrays by default, but it does merge maps. I think that array concatenation may be somewhat contrived. |
I wrote a quick tool to do this recently. I call it openapi-merge. There is a library and an associated CLI tool: In order to use the CLI tool you just write a configuration file and then run {
"inputs": [
{
"inputFile": "./gateway.swagger.json"
},
{
"inputFile": "./jira.swagger.json",
"pathModification": {
"stripStart": "/rest",
"prepend": "/jira"
}
},
{
"inputFile": "./confluence.swagger.json",
"disputePrefix": "Confluence",
"pathModification": {
"prepend": "/confluence"
}
}
],
"output": "./output.swagger.json"
} For more details, see the README on the NPM package. |
Description
While JSON references are useful for certain cases as described in this tutorial, they cannot "include" an entire specification and merge it into the main one. This would be helpful in organizing API specifications into modules, where each module can include a full complement of schemas, paths, etc.
An "import" feature is proposed as a pre-processor before calling the swagger parser, with a section like:
Of course, both YAML and JSON are supported. It would be enabled by a command-line switch
--enable-imports
. If enabled, the pre-processor would read each of the import sources, parse it as JSON, then merge the import JSON objects with the main file. It would write the resulting JSON to a temporary file and hand that to the main engine. This raises the possibility of collisions (same keys defined in multiple sources). Collisions are resoloved using a set of rules:collision_handling
settings controls behavior:error
is the default: an exception is thrownfirst_wins
means that the later definition is ignored and a warning is loggedlast_wins
means that the earlier definition is replaced and a warning is loggedopenapi-generator version
N/A
OpenAPI declaration file content or url
common.json
spec.json
When processed (there are no collisions), would produce:
Note that
ErrorResponse
'sallOf
list has been concatenated, andRequest
'sproperties
object has been merged.Command line used for generation
New option
--enable-imports
would enable this featureSteps to reproduce
Related issues/PRs
None in this repo, but in swagger-parser:
swagger-api/swagger-parser#147
Suggest a fix/enhancement
The text was updated successfully, but these errors were encountered: