Skip to content

Conversation

@AlexNolasco
Copy link
Contributor

PR checklist

  • Read the contribution guildelines.
  • Ran the shell/batch script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh and ./bin/security/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates)
  • Filed the PR against the correct branch: master for non-breaking changes and 2.3.0 branch for breaking (non-backward compatible) changes.

Description of the PR

The escapeReservedWord method does its job fairly well, but in some cases "reserved words" make some properties look a bit odd in some languages.

The method escapeReservedWord could be assisted through an additional command line parameter (reserved-words-mappings).

In the case of Objective-C, the definition below would create a property named _id which could mistakenly be interpreted as an iVar.

definitions:
  Pet:
    required:
      - id
    properties:
      id:
        type: integer
        format: int64
     name: 
        type: string

which in turn yields

@interface SWGPet : SWGObject
@property(nonatomic) NSNumber* _id;
@property(nonatomic) NSString* name;
@end

The escapeReservedWord method could look up what _id should be substituted with instead.

For example,
java -jar swagger-codegen-cli.jar generate -i pet.yaml -l objc --reserved-words-mappings id=identifier -o output

The new output

@interface SWGPet : SWGObject
@property(nonatomic) NSNumber* identifier;
@property(nonatomic) NSString* name;
@property(nonatomic) NSString* tag;
@end

GoLang can also benefit from this, for example

  Car:
    required:
      - id
      - name
      - range
    properties:
      range:
        type: array
        items:  
          type: integer
      id:
        type: integer
        format: int64
      name:
        type: string

Instead of this

type Car struct {
	Range_ []int32 `json:"range,omit empty"
	Id int64 `json:"id,omit empty"
	Name string `json:"name,omit empty"
}

It could generate

type Car struct {
	Ranges []int32 `json:"range,omit empty"
	Id int64 `json:"id,omit empty"
	Name string `json:"name,omit empty"
}

by using
java -jar swagger-codegen-cli.jar generate -i car.yaml -l go --reserved-words-mappings Range=Ranges -o output

@AlexNolasco AlexNolasco changed the title Issue 4416 Issue 4416 (Assist escapeReservedWord with custom mappings) Jan 3, 2017
@wing328
Copy link
Contributor

wing328 commented Jan 9, 2017

@AlexanderN thanks for the enhancement. I'm sure other Swagger Codegen users will find this new feature useful in customizing the output.

@wing328 wing328 merged commit 26ead9b into swagger-api:master Jan 9, 2017
@AlexNolasco AlexNolasco deleted the issue-4416 branch January 9, 2017 15:24
@wing328 wing328 added this to the v2.2.2 milestone Jan 13, 2017
@wing328 wing328 changed the title Issue 4416 (Assist escapeReservedWord with custom mappings) Assist escapeReservedWord with custom mappings Feb 22, 2017
davidgri pushed a commit to davidgri/swagger-codegen that referenced this pull request May 11, 2017
…api#4480)

* Preliminary implementation for issue-4416

* Updated README.md with  reserved-words-mappings  parameter.
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