Skip to content

Commit

Permalink
Default the access modifier to internal (#441)
Browse files Browse the repository at this point in the history
### Motivation

After further consideration, `internal` seems the be the right default
for access modifier of the generated, mirroring Swift itself.

### Modifications

Switch the default to `internal` and update docs.

### Result

`accessModifier: internal` is the default now.

### Test Plan

Updated tests.
  • Loading branch information
czechboy0 authored Dec 8, 2023
1 parent ca0e344 commit 4a3c485
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 39 deletions.
5 changes: 3 additions & 2 deletions IntegrationTest/Sources/Client/openapi-generator-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
generate:
- client
- client
additionalImports:
- Types
- Types
accessModifier: package
5 changes: 3 additions & 2 deletions IntegrationTest/Sources/Server/openapi-generator-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
generate:
- server
- server
additionalImports:
- Types
- Types
accessModifier: package
3 changes: 2 additions & 1 deletion IntegrationTest/Sources/Types/openapi-generator-config.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
generate:
- types
- types
accessModifier: package
2 changes: 1 addition & 1 deletion Sources/_OpenAPIGeneratorCore/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public struct Config: Sendable {
public var access: AccessModifier

/// The default access modifier.
public static let defaultAccessModifier: AccessModifier = .package
public static let defaultAccessModifier: AccessModifier = .internal

/// Additional imports to add to each generated file.
public var additionalImports: [String]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ The configuration file has the following keys:
- `server`: Server code that can be used with any server transport (depends on code from `types`).
- `accessModifier` (optional): a string. Customizes the visibility of the API of the generated code.
- `public`: Generated API is accessible from other modules and other packages (if included in a product).
- `package` (default): Generated API is accessible from other modules within the same package or project.
- `internal`: Generated API is accessible from the containing module only.
- `package`: Generated API is accessible from other modules within the same package or project.
- `internal` (default): Generated API is accessible from the containing module only.
- `additionalImports` (optional): array of strings. Each string value is a Swift module name. An import statement will be added to the generated source files for each module.
- `filter`: (optional): Filters to apply to the OpenAPI document before generation.
- `operations`: Operations with these operation IDs will be included in the filter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,18 @@
}
}
}

@Section(title: "(Optional) Creating a new Xcode target") {
In an existing Xcode project that already contains an app target, create a new Xcode target for the generated client.

While this isn't required and you can generate the client Swift code into your app target, it is recommended to keep the generated code in a separate framework to avoid potential file and type name conflicts.
@Steps {
@Step {
In the Project Navigator, click on the project.
}
@Step {
In the Project Editor, click the plus button at the bottom of the list titled Targets.
}
@Step {
Select the Framework template, click Next.
}
@Step {
Give the framework a name, for example "GeneratedClient", make sure the framework gets embedded in your app, and click Finish.
}
}
}

@Section(title: "Configuring your target to use the Swift OpenAPI Generator plugin") {

Let's extend this sample package to call our `GreetingService` API.
Let's extend this app to call our `GreetingService` API.

We will generate the client code into an Xcode target created in the previous step, called "GeneratedClient". You can generate the code into any target in your project.
We will generate the client code into your existing Xcode app target, for example called "GreetingServiceClient". Note that you can generate the code into any target in your project, and in larger projects, it can be helpful to generate the code into a dedicated framework or library.

@Steps {
@Step {
Add the two configuration files required by the Swift OpenAPI Generator build plugin.

The first is the OpenAPI document. Add it to to the "GeneratedClient" target by right-clicking on the "GeneratedClient" folder in the project navigator, and choosing Add Files to "GeneratedClient"…
The first is the OpenAPI document. Add it to to the "GreetingServiceClient" target by right-clicking on the "GreetingServiceClient" folder in the project navigator, and choosing Add Files to "GreetingServiceClient"…
@Code(name: "Sources/openapi.yaml", file: client.openapi.yaml)
}
@Step {
Expand Down Expand Up @@ -101,17 +81,17 @@
@Step {
Repeat the same steps two more times, with the packages `https://github.com/apple/swift-openapi-runtime` and `https://github.com/apple/swift-openapi-urlsession`.

This time, do check the library products to be added to the **GeneratedClient target**. Note, this might not be the default target Xcode offers to add the libraries to.
This time, do check the library products to be added to the **GreetingServiceClient target**. Note, this might not be the default target Xcode offers to add the libraries to.
}
@Step {
To finish configuring the build plugin in your target, navigate to the Build Phases tab of the GeneratedClient in the Project Editor, and expand the Run Build Tool Plug-ins section.
To finish configuring the build plugin in your target, navigate to the Build Phases tab of the GreetingServiceClient in the Project Editor, and expand the Run Build Tool Plug-ins section.

Click the plus button and add the OpenAPIGenerator plugin.
}
@Step {
To verify everything is configured correctly, choose Product -> Build. If this is the first time using the plugin, you will be asked for confirmation that you trust the plugin. To continue, click Trust & Enable All.

Xcode now builds the Swift OpenAPI Generator plugin itself, and then runs it on the configuration files `openapi.yaml` and `openapi-generator-config.yaml` to generate a Swift client for GreetingService. Once it finishes, the `Client` type will become available in the GeneratedClient target.
Xcode now builds the Swift OpenAPI Generator plugin itself, and then runs it on the configuration files `openapi.yaml` and `openapi-generator-config.yaml` to generate a Swift client for GreetingService. Once it finishes, the `Client` type will become available in the GreetingServiceClient target.
}
}
}
Expand All @@ -123,7 +103,7 @@

@Steps {
@Step {
Create a new Swift file in the GeneratedClient framework called `GreetingClient.swift`.
Create a new Swift file in the GreetingServiceClient app target called `GreetingClient.swift`.

Import the OpenAPIURLSession library, which provides a transport implementation that uses Foundation's URLSession to perform network calls.

Expand Down Expand Up @@ -166,7 +146,7 @@
@Code(name: "GreetingClient.swift", file: client.xcode.6.swift)
}
@Step {
Finally, in your app target, import the framework with the generated client and fetch the personalized greeting, for example to show it in the UI.
Finally, in your app target, integrate the client to fetch the personalized greeting, for example to show it in the UI.

@Code(name: "App.swift", file: client.xcode.7.swift, reset: true)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
import GeneratedClient

let greeting = try await GreetingClient().getGreeting(name: "App")
// Display the greeting text in the UI.
2 changes: 1 addition & 1 deletion Tests/OpenAPIGeneratorCoreTests/Test_Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ import OpenAPIKit
@testable import _OpenAPIGeneratorCore

final class Test_Config: Test_Core {
func testDefaultAccessModifier() { XCTAssertEqual(Config.defaultAccessModifier, .package) }
func testDefaultAccessModifier() { XCTAssertEqual(Config.defaultAccessModifier, .internal) }
}

0 comments on commit 4a3c485

Please sign in to comment.