apple / swift-openapi-generator

Generate Swift client and server code from an OpenAPI document.
https://swiftpackageindex.com/apple/swift-openapi-generator/documentation
Apache License 2.0
1.45k stars 120 forks source link

Need to be able to support multiple OpenAPI specs #581

Closed OurBigAdventure closed 4 months ago

OurBigAdventure commented 4 months ago

Motivation

I have a server project that needs to interface with multiple services with OpenAPI specs. Currently I can't see a way to use more than one spec.

Proposed solution

It would be great if the spec(s) could be either grouped or specified instead of detected. A specific folder or a place to create a list of file names would be ideal.

Alternatives considered

The alternative is to pick the more complex spec and use the generator, then code all other API interfaces by hand. Or, use the OpenAPI CLI generator to just generate the code and skip using this project all together.

Additional information

No response

czechboy0 commented 4 months ago

Hi @OurBigAdventure,

if you need to use more than one OpenAPI doc (either because your service implements the interface of multiple APIs, or because your service needs to talk to multiple upstream services), generate each into a separate Swift module.

For example:

- MyUpstreamService1
  - openapi.yaml
  - openapi-generator-config.yaml (generates a client)
- MyUpstreamService2
  - openapi.yaml
  - openapi-generator-config.yaml (generates a client)
- MyServiceA
  - openapi.yaml
  - openapi-generator-config.yaml (generates a server)
- MyServer
  - depends on and imports MyUpstreamService1, MyUpstreamService2, MyServiceA
  - implements the handler (conforming to `APIProtocol`) and uses the generated `MyUpstreamService1.Client` and `MyUpstreamService2.Client` to make upstream calls

Note that if you use this strategy, make sure to specify accessModifier: package or accessModifier: public, so that the generated code is accessible from other modules (the default is internal).

More details:

OurBigAdventure commented 4 months ago

Interesting, thanks for the great response!