Swagger-codegen: Allow modifying the generated model names by prefix or suffix

Created on 18 Sep 2015  Â·  11Comments  Â·  Source: swagger-api/swagger-codegen

This is a feature request for a new (optional) generator parameter.

Summary

It should be possible to specify a prefix and/or suffix which will be added to all model type names when generating the types.

Background
The model names generated from the swagger definition have usually quite natural names ... in Java, I got (in my case) for example Supplier, Organization, BusinessUnit.
I also have same-named domain classes in the service implementing the API. Now of course I need to write a mapper between those two, and here I can't import both the domain and the API classes. One of them needs to be written out with full package name in the source, and this is ugly:

private Organization mapOrganization(final 
    de.zalando.einkaufshelden.purchase_order.api.rest.model.Organization organization) {

For the similar problem previously with Soap services we used the suffix DTO (for Data Transfer Object) for the objects sent over the wire.

In other projects I've seen a prefix used for all class names to distinguish them from similar ones (think the Swing J* classes).

Proposed solution

There should be two configuration parameters (as a command line parameter in swagger-codegen-cli, as a maven configuration parameter in swagger-codegen-maven-plugin) for specifying a prefix and a suffix to be appended to the model names. Default values would be the empty string. (My name proposals would be modelNamePrefix and modelNameSuffix.)

These could be applied in DefaultCodegen.toModelName before passing the name to the sanitizing transformation.

Alternatively there could be also one single parameter somehow specifying the complete transformation, using a regular expression replacement or similar.

UPDATE
API client generators that need to be updated to support model prefix/suffix:

  • [x] Android
  • [ ] C++
  • [x] C# (#2211)
  • [ ] Clojure
  • [ ] Dart
  • [ ] Flash
  • [ ] Go
  • [x] Javascript (https://github.com/swagger-api/swagger-codegen/pull/2300)
  • [x] Java (#2211)
  • [x] ObjC (#2267)
  • [x] Perl (#2261)
  • [x] PHP (#2261)
  • [x] Python (#2161)
  • [x] Ruby (#2272)
  • [x] Scala
  • [x] Swift
  • [x] TypeScript (#2270)
C++ Clojure Dart FlasActionScript Go Feature help wanted

Most helpful comment

Hi! Now that Flutter is out of beta, Dart and swagger-generated code will be even more popular. Is there any progress on fixing this issue for Dart?

All 11 comments

+1

@ePaul you mentioned in #2104 that you intended to do this work. Have you gotten very far?

@jimschubert not much more than mentioned there, but I intend to work on this today.

@jimschubert I didn't get much done today (other urgent stuff interfered), but I pushed a feature branch in my fork. Hopefully tomorrow is better.

@ePaul Hey I've had conjunctivitis for the past couple of days and I've been a lot less productive than normal. I'm going to check out your feature branch today and see what I can contribute to it.

@ePaul I've implemented these options on swagger codegen cli. Please take a look (commit referenced above).

java -jar ./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
--model-name-prefix Gen \
-i modules/swagger-codegen/src/test/resources/2_0/petstore.json \
-l csharp \
-o samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient \
-DtargetFramework=v3.5,optionalProjectFile=true

If this looks good to you, I can open a pull request. Or, if you have changes for a specific language (as in my example of the csharp generator) you can grab my changes and update your desired language and open the pull request.

@jimschubert Thanks, your change was quite similar to what I did (but had not yet committed).

I added a similar change to JavaClientCodegen to use the superclass method (now in my feature branch).

Trying it out with

java -jar ./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
--model-name-suffix Dto -i modules/swagger-codegen/src/test/resources/2_0/petstore.json
-l java -o samples/client/petstore/java/default

generates stuff like

  private ListDto<String> photoUrls = new ArrayList<String>();
  private ListDto<TagDto> tags = new ArrayList<TagDto>();

(in PetDto) – but of course it should be List<String and List<TagDto>.

In you C# example it seems to work here – I guess Java does something differently here.
(I'm looking through the code right now.)

@jimschubert I fixed the problem mentioned before. I'll rebase the branch (with a new name) to fix some commit comments, then I'll create the pull request.

_Current status_: This was fixed in #2211 for Java and C#, as well as all languages which don't override DefaultCodegen.toModelName(String).

Other languages still need to be updated (the new options are otherwise doing nothing).

Hi! Now that Flutter is out of beta, Dart and swagger-generated code will be even more popular. Is there any progress on fixing this issue for Dart?

Don't add prefix or suffix if file starts or ends with same value.
--model-name-suffix=DTO adds DTO also to files which ends to DTO. This is not what I wan't.

Was this page helpful?
0 / 5 - 0 ratings