Skip to content
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

[Swift] property, parameter name mapping support #16207

Merged
merged 4 commits into from
Jul 30, 2023
Merged

Conversation

wing328
Copy link
Member

@wing328 wing328 commented Jul 28, 2023

  • property, parameter name mapping support

tested locally and the diff looks like:

 internal struct PropertyNameMapping: Codable, JSONEncodable, Hashable {
 
     internal var httpDebugOperation: String?
+    internal var underscoreType: String?
     internal var type: String?
-    internal var type: String?
-    internal var type: String?
-    internal var type: String?
+    internal var typeWithUnderscore: String?
+    internal var dashType: String?
 
-    internal init(httpDebugOperation: String? = nil, type: String? = nil, type: String? = nil, type: String? = nil, type: String? = nil) {
+    internal init(httpDebugOperation: String? = nil, underscoreType: String? = nil, type: String? = nil, typeWithUnderscore: String? = nil, dashType: String? = nil) {
         self.httpDebugOperation = httpDebugOperation
+        self.underscoreType = underscoreType
         self.type = type
-        self.type = type
-        self.type = type
-        self.type = type
+        self.typeWithUnderscore = typeWithUnderscore
+        self.dashType = dashType
     }
 
     internal enum CodingKeys: String, CodingKey, CaseIterable {
         case httpDebugOperation = "http_debug_operation"
-        case type = "_type"
+        case underscoreType = "_type"
         case type
-        case type = "type_"
-        case type = "-type"
+        case typeWithUnderscore = "type_"
+        case dashType = "-type"
     }
 
     // Encodable protocol methods
@@ -39,10 +39,10 @@ internal struct PropertyNameMapping: Codable, JSONEncodable, Hashable {
     internal func encode(to encoder: Encoder) throws {
         var container = encoder.container(keyedBy: CodingKeys.self)
         try container.encodeIfPresent(httpDebugOperation, forKey: .httpDebugOperation)
+        try container.encodeIfPresent(underscoreType, forKey: .underscoreType)
         try container.encodeIfPresent(type, forKey: .type)
-        try container.encodeIfPresent(type, forKey: .type)
-        try container.encodeIfPresent(type, forKey: .type)
-        try container.encodeIfPresent(type, forKey: .type)
+        try container.encodeIfPresent(typeWithUnderscore, forKey: .typeWithUnderscore)
+        try container.encodeIfPresent(dashType, forKey: .dashType)
     }
 }

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    For Windows users, please run the script in Git BASH.
  • In case you are adding a new generator, run the following additional script :
    ./bin/utils/ensure-up-to-date
    
    Commit all changed files.
  • File the PR against the correct branch: master (6.3.0) (minor release - breaking changes with fallbacks), 7.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

FYI @jgavris (2017/07) @ehyche (2017/08) @Edubits (2017/09) @jaz-ah (2017/09) @4brunu (2019/11) @dydus0x14 (2023/06)

Comment on lines 16 to 19
internal var type: String?
internal var type: String?
internal var type: String?
internal var type: String?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wing328 all the properties have the same name, this won't compile

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this won't compile without using the mapping option, e.g.

generatorName: swift5
outputDir: samples/client/petstore/swift5/nonPublicApi
inputSpec: modules/openapi-generator/src/test/resources/2_0/swift/petstore-with-fake-endpoints-models-for-testing.yaml
templateDir: modules/openapi-generator/src/main/resources/swift5
generateAliasAsModel: true
additionalProperties:
  podAuthors: ""
  podSummary: PetstoreClient
  sortParamsByRequiredFlag: false
  nonPublicApi: true
  projectName: PetstoreClient
  podHomepage: https://github.com/openapitools/openapi-generator
nameMappings:
  _type: underscoreType
  type_: typeWithUnderscore
  -type: dashType
parameterNameMappings:
  _type: underscoreType
  type_: typeWithUnderscore
  -type: dashType

I removed the files as these files are not supposed to be included. You can refer to the diff in the description to understand the change before and after using nameMappings, parameterNameMappings in the config.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this is what the schema looks like that will result in naming collisions:

components:
  schemas:
    PropertyNameMapping:
      properties:
        http_debug_operation:
          type: string
        _type:
          type: string
        type:
          type: string
        type_:
          type: string
        -type:
          type: string

@wing328 wing328 merged commit bf18190 into master Jul 30, 2023
12 checks passed
@wing328 wing328 deleted the swift-name-mapping branch July 30, 2023 14:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants