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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jonjomckay
Copy link

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 86f1379
🔍 Latest deploy log https://app.netlify.com/sites/modelina/deploys/6749f0532b54cb0008c54f07

@jonjomckay jonjomckay force-pushed the feat/support-typed-array-unmarshal-in-typescript branch from 1d70296 to 86f1379 Compare November 29, 2024 16:48
Copy link

sonarcloud bot commented Nov 29, 2024

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant