swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.03k stars 6.03k forks source link

How to generate common classes to a separate package and then reuse them #10852

Open eakonovalov opened 3 years ago

eakonovalov commented 3 years ago

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.

Kobee1203 commented 2 years ago

Have you found a solution ?

riju4git commented 1 year ago

@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.

Demonian commented 1 year ago

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:

  1. Use OpenAPI Spec 3 $ref with Remote reference for all your common classes in multiple specification
    
    # 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"

bounded-context2-spec.yaml

openapi: 3.0.3 info: title: OpenAPI 3 spec example for bounded context 2 version: 1.0.0 paths: /test2: get: tags:

In case of additional questions do not hesitate to ask me

BTW I believe the issue could be closed for now

gturi commented 9 months ago

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.

cuttingclyde commented 1 month ago

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.