Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/1255 suffix+prefix for model name #2211

Merged

Conversation

ePaul
Copy link
Contributor

@ePaul ePaul commented Feb 22, 2016

This fixes most of #1255 by adding the options --model-name-prefix and --model-name-suffix to the CLI, with corresponding changes to DefaultCodegen and CodegenConfigurator.

For C# and Java generators we (@jimschubert and me) also added support code as they override DefaultCodegen.toModelName.
I guess this needs to be changed for quite some other languages too.

We also need some unit tests for this functionality, these are not yet included. (The existing unit tests still run without changes, as none of them uses these options.)

ePaul referenced this pull request in jimschubert/swagger-codegen Feb 22, 2016
@wing328
Copy link
Contributor

wing328 commented Feb 23, 2016

@ePaul @jimschubert thanks for the PR. I'll review later today and let you guys know if I've any question.

@@ -82,4 +82,9 @@

public static enum MODEL_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case, original}

public static final String MODEL_NAME_PREFIX = "modelNamePrefix";
public static final String MODEL_NAME_PREFIX_DESC = "Prefix that will be prepended to all model names. Default is the empty string.";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you also aware that another way to add the default value is .defaultValue in CliOption (example)

In this case, I'm fine with both approaches as the default is an empty string.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true for CliOption, but I don't think it would work the same for maven/airline cli options, e.g.:

 @Option(name = {"--model-name-suffix"}, 
         title = "model name suffix", 
         description = CodegenConstants.MODEL_NAME_SUFFIX_DESC)

Airline, for example, only has allowedValues.

edit: probably better in this case to just not say there's a default since it doesn't matter to the user.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe better If the option is not given, nothing will be prepended. (and analogously for the suffix)?

@@ -377,6 +380,7 @@ public String toParamName(String name) {

@Override
public String toModelName(String name) {
name = super.toModelName(name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would there be an issue if we don't call super.toModelName, which is defined as follows?

    public String toModelName(String name) {
        return initialCaps(name);
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this super method (DefaultCodegen.toModelName) I added the actual logic of adding suffix and prefix. Of course I could instead duplicate this here instead of calling the super method (like what Jim did in the C# version).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I prefer doing it in JavaCodegen toModelName as it would make my life easier reviewing changes. For example if someone changes the default toModelName, I've to go through all codegen to see if any one of them calls super.toModelName to involve the default one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I think it's better for generators to opt-in to the prefix/suffix logic than to reimplement the base method just to remove prefix/suffix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think removing the prefix/suffix after the super method added it is not a good idea (if you don't want them, simply don't pass those parameters), but there might be the use case of using a different separator/naming convention (instead of the camelCase) for a programming language, which could be a reason to implement it in the subclass too.

So I would let it stay in the superclass method (which makes it available for all generators which don't override it), but implement it again in the JavaClientCodegen instead of calling super.toModelName(...).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done as mentioned above.

ePaul and others added 6 commits February 26, 2016 11:20
…refix + suffix.

This is just the implementation in DefaultCodegen and some configurator stuff,
the connection to command line arguments is still missing.
This implements the command line arguments `--model-name-prefix` and `--model-name-suffix`
for all languages which do not override `DefaultCodegen.toModelName()`.
This fixes most of swagger-api#1255.

Connection to the maven plugin works with additional-properties, a more explicit
parameter waits for swagger-api#2168.
…r C#.

AbstractCSharpCodegen does override toModelName, which made the change
to DefaultCodegen have no effect.
…x/Suffix addition.

This allows using the model name prefix and suffix parameters also in Java generators.
We add List and Map to the "language specific primitives" so they don't get mangled
by the suffixes/prefixes in `getSwaggerType`
Instead of declaring `Map` and `List` as primitive (which caused them
to be not imported anymore), now we exclude them from the model name
transformation in `JavaClientCodegen.getSwaggerType`.
…dent from super implementation.

As pointed out in the review, using a super.toModelName call makes future changes harder
to review, therefore we are implementing the addition of suffix and prefix here again.

In addition, I fixed the FIXME about assigning the parameter.
@ePaul ePaul force-pushed the feature/1255-suffix+prefix-for-model-name branch from bf88ff6 to 36f7ffd Compare February 26, 2016 10:35
@ePaul
Copy link
Contributor Author

ePaul commented Feb 26, 2016

I rebased again to solve merge conflicts. @wing328 is it now fine this way or should I still change the parameter description text?

@wing328
Copy link
Contributor

wing328 commented Feb 26, 2016

@ePaul let me review now

wing328 added a commit that referenced this pull request Feb 26, 2016
…del-name

Feature/1255 suffix+prefix for model name
@wing328 wing328 merged commit ce83a90 into swagger-api:master Feb 26, 2016
@wing328
Copy link
Contributor

wing328 commented Feb 26, 2016

@ePaul @jimschubert thanks for the PR.

For other langauges, we'll update them one by one and hopefully it will be ready before the 2.1.6 release.

@ePaul ePaul deleted the feature/1255-suffix+prefix-for-model-name branch February 26, 2016 14:21
@jimschubert
Copy link
Contributor

@wing328 what's the best way to track languages needing prefix/suffix modifications? Maybe a check list on #1255?

@wing328
Copy link
Contributor

wing328 commented Feb 27, 2016

updated #1255 with a
task list.

On Sat, Feb 27, 2016 at 12:33 AM, Jim Schubert notifications@github.com
wrote:

@wing328 https://github.com/wing328 what's the best way to track
languages needing prefix/suffix modifications? Maybe a check list on #1255
#1255?


Reply to this email directly or view it on GitHub
#2211 (comment)
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants