OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.36k stars 6.46k forks source link

[BUG][Java] Incompatibility between typeMapping and supportUrlQuery #16183

Open nvervelle opened 1 year ago

nvervelle commented 1 year ago

Bug Report Checklist

Description

When using type mapping for some model objects to Map with native library, the generator produces a class that doesn't compile because the type mapping is not taken into account in the toUrlQueryString() method. While the attribute type has been correctly changed following the type mapping, the implementation of toUrlQueryString() doesn't take the change into account calling an non-existent method.

The problem happens by default with native library (because supportUrlQuery is set to true by default for this library), but if you explicitly set supportUrlQuery to false, the result builds (because there's no toUrlQueryString() method).

The problem happens also for other libraries if you explicitly set supportUrlQuery to true.

    private Map fields;
...
    public Map getFields() {
        return fields;
    }
....
    if (getFields() != null) {
        joiner.add(getFields().toUrlQueryString(prefix + "fields" + suffix)); // <= DOESN'T COMPILE
    }
....
openapi-generator version

I saw the problem with version 6.6.0, but I reproduced the problem with master : see the sample and test reproducing the problem

OpenAPI declaration file content or url

Simple OpenAPI declaration file used by the test reproducing the problem

Generation Details

See test reproducing the problem.

Steps to reproduce

See test reproducing the problem.

The generated model file shouldn't call getFields().toUrlQueryString().

Related issues/PRs
Suggest a fix

The problem seems to happen because pojo.mustache relies on {{#isModel}} to decide if it can use a call to toUrlQueryString() instead of a manual encoding (Java/pojo.mustache, Java/libraries/native/pojo.mustache) but {{#isModel}} doesn't take into account the type mapping.

wing328 commented 1 year ago

As a workaround, you can skip toUrlQueryString in POJO by disabling supportUrlQuery, for example in CLI

--additional-properties supportUrlQuery=false

nvervelle commented 1 year ago

Yes, that's what I'm currently doing, that's what I meant by

if you explicitly set supportUrlQuery to false, the result builds

wing328 commented 1 year ago

OK. May I know if you've time to contribute a PR to fix the issue?

nvervelle commented 1 year ago

Hi @wing328

Not sure I will have enough time in the following weeks (holidays soon), but if I find some time, I will look into it.

Do you have any idea were the problem might be ?