swift-openapi-generator icon indicating copy to clipboard operation
swift-openapi-generator copied to clipboard

Swift Package does not generate code when added via Swift Package Manager to Xcode Project

Open TheCoderateKid opened this issue 9 months ago • 4 comments

Question

So I can run and generate the code when i have the swift package opened as a stand alone. My goal is to have just the open api spec and the required configuration file committed and have the code auto generated when it is added as a package to the main app project via swift package manager. is this possible?? currently it is not running at all

Package.swift

import PackageDescription

let package = Package(
    name: "ClientLibrary",
    platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)],
    dependencies: [
        .package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"),
        .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"),
        .package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.0"),
    ],
    targets: [
        .executableTarget(
            name: "ClientLibrary",
            dependencies: [
                .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"),
                .product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"),
            ],
            plugins: [
                .plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator"),
            ]
        )
    ]
)

configuration yaml


generate:
  - types
  - client
accessModifier: package
namingStrategy: idiomatic

TheCoderateKid avatar Mar 11 '25 01:03 TheCoderateKid

Yes. Automatic generation from the plugin is the usual workflow. Both the openapi.yaml and openapi-generator-config.yaml must exist in the target source directory for which you have configured the plugin in your package manifest.

If you check out the examples directory in this project, almost all use the plugin this way. The hello-world-urlsession-client is probably a good place to start. Maybe see if that works for you as-is, running swift build.

simonjbeaumont avatar Mar 11 '25 06:03 simonjbeaumont

when it is added as a package to the main app project via swift package manager

Wait, this sounds like you're asking about using this from an Xcode project? If so, yes, that's possible but you configure the plugin differently. We have an example of this in the repo too, though. Here's a guided tutorial0 and an example project1.

simonjbeaumont avatar Mar 11 '25 10:03 simonjbeaumont

i was able to get this to build as a swift package imported via SPM in my app project using this and removing the executableTarget. The examples are kind of useless if it is not providing a real world example.

import PackageDescription

let package = Package(
    name: "ClientLibrary",
    platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)],
    products: [
        .library(name: "ClientLibrary", targets: ["ClientLibrary"])
    ],
    dependencies: [
        .package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"),
        .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"),
        .package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.0"),
    ],
    targets: [
        .target(
            name: "ClientLibrary",
            dependencies: [
                .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"),
                .product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"),
            ],
            plugins: [
                .plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator"),
            ]
        )
    ]
)

TheCoderateKid avatar Mar 12 '25 02:03 TheCoderateKid

Glad you managed to get it working. What example or documentation was missing that would have helped you reach this solution sooner?

simonjbeaumont avatar Mar 12 '25 05:03 simonjbeaumont