OpenAPITools / OpenAPITools/openapi-generator

[BUG] [DefaultCodeGen] Map<String, X> is generated as API requestBody if Model class has additionalProperties=true

Open
#3,388 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

In case a model class has additionalProperties=true, in the API a Map is inserted as requestBody parameter instead of the model class.

openapi-generator version

4.0.3

OpenAPI declaration file content or url

See steps to reproduce.

Command line used for generation

See steps to reproduce.

Steps to reproduce

I used the following pom.xml to generate the sources: https://gist.github.com/balazs-zsoldos/7c707b098d97bac59a3b411d5d6b0664

For an example, see GroupResourceAPI.addUserToGroup(...). The requestBody parameter here is a Map, but it should be UpdateUserToGroupBean.

Related issues/PRs
Suggest a fix

I analyzed the problem and I think the behavior could be changed in DefaultCodeGen.fromRequestBody(...) function, more specifically changing this and the next line to the following code solves the issue:

if (name != null) {
    CodegenModel codegenModel = fromModel(name, schema);
    codegenParameter.baseType = codegenModel.classname;
    codegenParameter.dataType = getTypeDeclaration(codegenModel.classname);
    imports.add(codegenParameter.baseType);
} else {
    codegenParameter.dataType = getTypeDeclaration(schema);
    codegenParameter.baseType = getSchemaType(inner);
}

However, I am not sure how this would affect all generators that already exist. If there is a specific reason why Map is inserted in this case instead of the model type. I would like to send a patch that does not cause any trouble.

Do you think this is a good fix? Or shall I create a new configuration property for the generator like useModelTypeForRequestBodyParamWhereAdditionalPropertiesIsTrue and make it default false? In that case, the current behavior is not changed only if explicitly sets this configuration to true.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with DefaultCodegen.java and inspect DefaultCodegen.fromRequestBody(...) around the linked lines, then reproduce the generated GroupResourceAPI.addUserToGroup(...) case using the pom.xml gist. Check how the change affects existing generators; done means the request body uses UpdateUserToGroupBean rather than Map without regressions across relevant generator tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.