Skip to content

[JAVA] Java-Types (BigDecimal, LocalDate, ...) are also effected by modelNamePrefixes and Suffixes #3285

@d0x

Description

@d0x
Description

When swagger is using Java-Types (BigDecimal, LocalDate, ...) as fields, those are also renamed.

In case modelNameSuffix is set to Dto, this will into Types like BigDecimalDto which is wrong.

This snipped is taken from the DefaultCodegen:

    /**
     * Output the proper model name (capitalized)
     *
     * @param name the name of the model
     * @return capitalized model name
     */
    public String toModelName(final String name) {
        return initialCaps(modelNamePrefix + name + modelNameSuffix);
    }

and this from AbstractJavaCodegen:

    @Override
    public String toModelName(final String name) {
        final String sanitizedName = sanitizeName(modelNamePrefix + name + modelNameSuffix);

        // camelize the model name
        // phone_number => PhoneNumber
        final String camelizedName = camelize(sanitizedName);

        // model name cannot use reserved keyword, e.g. return
        if (isReservedWord(camelizedName)) {
            final String modelName = "Model" + camelizedName;
            LOGGER.warn(camelizedName + " (reserved word) cannot be used as model name. Renamed to " + modelName);
            return modelName;
        }

        // model name starts with number
        if (name.matches("^\\d.*")) {
            final String modelName = "Model" + camelizedName; // e.g. 200Response => Model200Response (after camelize)
            LOGGER.warn(name + " (model name starts with number) cannot be used as model name. Renamed to " + modelName);
            return modelName;
        }

        return camelizedName;
    }
Swagger-codegen version

2.2.0-SNAPSHOT

Suggest a Fix

Use the typeMapper.values and ignore those.

I'll open a PR for this.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions