Skip to content

Commit

Permalink
Updated draft proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
czechboy0 committed Nov 22, 2024
1 parent d9b901e commit 3ded3fe
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ If you have any questions, tag [Honza Dvorsky](https://github.com/czechboy0) or
- <doc:SOAR-0010>
- <doc:SOAR-0011>
- <doc:SOAR-0012>
- <doc:SOAR-0013>
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# SOAR-0013: Optimistic naming strategy

Introduce an alternative naming strategy for more idiomatic Swift identifiers, including a way to provide custom name overrides.

## Overview

- Proposal: SOAR-0013
- Author(s): [Honza Dvorsky](https://github.com/czechboy0), [Si Beaumont](https://github.com/simonjbeaumont)
- Status: **Awaiting Review**
- Issues:
- [apple/swift-openapi-generator#112][issuePlugin]
- [apple/swift-openapi-generator#107][issue1]
- [apple/swift-openapi-generator#503][issue2]
- [apple/swift-openapi-generator#244][issue3]
- [apple/swift-openapi-generator#405][issue4]
- Implementation:
- [apple/swift-openapi-generator#679][pr]
- New configuration options:
- `namingStrategy`
- `nameOverrides`
- Affected components:
- generator

### Introduction

Introduce a new naming strategy as an opt-in feature, instructing the generator to produce more conventional Swift names, and offer a way to completely customize how any OpenAPI identifier gets projected to a Swift identifier.

### Motivation

The purpose of Swift OpenAPI Generator is to generate Swift code from OpenAPI documents. As part of that process, names specified in the OpenAPI document have to be converted to names in Swift code - and there are many ways to do that. We call these "naming strategies" in this proposal.

When Swift OpenAPI Generator 0.1.0 went open-source in May 2023, it had a simple naming strategy that produced relatively conventional Swift identifiers from OpenAPI names, however when tested on a large test corpus of around 3000 OpenAPI documents, it produced an unacceptably high number of non-compiling packages due to naming conflicts.

The root cause of conflicts are the different allowed character sets for OpenAPI names and Swift identifiers. OpenAPI has a more flexible allowed character set than Swift identifiers.

In response to the findings on the test corpus, the proposal [SOAR-0001: Improved mapping of identifiers][soar0001], which shipped in 0.2.0, changed the naming strategy to avoid conflicts and resulted in no conflicts produced in the test corpus, allowing hundreds of additional OpenAPI documents to be correctly handled by Swift OpenAPI Generator.

The way the conflicts are avoided in the naming strategy from SOAR-0001 is by turning any special characters (any characters that aren't letters, numbers, or an underscore) into words, resulting in identifiers like:

```
User -> User
User_1 -> User_1
user-name -> user_hyphen_name
my.org.User -> my_period_org_period_User
```

The existing naming strategy also avoids changing the character casing, as we discovered OpenAPI documents with properties within an object schema that only differred by case.

The decision to rely on a naming strategy that can handle all the tested OpenAPI documents was the right one, and it has allowed more developers to get value from Swift OpenAPI Generator since then.

However, we've also [heard][issue1] [from][issue2] [adopters][issue3] [who][issue4] don't use special characters in their OpenAPI documents, and how some of the generated Swift names are still difficult to read and are simply unpleasant to look at.

We originally believed that a fully generalized solution, such as a ["naming extension"][issuePlugin], would be the right way to go, however after further consideration, we believe we can offer a solution that solves the majority of the problem without needing to invent an extension architecture for Swift OpenAPI Generator.

### Proposed solution

We propose to introduce a second, opt-in naming strategy, which produces idiomatic Swift identifiers from arbitrary OpenAPI names, and a way to fully customize the conversion from an OpenAPI name to a Swift identifier using a string -> string map.

For clarity, we'll refer to the existing naming strategy as the "defensive" naming strategy, and to the new proposed strategy as the "optimistic" naming strategy. The names reflect the strengths of each strategy - the defensive strategy can handle any OpenAPI document and produce compiling Swift code, the optimistic naming strategy produces prettier names, but does not work for all documents, and falls back to the defensive strategy when needed on a per-name basis.

Part of the new strategy is adjusting the capitalization, and producing `UpperCamelCase` names for types, and `lowerCamelCase` names for members, as is common in hand-written Swift code.

> Warning: Due to the optimistic naming strategy changing capitalization, it is possible to get non-compiling Swift code from more OpenAPI documents than with the defensive naming strategy. We recommend you try to use the optimistic naming strategy on your OpenAPI document, and if it produces conflicts, switch back to the defensive naming strategy, which avoids conflicts. However, the number of documents that result in conflicts with the optimistic naming strategy is estimated to be very small (<1%).
The second feature introduced as part of this proposal is a way to provide a string -> string map to fully override only specific OpenAPI names and provide their exact Swift identifiers. This is the ultimate escape hatch when both naming strategies fail to provide the desired result for the adopter.

#### Examples

To get a sense for the proposed change, check out the table below that compares the existing defensive strategy against the proposed optimistic strategy on a set of examples:

| OpenAPI name | Defensive | Optimistic (capitalized) | Optimistic (non-capitalized) |
| ------------ | --------- | ------------------------ | ---------------------------- |
| `foo` | `foo` | `Foo` | `foo` |
| `Hello world` | `Hello_space_world` | `HelloWorld` | `helloWorld` |
| `My_URL_value` | `My_URL_value` | `MyURLValue` | `myURLValue` |
| `Retry-After` | `Retry_hyphen_After` | `RetryAfter` | `retryAfter` |
| `NOT_AVAILABLE` | `NOT_AVAILABLE` | `NotAvailable` | `notAvailable` |
| `version 2.0` | `version_space_2_period_0` | `Version2_0` | `version2_0` |
| `naïve café` | `naïve_space_café` | `NaïveCafé` | `naïveCafé` |
| `__user` | `__user` | `__User` | `__user` |
| `order#123` | `order_num_123` | `order_num_123` | `order_num_123` |

Notice that in the last example, since the OpenAPI name contains the pound (`#`) character, the optimistic naming strategy falls back to the defensive naming strategy. In all the other cases, however, the resulting names are more idiomatic Swift identifiers.

> Tip: For more examples, check out the updated [test suite](https://github.com/czechboy0/swift-openapi-generator/blob/hd-naming-strategy-optimistic/Tests/OpenAPIGeneratorCoreTests/Extensions/Test_SwiftSafeNames.swift).
### Detailed design

This section goes into detail of the [draft implementation][pr] that you can already check out and try to run on your OpenAPI document.

> Note: To enable it, you'll need to add `namingStrategy: optimistic` to your `openapi-generator-config.yaml` file.
#### Naming logic

The optimistic naming strategy (check out the current code [here][impl], look for the method `safeForSwiftCode_optimistic`) is built around the decision to _only_ optimize for names that include the following:

- letters
- numbers
- periods (`.`, ASCII: `0x2e`)
- dashes (`-`, ASCII: `0x2d`)
- underscores (`_`, ASCII: `0x5f`)
- spaces (` `, ASCII: `0x20`)

> Note: We let [`Swift.String.isLetter`](https://developer.apple.com/documentation/swift/character/isletter) decide whether a character is a letter, which has the advantage of including letters in the non-ASCII range. Swift identifiers also support a [wide range](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/lexicalstructure/#Identifiers) of alphanumeric characters.
If the OpenAPI name includes any _other_ characters, the optimistic naming strategy _falls back_ to the defensive naming strategy for that input string only.

There's a second special case for handling all uppercased names, such as `NOT_AVAILABLE` - if this situation is detected, the optimistic naming strategy turns it into `NotAvailable` for types and `notAvailable` for members.

The best way to understand the detailed logic is to check out the [code][impl], feel free to leave comments on the pull request.

#### Naming strategy configuration

Since Swift OpenAPI Generator is on a stable 1.x version, we cannot change the naming strategy for everyone, as it would be considered an API break. So this new naming strategy is fully opt-in using a new configuration key called `namingStrategy`, with the following allowed values:

- `defensive`: the existing naming strategy introduced in 0.2.0
- `optimistic`: the new naming strategy proposed here
- not specified: defaults to `defensive` for backwards compatibility

Enabling this feature in the configuration file would look like this:

```yaml
namingStrategy: optimistic
```
#### Name overrides
While the new naming strategy produces much improved Swift names, there are still cases when the adopter knows better how they'd like a specific OpenAPI name be translated to a Swift identifier.
A good examples are the `+1` and `-1` properties in the GitHub OpenAPI document: using both strategies, the names would be `_plus_1` and `_hyphen_1`, respectively. While such names aren't too confusing, the adopter might want to customize them to, for example: `thumbsUp` and `thumbsDown`.

Enabling this feature in the configuration file would look like this:

```yaml
nameOverrides:
'+1': 'thumbsUp'
'-1': 'thumbsDown'
```

### API stability

Both the new naming strategy and name overrides are purely additive, and require the adopter to explicitly opt-in.

### Future directions

With this proposal, we plan to abandon the ["naming extensions" idea][issuePlugin], as we consider the solution in this proposal to solve the name conversion problem for Swift OpenAPI Generator 1.x for all use cases.

### Alternatives considered

- ["Naming extensions"][issuePlugin], however that'd require the community to build and maintain custom naming strategies, and it was not clear that this feature would be possible in SwiftPM using only current features.
- Not changing anything, this was the status quo since 0.2.0, but adopters have made it clear that there is room to improve the naming strategy through the several filed issues linked at the top of the proposal, so we feel that some action here is justified.

[soar0001]: https://swiftpackageindex.com/apple/swift-openapi-generator/documentation/swift-openapi-generator/soar-0001
[issue1]: https://github.com/apple/swift-openapi-generator/issues/107
[issue2]: https://github.com/apple/swift-openapi-generator/issues/503
[issue3]: https://github.com/apple/swift-openapi-generator/issues/244
[issue4]: https://github.com/apple/swift-openapi-generator/issues/405
[issuePlugin]: https://github.com/apple/swift-openapi-generator/issues/112
[pr]: https://github.com/apple/swift-openapi-generator/pull/679
[impl]: https://github.com/czechboy0/swift-openapi-generator/blob/hd-naming-strategy-optimistic/Sources/_OpenAPIGeneratorCore/Translator/CommonTranslations/SwiftSafeNames.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ final class Test_SwiftSafeNames: Test_Core {
// Content type components
("application", "application", "Application", "application"),
("vendor1+json", "vendor1_plus_json", "vendor1_plus_json", "vendor1_plus_json"),


// Known real-world examples
("+1", "_plus_1", "_plus_1", "_plus_1"),
("-1", "_hyphen_1", "_hyphen_1", "_hyphen_1"),

// Override
("MEGA", "m_e_g_a", "m_e_g_a", "m_e_g_a"),
]
Expand Down

0 comments on commit 3ded3fe

Please sign in to comment.