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

Fix: Always serialise Map and Set to object and array in typescript-axios

Open adrianhelvikspond opened this issue 1 year ago • 4 comments

This produces type definitions that aren't compatible with the typescript-axios client:

        someField:
          uniqueItems: true
          type: array
          items:
            type: string

My fix serialises of Sets into arrays and Maps into objects.

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.

@TiFu @taxpon @sebastianhaas @kenisteward @Vrolijkx @macjohnny @topce @akehir @petejohansonxo @amakhrov @davidgamero @mkusaka

adrianhelvikspond avatar Feb 05 '24 14:02 adrianhelvikspond

Fixes the serialization part of #11746

adrianhelvikspond avatar Feb 05 '24 21:02 adrianhelvikspond

can you please follow step 3 to update the samples?

wing328 avatar Feb 07 '24 04:02 wing328

Done!

Sorry about missing the samples. Step 3 messed up some samples for all languages on Mac. Worked fine with Docker though. This script might be useful for Mac users if it happens to others:

docker run -it --rm --workdir /project -v $PWD:/project sapmachine:21-jdk-ubuntu sh -c './mvnw clean package && ./bin/generate-samples.sh ./bin/configs/*.yaml && ./bin/utils/export_docs_generators.sh'

adrianhelvikspond avatar Feb 07 '24 21:02 adrianhelvikspond

I replaced Object.entries and Object.fromEntries with ponyfills that are compatible with the target environment.

adrianhelvikspond avatar Feb 12 '24 14:02 adrianhelvikspond

can you please merge the latest master into your branch as the ts axios test failures have been fixed?

wing328 avatar Feb 28 '24 10:02 wing328

Rebased from master now.

adrianhelvikspond avatar Mar 15 '24 16:03 adrianhelvikspond

@adrianhelvikspond can you fix the build issue?

macjohnny avatar Mar 16 '24 08:03 macjohnny

I updated the tsconfig so it downlevels iterators to Symbol.iterator, which is needed for map iterators.

adrianhelvikspond avatar Mar 18 '24 08:03 adrianhelvikspond

@macjohnny, @wing328, @adrianhelvikspond this introduced new issue. Now Date object is serialized to {}. Request:

{ testDate: new Date() }

After serialization becomes:

{ testDate: {} }

This might happen not only with dates. Looks like you have to check if object has toJSON method. And if has, execute it for final value: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

Had to downgrade from v7.5.0-SNAPSHOT to v7.4.0.

pauliusg avatar Apr 17 '24 06:04 pauliusg

@adrianhelvikspond do you want to fix this?

macjohnny avatar Apr 22 '24 06:04 macjohnny

I reverted this PR in https://github.com/OpenAPITools/openapi-generator/pull/18452 @pauliusg thanks for reporting the issue!

macjohnny avatar Apr 22 '24 06:04 macjohnny

This PR also changed the response for binary media, prior to 7.5.0, my response looked like:

{
      type: 'Buffer',
      data: [
        255, 216, 255, 224,  0,  16,  74, 70,  73,  70,  0,  1,
          1,   0,   0,   1,  0,   1,   0,  0, 255, 219,  0, 67,
        ... 32003 more items
      ]
    }

but it now appears as:

{
      '0': 255,
      '1': 216,
      '2': 255,
      '3': 224,
      '4': 0,
      '5': 16,
      '6': 74,
...
}
This makes it more difficult to write this data to a file. Reverting this PR helps for the time being, but we'll still need a solution for the Maps and Sets to be serialized properly.

ckoegel avatar Apr 23 '24 19:04 ckoegel