As far as I could find, there isn't any "automatic" way to do this. But I have found a rather straightforward method:
Step 1: Retrieve the OpenAPI files and parse them into OpenAPI objects.
You can achieve this using Swagger OpenAPIV3Parser
:
Loop the below code for each fileLocation
(OpenAPI YAML file) that you have.
OpenAPI parseOpenAPI(FileLocation fileLocation) throws MojoFailureException {
SwaggerParseResult swaggerParseResult = parser.readLocation(fileLocation.getUri().toString(), null, null);
if (null == swaggerParseResult || null == swaggerParseResult.getOpenAPI()) {
throw new MojoFailureException("Failed to parse the openAPI: " + fileLocation);
}
return swaggerParseResult.getOpenAPI();
}
Step 2: Merge the objects
This step is pretty much as you'd expect, just adding anything I need from the other OpenAPI objects into my main OpenAPI object.
private OpenAPI aggregateOpenAPI(FileLocation mainopenAPI, List<FileLocation> openAPILocations) throws IOException, MojoFailureException {
OpenAPI projectOpenAPI = getOpenAPIFile(mainopenAPI);
if (projectOpenAPI.getComponents() == null) {
projectOpenAPI.setComponents(new Components());
}
for (FileLocation fileLocation: openAPILocations) {
OpenAPI openAPI = getOpenAPIFile(fileLocation);
openAPI.getPaths().forEach(projectOpenAPI.getPaths()::addPathItem);
Components components = openAPI.getComponents();
if (components != null) {
if (components.getParameters() != null) {
components.getParameters().forEach(projectOpenAPI.getComponents()::addParameters);
}
if (components.getSchemas() != null) {
components.getSchemas().forEach(projectOpenAPI.getComponents()::addSchemas);
}
}
}
return projectOpenAPI;
}
Step 3: Parse and write to a file
When I wrote this question, I was missing that Swagger actually provides the ability to parse an OpenAPI object directly into a YAML or JSON file.
String yamlOpenAPI = Yaml.pretty(projectOpenAPI);
// or
String jsonOpenAPI = Json.pretty(projectOpenAPI);
// Write it into a file (make sure directory and file exist)
Files.write(Paths.get("path/to/file.yaml"), yamlOpenAPI.getBytes());
Edit:
Note that you may need to use writeString
instead of write
to support UTF-8
characters like so:
Files.writeString(Paths.get("path/to/file.yaml"), yamlOpenAPI);