Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for typed array unmarshalling in TypeScript #2131

Conversation

jonjomckay
Copy link
Contributor

Description

This PR adds support for properly unmarshalling array types, when using the TypeScript generator. Previously, the JSON array was being passed straight into the resulting object, which causes issues when using the value in another schema object and marshalling that object. The other object expects the array to contain full objects with a callable marshal function, not a plain object, giving an error like:

err: {
      "type": "TypeError",
      "message": "unionItem.marshal is not a function",
      "stack":
          TypeError: unionItem.marshal is not a function
              at KeywordSearchRequest.marshal
...

Example

Given a type like this:

arrayProperty:
  type: array
  items:
    $ref: "#/definitions/SomeObjectType"

The generator will now generate unmarshalling code like this:

...
instance.arrayProperty = obj["arrayProperty"] == null
  ? null
  : obj["arrayProperty"].map((item: any) => SomeObjectType.unmarshal(item));

Related Issue

Checklist

  • The code follows the project's coding standards and is properly linted (npm run lint).
  • Tests have been added or updated to cover the changes.
  • Documentation has been updated to reflect the changes.
  • [] All tests pass successfully locally.(npm run test).

Additional Notes

The tests don't seem to pass even before my changes. I have updated the existing snapshot to cater for this change, and that test runs successfully.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welcome to AsyncAPI. Thanks a lot for creating your first pull request. Please check out our contributors guide useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

Copy link

netlify bot commented Nov 29, 2024

Deploy Preview for modelina canceled.

Name Link
🔨 Latest commit dd498d0
🔍 Latest deploy log https://app.netlify.com/sites/modelina/deploys/67587390cb79640008034e95

@jonjomckay jonjomckay force-pushed the feat/support-typed-array-unmarshal-in-typescript branch from 1d70296 to 86f1379 Compare November 29, 2024 16:48
@jonjomckay jonjomckay changed the title feat: Add support for typed array unmarshalling in TypeScript feat: add support for typed array unmarshalling in TypeScript Nov 29, 2024
@coveralls
Copy link

coveralls commented Dec 10, 2024

Pull Request Test Coverage Report for Build 12260990273

Details

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.005%) to 92.319%

Totals Coverage Status
Change from base Build 12189640256: 0.005%
Covered Lines: 6004
Relevant Lines: 6335

💛 - Coveralls

@jonaslagoni jonaslagoni merged commit ca1177a into asyncapi:master Dec 10, 2024
17 checks passed
@jonaslagoni
Copy link
Member

@all-contributors please add @jonjomckay for code, bug, test

Copy link
Contributor

@jonaslagoni

I've put up a pull request to add @jonjomckay! 🎉

@jonjomckay jonjomckay deleted the feat/support-typed-array-unmarshal-in-typescript branch December 10, 2024 20:34
@asyncapi-bot
Copy link
Contributor

🎉 This PR is included in version 3.10.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants