SOAR-0001: Improved mapping of identifiers
Improved mapping of OpenAPI identifiers to Swift identifiers.
Overview
Proposal: SOAR-0001
Author(s): Denil Chungath
Status: Implemented (0.2.0)
Issue: https://github.com/apple/swift-openapi-generator/issues/21
Implementation: https://github.com/apple/swift-openapi-generator/pull/89
Review: (review)
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. 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.