openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[JAVA] Fix generation of remove method of map entries

Open rehand opened this issue 1 year ago • 2 comments

Map::remove in java removes entries by key, therefore the (String) key must be used as parameter.

When having an object with values not being a string the generated remove method is wrong as entries of a map are removed by key, not by value.

Example:

openapi: 3.0.3
info:
    title: Test
    description: Test.
    version: 1.0.0
servers:
    - url: https://localhost:29090/api
paths:
    /test:
        post:
            summary: Test.
            requestBody:
                content:
                    application/json:
                        schema:
                            type: array
                            items:
                                $ref: '#/components/schemas/TestDto'
            responses:
                '201':
                    description: OK.
components:
    schemas:
        TestDto:
            type: object
            properties:
                testId:
                    type: string
                    description: Unique ID
                    format: uuid
                mapStringToString:
                    type: object
                    additionalProperties:
                        type: string
                    description: A key/value map string to string.
                mapStringToNumber:
                    type: object
                    additionalProperties:
                        type: number
                    description: A key/value map of string to number.

The generated remove method is wrong:

  public TestDto removeMapStringToNumberItem(BigDecimal mapStringToNumberItem) {
    if (mapStringToNumberItem != null && this.mapStringToNumber != null) {
      this.mapStringToNumber.remove(mapStringToNumberItem);
    }

    return this;
  }

With this PR the remove method looks like this:

  public TestDto removeMapStringToNumberItem(String key) {
    if (this.mapStringToNumber != null) {
      this.mapStringToNumber.remove(key);
    }

    return this;
  }

Please review @bbdouglas @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger @karismann @Zomzog @lwlee2608 @martin-mfg

I'm unsure regarding "breaking changes" as the generated API changes without fallback, but the previous generated method was simply wrong and not usable. For maps with a key value the API is compatible.

PR checklist

  • [X] Read the contribution guidelines.
  • [X] 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.
  • [X] Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (For Windows users, please run the script in Git BASH) Commit all changed files. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*. IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • [X] File the PR against the correct branch: master (upcoming 7.1.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • [X] If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

rehand avatar May 03 '24 09:05 rehand

Thanks for the PR but your commit (as shown in the Commits tab) is not linked to your Github account, which means this PR won't count as your contribution in https://github.com/OpenAPITools/openapi-generator/graphs/contributors.

Let me know if you need help fixing it.

Ref: https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-can-i-update-commits-that-are-not-linked-to-my-github-account

wing328 avatar May 03 '24 13:05 wing328

Hi, I've pushed a second commit, I think it should be linked now to my account. Also I've added some changed test files from the generators, forgot those.

rehand avatar May 03 '24 17:05 rehand

thanks for the fix, which has been merged into master

wing328 avatar May 07 '24 04:05 wing328