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

TypeScript: marshalling of additionalProperties properties is naive #1435

Closed
moltar opened this issue Jul 9, 2023 · 8 comments
Closed

TypeScript: marshalling of additionalProperties properties is naive #1435

moltar opened this issue Jul 9, 2023 · 8 comments
Labels
bug Something isn't working

Comments

@moltar
Copy link

moltar commented Jul 9, 2023

Describe the bug

It looks like nested models get a simple JSON.stringify treatment and thus result in stringifying the internal/private (_foo) props, rather than public names.

How to Reproduce

  1. Use a JSON Schema with a additionalProperties option. I am using a JSON Schema for GitHub Actions Workflows.
  2. Create a TS generator with modelType: 'class' and marshalling: true options
  3. Run generator
  4. Observe that nested marhsalling code simply looks like:
export class SgJobs {
  private _additionalProperties?: Map<string, SgNormalJob | SgReusableWorkflowCallJob>;

  constructor(input: {
    // ⚠️ ➡️                           👇🏻 type is known
    additionalProperties?: Map<string, SgNormalJob | SgReusableWorkflowCallJob>,
  }) {
    this._additionalProperties = input.additionalProperties;
  }

  get additionalProperties(): Map<string, SgNormalJob | SgReusableWorkflowCallJob> | undefined { return this._additionalProperties; }
  set additionalProperties(additionalProperties: Map<string, SgNormalJob | SgReusableWorkflowCallJob> | undefined) { this._additionalProperties = additionalProperties; }

  public marshal() : string {
    let json = '{'
    if(this.additionalProperties !== undefined) { 
    for (const [key, value] of this.additionalProperties.entries()) {
      //Only unwrap those who are not already a property in the JSON object
      if(Object.keys(this).includes(String(key))) continue;
        // ⚠️ ➡️                                                                               👇🏻 calls JSON.stringify naively
        json += `"${key}": ${typeof value === 'number' || typeof value === 'boolean' ? value : JSON.stringify(value)},`;
      }
    }

    //Remove potential last comma 
    return `${json.charAt(json.length-1) === ',' ? json.slice(0, json.length-1) : json}}`;
  }

Expected behavior

Probably needs to call .marhsal() if it is a known model object

@moltar moltar added the bug Something isn't working label Jul 9, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Jul 9, 2023

Welcome to AsyncAPI. Thanks a lot for reporting your first issue. Please check out our contributors guide and the instructions about a basic recommended setup 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.

@jonaslagoni
Copy link
Member

jonaslagoni commented Jul 14, 2023

Sorry for the delay in response @moltar, currently on holiday 😄

I think #1197 should have solved this, what version of Modelina are you using? 1.3.1 and next should contain the fix 🤔

@moltar
Copy link
Author

moltar commented Jul 15, 2023

I was using 2.0.0-next.14, and on the PR, it says that the fix was included in 2.0.0-next.6.

@jonaslagoni
Copy link
Member

Might be an issue with the additional properties then 🤔

@jonaslagoni
Copy link
Member

Ill have a look once I get back next week 🙂

@jonaslagoni
Copy link
Member

@duckling69 maybe you wanna have a go at this issue?

Copy link
Contributor

This issue has been automatically marked as stale because it has not had recent activity 😴

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience ❤️

@github-actions github-actions bot added the stale label Nov 22, 2023
@jonaslagoni
Copy link
Member

jonaslagoni commented Dec 31, 2023

Tracing this back to dictionary types and unions, all dictionary types regardless of whether it's for additionalProperties or something else.

Working on a fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants