-
Notifications
You must be signed in to change notification settings - Fork 129
[Proposal] SOAR-0001 Improved OpenAPI -> Swift name mapping #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
Sources/swift-openapi-generator/Documentation.docc/Proposals/SOAR-0001.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# SOAR-0001 | ||
|
||
Encoding for Property Names | ||
|
||
## Overview | ||
|
||
- Proposal: SOAR-0001 | ||
- Author(s): [Denil](https://github.com/denil-ct) | ||
- Status: **In Preview** | ||
- Issue: https://github.com/apple/swift-openapi-generator/issues/21 | ||
- Implementation: | ||
- https://github.com/apple/swift-openapi-generator/pull/89 | ||
- Feature flag: `proposal0001` | ||
- Affected components: | ||
- generator | ||
|
||
### Introduction | ||
|
||
The goal of this proposal is to improve the way we handle unsupported characters in property names when generating code from specs. Currently, we use a block list approach, replacing offending characters with `_` which can cause name conflicts. By encoding the offending character we create unique and valid property names. This will avoid name collisions and ensure consistent code generation. | ||
|
||
### Motivation | ||
|
||
The current approach for handling unsupported characters in property names is not robust and can lead to unexpected and undesirable outcomes. For example, if there are two properties, `a_b` and `a b`, with the current implementation, this will result in the same generated property `a_b` for both, which would create a conflict. It can also result in loss of information or meaning from the original specification. Therefore, we need a better solution that can handle any unsupported character in a consistent and reliable way, without compromising the quality and functionality of the code. | ||
|
||
### Proposed solution | ||
|
||
The proposed solution to the problem is to use a mix of replacement words and hex encoding for any unsupported character in property names. We replace characters in the printable ASCII range (20-7E) with a wordified representation inspired by the HTML entity names [here](https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references). Hex encoding is a simple and standard way of representing any character as a sequence of hexadecimal digits. For example, the asterisk (*) character is encoded as 2A, the space ( ) character is encoded as 20, and the slash (/) character is encoded as 2F. Hex encoding also has the added benefit of not introducing any additional special characters. | ||
In addition to this, we will be prefixing the hex codes with an `x` to indicate they are hex values. There are also delimiters added in the form of the underscore character to indicate a possible replacement. | ||
|
||
Some examples, | ||
|
||
yaml | swift | ||
-- | -- | ||
a b | a_space_b | ||
a*b | a_ast_b | ||
ab_ | ab_ | ||
ab* | ab_ast_ | ||
/ab | _sol_ab | ||
Hu&J_?kin | Hu_amp_J__quest_kin | ||
$nake… | \_dollar_nake_x2026\_ | ||
message | message | ||
|
||
This would mean, that for the users of the generator, a future version of the generator might produce different names that what it currently produces right now and should be ready to make those changes before upgrading to this version. | ||
|
||
### Detailed design | ||
|
||
The implementation for this is quite simple as you can see in https://github.com/apple/swift-openapi-generator/pull/89, we just made changes to the substitution logic where it used to substitute with `_`. We have added an additional encoding step to the special character before substituting it. Contributors should be aware of this change and should review the places where they use this extension and evaluate if its suitable for them with this change. | ||
|
||
### API stability | ||
|
||
This is an API breaking change, as it will produce different symbol names than before. Other components such as the runtime and transports should not have any impacts. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.