Skip to content

Commit 11e94cd

Browse files
authored
[Proposal] SOAR-0001 Improved OpenAPI -> Swift name mapping (#95)
### Motivation Encoding a string would help distinguish types properly. For eg. both a b and a_b would be rendered as a_b. This change would make it render a_space_b and a_b respectively. Adding a proposal as its not a trivial change, as recommended by the contribution guidelines. ### Modifications Added proposal document - `SOAR-0001.md` An initial draft of the proposed implementation is at #89. Any suggestions or improvements for the proposal are welcome.
1 parent ea1a1db commit 11e94cd

File tree

1 file changed

+51
-0
lines changed
  • Sources/swift-openapi-generator/Documentation.docc/Proposals

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# SOAR-0001
2+
3+
Encoding for Property Names
4+
5+
## Overview
6+
7+
- Proposal: SOAR-0001
8+
- Author(s): [Denil](https://github.com/denil-ct)
9+
- Status: **In Preview**
10+
- Issue: https://github.com/apple/swift-openapi-generator/issues/21
11+
- Implementation:
12+
- https://github.com/apple/swift-openapi-generator/pull/89
13+
- Feature flag: `proposal0001`
14+
- Affected components:
15+
- generator
16+
17+
### Introduction
18+
19+
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.
20+
21+
### Motivation
22+
23+
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.
24+
25+
### Proposed solution
26+
27+
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.
28+
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.
29+
30+
Some examples,
31+
32+
yaml | swift
33+
-- | --
34+
a b | a_space_b
35+
a*b | a_ast_b
36+
ab_ | ab_
37+
ab* | ab_ast_
38+
/ab | _sol_ab
39+
Hu&J_?kin | Hu_amp_J__quest_kin
40+
$nake… | \_dollar_nake_x2026\_
41+
message | message
42+
43+
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.
44+
45+
### Detailed design
46+
47+
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.
48+
49+
### API stability
50+
51+
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.

0 commit comments

Comments
 (0)