Skip to content

Commit

Permalink
Proposal revision v1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
czechboy0 committed Dec 17, 2024
1 parent ae13b02 commit 73fd986
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Introduce an alternative naming strategy for more idiomatic Swift identifiers, i
- v1.0 (2024-11-27): Initial version
- v1.1 (2024-11-28): Also handle "/", "{", and "}" for better synthesized operation names
- v1.2 (2024-12-10): Treat "/" as a word separator and improve handling of the lowercasing of acronyms at the start of an identifier.
- v1.3 (2024-12-17): Add "+" as a word separator, clarify that defensive strategy always post-processes the output of the idiomatic strategy, clarify that content type names also change with the idiomatic strategy
- Issues:
- [apple/swift-openapi-generator#112][issuePlugin]
- [apple/swift-openapi-generator#107][issue1]
Expand Down Expand Up @@ -54,11 +55,13 @@ Our priority was to support as many valid OpenAPI documents as possible. However

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 "idiomatic" naming strategy. The names reflect the strengths of each strategy - the defensive strategy can handle any OpenAPI document and produce compiling Swift code, the idiomatic 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.
For clarity, we'll refer to the existing naming strategy as the "defensive" naming strategy, and to the new proposed strategy as the "idiomatic" naming strategy. The names reflect the strengths of each strategy - the defensive strategy can handle any OpenAPI document and produce compiling Swift code, the idiomatic naming strategy produces prettier names, but does not work for all documents.

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.

This strategy will fall back to the defensive strategy if it encounters any invalid symbols in the identifier.
This strategy will pre-process the input string, and then still apply the defensive strategy on the output, to handle any illegal characters that are no explicitly handled by the idiomatic strategy.

The configurable naming strategy will affect not only general names from the OpenAPI document ([SOAR-0001][soar0001]), but also content type names ([SOAR-0002][soar0002]).

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.

Expand All @@ -80,9 +83,10 @@ To get a sense for the proposed change, check out the table below that compares
| `__user` | `__user` | `__User` | `__user` |
| `get/pets/{petId}` _Changed in v1.2_ | `get_sol_pets_sol__lcub_petId_rcub_` | `GetPetsPetId` | `getPetsPetId` |
| `HTTPProxy` _Added in v1.2_ | `HTTPProxy` | `HTTPProxy` | `httpProxy` |
| `application/myformat+json` _Added in v1.3_ | `application_myformat_plus_json` | - | `applicationMyformatJson` |
| `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 idiomatic naming strategy falls back to the defensive naming strategy. In all the other cases, however, the resulting names are more idiomatic Swift identifiers.
Notice that in the last example, since the OpenAPI name contains the pound (`#`) character, the idiomatic naming strategy lets the defensive naming strategy handle the illegal character. 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).
Expand All @@ -104,10 +108,11 @@ The idiomatic naming strategy (check out the current code [here][impl], look for
- spaces (` `, ASCII: `0x20`)
- slashes (`/`, ASCII: `0x2f`) _Added in v1.1, changed meaning in 1.2_
- curly braces (`{` and `}`, ASCII: `0x7b` and `0x7d`) _Added in v1.1_
- pluses (`+`, ASCII: `0x2b`) _Added in v1.3_

> 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 idiomatic naming strategy _falls back_ to the defensive naming strategy for that input string only.
If the OpenAPI name includes any _other_ characters, the idiomatic naming strategy lets the defensive strategy handle those characters.

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

Expand Down Expand Up @@ -155,6 +160,7 @@ With this proposal, we plan to abandon the ["naming extensions" idea][issuePlugi
- 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
[soar0002]: https://swiftpackageindex.com/apple/swift-openapi-generator/documentation/swift-openapi-generator/soar-0002
[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
Expand Down

0 comments on commit 73fd986

Please sign in to comment.