Open eakonovalov opened 3 years ago
Have you found a solution ?
@eakonovalov Hi, have you found a solution for this ? We are facing a similar issue but I didn't find a way to resolve it yet.
JFYI I have found the way how to do this, so in case anybody will search for solution in future this could help. The idea here is the combination of needed actions:
# Folder structure
|── src
│ |── main
│ │ |── resources
│ | | |── bounded-context1-spec.yaml
| | | |── bounded-context2-spec.yaml
| | | |── common-classes.yaml
# bounded-context1-spec.yaml
openapi: 3.0.3
info:
title: OpenAPI 3 spec example for bounded context 1
version: 1.0.0
paths:
/test:
get:
tags:
- test
responses:
'200':
description: Success
content:
application/json:
schema:
$ref: "common-classes.yaml#/components/schemas/Test"
openapi: 3.0.3 info: title: OpenAPI 3 spec example for bounded context 2 version: 1.0.0 paths: /test2: get: tags:
openapi: 3.0.3 info: title: Common Classes which are used by both bounded contexts version: 1.0.0 paths: {} components: schemas: Test: type: object properties: message: type: string
2. Additional execution step for **swagger-codegen-plugin** which is configured to generate only Models. (classes will be generated to _com.test.common.model_ package)
...
...
3. Add **importMappings** for common models into your main execution steps. This configuration will disable generation of common classes for specific bounded context and reuse classes generated in above step.
...
...
4. Profit 🙂
In case of additional questions do not hesitate to ask me
BTW I believe the issue could be closed for now
Hi, even though it works, in my opinion the solution is not ideal, because you need to explicitly declare the mapping for each class you want to be "shared". It would be better to have an option to let the plugin know that every class imported from common-classes.yaml remote reference should use the common package instead.
I agree with @gturi , OpenAPI specification supports $ref: to reuse common classes, the codegen should allow you to declare that those should be respected. In case of any ambiguities or name conflicts in a particular set of files, then the importMappings or other flags can individually specify whether to reuse or generate new.
I can define common classes in a separate schema file:
common.json { ... "components": { "schemas": { "FinancialContext": { ... } } }
and then reuse it in many other schemas: "financialContext": "$ref": "common.json#/components/schemas/FinancialContext" }
but when I generate models using swagger-codegen-maven-plugin this class will be duplicated in each package for each schema (I cannot generate all the classes to the same package because they can have other classes with the same name, but different content).
I want to be able to generate common.json to a separate package (this is actually possible now) and then all other executions of the plugin should import already generated classes from the common package instead of generating copies of the class in the apis packages.