Created by: ahsanfz
This is for issue #9444
@bbdouglas, @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04) @lwlee2608 (2019/10)
Need feedback
So I analyzed the problem as following:
org.openapitools.codegen.utils.StringUtils.camelize(final String inputWord, boolean lowercaseFirstLetter)
removes all underscores characters using this matcher:
m = camelizeUnderscorePattern.matcher(word);
If we let camelize do what it does and say that even removing the first underscore is part of its job, then one can do a more local fix in class "AbstractJavaCodegen" just for the java generators.
So toVarName() and getterAndSetterCapitalize() could be changed with something as simple as the line below at start of methods:
boolean beginsWithUnderScore = name.startsWith("_");
And when the methods are ready to return, re-add back the underscore at the beginning. For example the toVarName could change as follows towards the end.
And getterAndSetterCapitalize is just a little bit complicated, but doable as follows:
public String getterAndSetterCapitalize(String name) {
boolean lowercaseFirstLetter = false;
if (name == null || name.length() == 0) {
return name;
}
boolean prependUnderscore = name.length()!=1 && name.startsWith("_");
name = toVarName(name);
//
// Let the property name capitalized
// except when the first letter of the property name is lowercase and the second letter is uppercase
// Refer to section 8.8: Capitalization of inferred names of the JavaBeans API specification
// http://download.oracle.com/otn-pub/jcp/7224-javabeans-1.01-fr-spec-oth-JSpec/beans.101.pdf)
//
if (name.length() > 1 && Character.isLowerCase(name.charAt(0)) && Character.isUpperCase(name.charAt(1))) {
lowercaseFirstLetter = true;
}
name = camelize(name, lowercaseFirstLetter);
// Note capitalizing "P" initial letter since camelize capitalizes it
if ( prependUnderscore && !name.equals("PropertyClass")) {
name = "_" + name;
}
return name;
}
Can some one see a problem for these changes. Any other suggestions ?
We could make this a configurable option as well.
Open Part still
Also the last open part is the moustache templates.
For this schema with props type, _type and subtype:
components:
schemas:
Message:
required:
- _type
- text
type: object
properties:
type:
type: string
description: Type of this message.
enum:
- STATUS
- TEXT
- ERROR
- PROGRESS
subType:
type: string
description: Sub-type of this message.
enum:
- STATUS
- LOCK
- UNKNOWN
_type:
type: string
discriminator:
propertyName: _type
I should get this generated in the annotations
@JsonPropertyOrder({
Message.JSON_PROPERTY_TYPE,
Message.JSON_PROPERTY_SUB_TYPE,
Message.JSON_PROPERTY__TYPE
})
but the double "__" in third property above is not generated:
I looked at the moustache templates. No expert here, but maybe the "nameInSnakeCase" is the culprit in pojo.moustasche. I just searched the string "JSON_PROPERTY_" and the following lines poped up:
{{classname}}.JSON_PROPERTY_{{nameInSnakeCase}}{{^-last}},{{/-last}}
Must be other places also as variable is wrong at a couple of places.
Any suggestions
Regards,
PR TEMPLATE Leave alone for now.
PR checklist
-
Read the contribution guidelines. -
Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community. -
Run the following to build the project and update samples: ./mvnw clean package ./bin/generate-samples.sh ./bin/utils/export_docs_generators.sh
./bin/generate-samples.sh bin/configs/java*
. For Windows users, please run the script in Git BASH. -
File the PR against the correct branch: master
(6.1.0) (minor release - breaking changes with fallbacks),7.0.x
(breaking changes without fallbacks) -
If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.