swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.92k stars 6.03k forks source link

[RUBY] Generator should fail if model classes shadow standard classes in the same namespace #8283

Open cgrothaus opened 6 years ago

cgrothaus commented 6 years ago
Description

The ruby code generator should fail if one of the generated model classes shadows a standard class.

The code generator generates the model classes in the same namespace as the standard classes like for example ApiClient, ApiError, Configuration or DefaultApi. If there is a definition in the declaration file with the same name, the model class that is generated from that definition shadows the standard class. For ruby this means the model class and the standard class are mixed into one big class with unpredictable effects.

Swagger-codegen version

2.3.1

Swagger declaration file content or url
Command line used for generation

swagger-codegen generate -i ./namespace-issue-swagger-spec.yaml -l ruby -o ./namespace-issue/ -c ./codegen.json

Steps to reproduce
  1. Generate ruby code with the command line from above
  2. Have a look at the two generated files ./namespace-issue/lib/namespace_issue/api_error.rb and ./namespace-issue/lib/namespace_issue/models/api_error.rb: they both define the same class ApiError in the same module namespace.
Related issues/PRs

Nothing known to me.

Suggest a fix/enhancement

The generator should fail under such circumstances. Definitions in the swagger declaration should not be allowed to shadow standard class names.

tmertens commented 5 years ago

I just ran into this as well, but I think the solution offered can be improved. The presence of a collision is not an indication of an error in the swagger definition file and as such, the compiler should succeed; however, the compiled code should be designed so as to prevent such a conflict from occurring in the first place.

A proper solution would be to namespace the models under a Models (or similar) namespace, so that a collision cannot occur with other supporting code within the generated API client.