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.21k stars 87 forks source link

Manual invocation of the generator and SPM warnings #552

Closed groue closed 1 month ago

groue commented 1 month ago

Question

Hello,

I have started manually invoking the generator, due to an Xcode bug (probably the same one as in https://github.com/apple/swift-openapi-generator/issues/376).

In the process, I have:

Other necessary changes - Modified my .gitignore: The Package.resolved that counts is the one in the Xcode project, but the plugin insists on creating his own (and downloading its own copies of dependencies). This is normal, I understand - just a painful side effect of the weird Xcode/SPM integration. Anyway, since I don't want to redownload dependencies afresh each time I run `make MyServiceAPI`, I don't want git to bother me about `.build` and `Package.resolved`: ```diff +Packages/MyServiceAPI/Package.resolved ``` - Modified my `swiftlint.yml`: ```diff excluded: + # Exclude SPM build artefacts + - ./**/.build + # Exclude Generated Sources + - ./**/GeneratedSources ``` At that point, manually running the generator stopped being a big inconvenience.

When I run the generator, the output warns:

$ make MyServiceAPI
...
warning: found 2 file(s) which are unhandled; explicitly declare them as resources or exclude from the target
.../Sources/MyServiceAPI/openapi.yaml
.../Sources/MyServiceAPI/openapi-generator-config.yaml

Those "unhandled" files are the OpenAPI generator input. They are not resources, so I exclude them in my Package.swift, as recommended by the warning:

+exclude: [
+    "openapi.yaml",
+    "openapi-generator-config.yaml",
+]

And now the generator fails:

$ make MyServiceAPI
...
error: Targets ... don't contain any config or OpenAPI document files with expected names. See documentation for details.
make: *** [MyServiceAPI] Error 1

Pick your poison: a warning, or an error 🙂


Note that this is not a blocker 👍

Is this a known issue? Is this a bug in the generator plugin?

czechboy0 commented 1 month ago

Hi @groue,

yeah we've seen this before, which Xcode toolchain are you on? Can you try on the recently released 5.10, does it reproduce there? (And bump the swift-tools-version to 5.10 as well)?

One way to get rid of the warnings is to invoke the CLI manually, not even using the command plugin: https://swiftpackageindex.com/apple/swift-openapi-generator/1.2.1/documentation/swift-openapi-generator/manually-invoking-the-generator-cli#Invoke-the-CLI-manually

czechboy0 commented 1 month ago

I think this was fixed here: https://github.com/apple/swift-package-manager/pull/7300

groue commented 1 month ago

Hi @czechboy0 :-)

I'm on the latest Xcode 15.3, swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4).

One way to get rid of the warnings is to invoke the CLI manually

Oh yes, it works (I can exclude the YAML files in Package.swift without errors) 👍

OK, this solves the issue for me. I will opt-in for the warning because I hope that #376 will be fixed eventually and that I can get back to the automatic generation. Meanwhile, I'd rather keep openapi-generator-config.yaml as the source of truth.

Thank you for your support!

czechboy0 commented 1 month ago

Meanwhile, I'd rather keep openapi-generator-config.yaml as the source of truth.

You can continue to do that, by passing the path to the config file to the CLI - that's what the plugin does under the hood. No need to specify inputs using CLI options.

groue commented 1 month ago

Oh, you're right, with the --config option:

swift run swift-openapi-generator generate \
  --output-directory Sources/MyTarget/GeneratedSources \
  --config Sources/MyTarget/openapi-generator-config.yaml \
  Sources/MyTarget/openapi.yaml

Now I can exclude the yaml files from Package.swift.

Thanks, this is just perfect now 💯