Skip to content

Commit ec746b6

Browse files
authored
chore: fix python generation issue for nested object (#770)
<!-- 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 # Added two fixes - Empty to_dict generation for nested object. This was happening due to `json-name` not getting propagated to nested objects - reserved Keyword for nested property fix. Should use name instead of baseName, sInce name has the updated value for reserved keyword like _from for from. Code generated of conversation v2 here twilio/twilio-python#909 ### Checklist - [x] I acknowledge that all my contributions will be made under the project's license - [ ] Run `make test-docker` - [ ] Verify affected language according to the code change: - [ ] Generate [twilio-java](https://github.com/twilio/twilio-java) from our [OpenAPI specification](https://github.com/twilio/twilio-oai) using the [scripts/build_twilio_library.py](./scripts/build_twilio_library.py) using `python scripts/build_twilio_library.py path/to/twilio-oai/spec/yaml path/to/twilio-java -l java` and inspect the diff - [ ] Run `make test` in `twilio-java` - [ ] Create a pull request in `twilio-java` - [ ] Provide a link below to the pull request, this ensures that the generated code has been verified - [ ] 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 e9d95d3 commit ec746b6

4 files changed

Lines changed: 28 additions & 15 deletions

File tree

src/main/java/com/twilio/oai/api/PythonApiResourceBuilder.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,17 @@ public ApiResourceBuilder updateResponseModel(Resolver<CodegenProperty> codegenP
151151
.forEach(this::processModelVariables);
152152
}
153153

154-
modelTree.values().forEach(model -> model.setName(getModelName(model.getClassname())));
154+
modelTree.values().forEach(model -> {
155+
model.setName(getModelName(model.getClassname()));
156+
// Set json-name for properties in nested models
157+
model.getVars().forEach(property -> {
158+
// Set json-name using baseName (original field name from spec) if it differs from name
159+
String jsonName = (property.baseName != null && !property.baseName.equals(property.name))
160+
? property.baseName
161+
: property.name;
162+
property.vendorExtensions.put("json-name", jsonName);
163+
});
164+
});
155165
if (responseModel != null) {
156166
responseModel.getVars().forEach(variable -> {
157167
if (variable.complexType != null && !variable.complexType.contains(ApplicationConstants.ENUM)) {

src/main/java/com/twilio/oai/resolver/python/PythonCodegenModelDataTypeResolver.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,13 @@ public void setCodegenModel(CodegenModelResolver codegenModelResolver) {
4141
*/
4242
@Override
4343
public CodegenProperty resolve(CodegenProperty property, ApiResourceBuilder apiResourceBuilder) {
44-
property = super.resolve(property, apiResourceBuilder);
44+
property = super.resolve(property, apiResourceBuilder);
4545
CodegenModel codegenModel = apiResourceBuilder.getModel(property.dataType);
4646
if (codegenModel != null && !CodegenUtils.isPropertySchemaEnum(property)) {
47-
// this is recursion as codegenModelResolver will again call PythonCodegenModelDataTypeResolver
4847
codegenModelResolver.resolve(codegenModel, apiResourceBuilder);
4948
apiResourceBuilder.addNestedModel(codegenModel);
50-
} else {
51-
super.resolve(property, apiResourceBuilder);
52-
resolveProperty(property, apiResourceBuilder);
5349
}
50+
resolveProperty(property, apiResourceBuilder);
5451
return property;
5552
}
5653

@@ -61,6 +58,7 @@ public CodegenProperty resolve(CodegenProperty property, ApiResourceBuilder apiR
6158
*/
6259
public void resolveResponseModel(CodegenProperty property, ApiResourceBuilder apiResourceBuilder) {
6360
super.resolve(property, apiResourceBuilder);
61+
resolveProperty(property, apiResourceBuilder);
6462
}
6563

6664
/**
@@ -74,19 +72,24 @@ protected void resolveProperty(CodegenProperty property, ApiResourceBuilder apiR
7472
property.name = "from_";
7573
property.vendorExtensions.put("json-name", "from");
7674
} else {
77-
property.vendorExtensions.put("json-name", property.name);
75+
// Use baseName (original field name from spec) as the json-name if it differs from name
76+
// This handles cases where reserved word mappings have been applied
77+
String jsonName = (property.baseName != null && !property.baseName.equals(property.name))
78+
? property.baseName
79+
: property.name;
80+
property.vendorExtensions.put("json-name", jsonName);
7881
}
7982
if(CodegenUtils.isPropertySchemaEnum(property)
8083
&& !property.dataType.contains(ApplicationConstants.ENUM)
8184
&& !property.dataType.contains("Instance.")) {
8285
property.baseType = ApplicationConstants.ENUM + property.baseType;
8386
property.dataType = ApplicationConstants.ENUM + property.dataType;
87+
updateDataType(property.baseType, property.dataType, apiResourceBuilder, (dataTypeWithEnum, dataType) -> {
88+
property.datatypeWithEnum = dataTypeWithEnum;
89+
property.dataType = dataType;
90+
});
91+
property.baseType = property.dataType;
8492
}
85-
updateDataType(property.baseType, property.dataType, apiResourceBuilder, (dataTypeWithEnum, dataType) -> {
86-
property.datatypeWithEnum = dataTypeWithEnum;
87-
property.dataType = dataType;
88-
});
89-
property.baseType = property.dataType;
9093
}
9194

9295
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
def __init__(self, payload: Dict[str, Any]):
88

99
{{#vars}}
10-
self.{{name}}: Optional[{{{dataType}}}] = {{#if vendorExtensions.x-deserialize}}{{vendorExtensions.x-deserialize}}(payload.get("{{{name}}}")){{else}}payload.get("{{{name}}}"){{/if}}{{/vars}}
10+
self.{{name}}: Optional[{{{dataType}}}] = {{#if vendorExtensions.x-deserialize}}{{vendorExtensions.x-deserialize}}(payload.get("{{{vendorExtensions.json-name}}}")){{else}}payload.get("{{{vendorExtensions.json-name}}}"){{/if}}{{/vars}}
1111

1212
def to_dict(self):
1313
return {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
Nested response model for {{name}}
44
'''
55
class {{name}}:
6-
def __init__(self,{{#vars}}{{baseName}}: {{{dataType}}}{{#unless @last}}, {{/unless}}{{/vars}}):
7-
{{#vars}}self.{{baseName}} = {{baseName}}
6+
def __init__(self,{{#vars}}{{name}}: {{{dataType}}}{{#unless @last}}, {{/unless}}{{/vars}}):
7+
{{#vars}}self.{{name}} = {{name}}
88
{{/vars}}
99

1010
{{/models}}

0 commit comments

Comments
 (0)