swagger-api / swagger-api/swagger-codegen
Is there any existing tool for mapping definition names?
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
This is an information-seeking question. Before I spend any time developing a tool to do this, I want to make sure there is not an existing tool which already does this. Here is the problem I'm trying to solve.
We have an existing OpenAPI spec which is produced by a backend team. They use a naming convention which frequently incorporates the term "View", as in "WidgetView". To them this means a rendering of data. When I code-generate from this for mobile clients like Android and iOS, this produces a class named something like "WidgetView". To a mobile developer, if they saw a class called "WidgetView", then this means something very different. They would interpret that class as some sort of view (i.e. - user interface) class.
So what I would like to do is map the model name "WidgetView" to some other name (like "WidgetData" or "WidgetModel", for example) in every place it is used: in the generated model class, when that model is referenced via a property, etc.
I tried using the --type-mappings option from the swagger-codegen command-line, as in:
swagger-codegen generate ... --type-mapping WidgetView=WidgetData
and this worked when WidgetView is referenced as a property. So using --type-mapping changed this (using swift as an example):
var widget: WidgetView
to:
var widget: WidgetData
However, this actually resulted in a build error, because the original model class was still called WidgetView:
class WidgetView
Is this a bug? I tried this using both the "swift3" and "android" languages, and they behaved exactly the same: they mapped the property name as expected, but they did not map the class name. Since both behaved the same, then I am guessing that --type-mapping was more intended for mapping built-in types which are native to the language (like "Int" -> "MyInt", for example) rather than mapping definition names ("WidgetView" -> "WidgetData").
If the current behavior of --type-mapping is NOT a bug, then my question is: does anyone know of any existing tools that do what I want?
I realize that this is not a trivial task, since both ref's and the definition name would have to be mapped, and if those definitions are actually in a separate file, then this becomes complicated really quickly.
Swagger-codegen version
I was using 2.2.3.
Swagger declaration file content or url
Any swagger-schema with a definition name "Foo"
Command line used for generation
swagger-codegen -l ... --type-mapping Foo=Bar
Steps to reproduce
- Take a spec with definition named "Foo"
- Use the command-line above
- See if the generated code compiles
Suggest a fix/enhancement
I'm not sure if this is a bug or intended behavior.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the --type-mapping Foo=Bar command with a schema definition and property reference, using the swift3 and android generators mentioned in the issue. Inspect the generated model class and its references; done would mean determining whether both use the mapped name and whether the generated code compiles.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, openapi, swift
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100