swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

Model IDs with dots in them cause Python generated code to fail

Open madhuri-rai07 opened this issue 11 years ago • 8 comments

The class name in python can't contain dots "." which make the generated code to fail.

madhuri-rai07 avatar Apr 06 '15 00:04 madhuri-rai07

I think this error is due to the swagger-spec used to generate code but not swagger-codegen issue.

madhuri-rai07 avatar Apr 06 '15 00:04 madhuri-rai07

Technically, it's legal to have dots in model names in the spec, and those need to be escaped (or handled differently) by the generator.

webron avatar May 03 '15 20:05 webron

@wing328, @fehguy, For python dots in file name should be replaced by underscore. But now they are replaced by / (https://github.com/swagger-api/swagger-codegen/blob/develop_2.0/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java#L1100) This method is static and it's impossible to override it. I'm thinking to remove static and override the method. What do you think?

lugaru1234 avatar May 14 '15 13:05 lugaru1234

@lugaru1234 As an example, do you mind showing me what the file name (full path) looks like and what's the class name for the model with dot ?

wing328 avatar May 14 '15 13:05 wing328

@wing328, I took this file for the testing https://github.com/GoogleCloudPlatform/kubernetes/blob/master/api/swagger-spec/v1beta3.json#L6361. Will take v1beta3.ResourceQuota as example. In the current state classname=V1beta3ResourceQuota and classVarName=v1beta3/resource_quota. classVarName then is used in __init__package.mustache template and generated code looks like "from .models.v1beta3/resource_quota import V1beta3ResourceQuota". Full path for that file is client/models/v1beta3/resource_quota.py

lugaru1234 avatar May 14 '15 14:05 lugaru1234

@lugaru1234 thanks for the information. I'll see what I can do.

wing328 avatar May 14 '15 14:05 wing328

@wing328 - @lugaru1234 is working on the issue, just need some pointers regarding the comment made above. Would be great if you can voice your opinion with the knowledge you have of the code.

webron avatar May 14 '15 14:05 webron

@lugaru1234 A good starting point is the toModelFileName function in Python client codegen

Adding a line to replace . with _ before calling underscore should be able to resolve the issue.

If testing confirms the fix works, please update toModelName as well with the same fix.

wing328 avatar May 14 '15 14:05 wing328