Skip to content

Commit e8929a5

Browse files
fix: handling nested enum correctly in python (#589)
<!-- We appreciate the effort for this pull request but before that please make sure you read the contribution guidelines, then fill out the blanks below. Please format the PR title appropriately based on the type of change: <type>[!]: <description> Where <type> is one of: docs, chore, feat, fix, test, misc. Add a '!' after the type for breaking changes (e.g. feat!: new breaking feature). **All third-party contributors acknowledge that any contributions they provide will be made under the same open-source license that the open-source project is provided under.** Please enter each Issue number you are resolving in your PR after one of the following words [Fixes, Closes, Resolves]. This will auto-link these issues and close them when this PR is merged! e.g. Fixes #1 Closes #2 --> # Fixes # Earlier in case of Enums inside nested classes, the enums were treated as class. This fix makes sure .to_dict() is added only when the object is no way an enum. ### Checklist - [x] I acknowledge that all my contributions will be made under the project's license - [ ] Run `make test-docker` - [ ] Verify affected language: - [ ] Generate [twilio-go](https://github.com/twilio/twilio-go) from our [OpenAPI specification](https://github.com/twilio/twilio-oai) using the [build_twilio_go.py](./examples/build_twilio_go.py) using `python examples/build_twilio_go.py path/to/twilio-oai/spec/yaml path/to/twilio-go` and inspect the diff - [ ] Run `make test` in `twilio-go` - [ ] Create a pull request in `twilio-go` - [ ] Provide a link below to the pull request - [ ] I have made a material change to the repo (functionality, testing, spelling, grammar) - [ ] I have read the [Contribution Guidelines](https://github.com/twilio/twilio-oai-generator/blob/main/CONTRIBUTING.md) and my PR follows them - [ ] I have titled the PR appropriately - [ ] I have updated my branch with the main branch - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] I have added the necessary documentation about the functionality in the appropriate .md file - [ ] I have added inline documentation to the code I modified If you have questions, please create a GitHub Issue in this repository.
1 parent aebb808 commit e8929a5

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/main/java/com/twilio/oai/CodegenUtils.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ public static boolean isPropertySchemaEnum(CodegenProperty codegenProperty) {
2424
codegenProperty.allowableValues.containsKey(TwilioJavaGenerator.VALUES);
2525
boolean listEnumValues = codegenProperty.items != null && (codegenProperty.items.allowableValues != null
2626
&& codegenProperty.items.allowableValues.containsKey(TwilioJavaGenerator.VALUES));
27-
return enumValues || listEnumValues;
27+
boolean isEnum = enumValues || listEnumValues;
28+
codegenProperty.vendorExtensions.put("x-enum-object", isEnum);
29+
return isEnum;
2830
}
2931

3032
public static boolean isParameterSchemaEnum(CodegenParameter codegenParameter) {
@@ -38,7 +40,9 @@ public static boolean isParameterSchemaEnum(CodegenParameter codegenParameter) {
3840
codegenParameter.allowableValues.containsKey(TwilioJavaGenerator.VALUES);
3941
boolean listEnumValues = codegenParameter.items != null && (codegenParameter.items.allowableValues != null
4042
&& codegenParameter.items.allowableValues.containsKey(TwilioJavaGenerator.VALUES));
41-
return enumValues || listEnumValues;
43+
boolean isEnum = enumValues || listEnumValues;
44+
codegenParameter.vendorExtensions.put("x-enum-object", isEnum);
45+
return isEnum;
4246
}
4347

4448
// TODO: Refactor java code.
@@ -103,4 +107,4 @@ public static boolean isPredefinedEnum(CodegenParameter codegenParameter) {
103107
}
104108
return false;
105109
}
106-
}
110+
}

src/main/resources/twilio-python/modelClasses.handlebars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
def to_dict(self):
1313
return {
1414
{{#vars}}
15-
"{{{vendorExtensions.json-name}}}": {{#if complexType}}{{#containerType}}[{{name}}.to_dict() for {{name}} in self.{{name}}]{{/containerType}}{{^containerType}}self.{{name}}.to_dict(){{/containerType}}{{else}}self.{{name}}{{/if}},{{/vars}}
15+
"{{{vendorExtensions.json-name}}}": {{#if complexType}}{{#containerType}}[{{name}}.to_dict() for {{name}} in self.{{name}}]{{/containerType}}{{^containerType}}self.{{name}}{{^vendorExtensions.x-enum-object}}.to_dict(){{/vendorExtensions.x-enum-object}}{{/containerType}}{{else}}self.{{name}}{{/if}},{{/vars}}
1616
}
1717
{{/hasVars}}{{/vendorExtensions.part-of-response-model}}{{/models}}

0 commit comments

Comments
 (0)