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: ensure unions of enums are rendered for properties for TS #155

Merged

Conversation

jonaslagoni
Copy link
Member

Description
This PR ensures the type for properties are rendered with enums if sat.

Related issue(s)
solves #133

@czlowiek488
Copy link

It may be enough and I will use it but I don`t like it.
In perfect world I as a user should be able to generate mutual enum if I reference to it by $ref in schema.
Also I would love to see generic interface when I use allOf with $ref.

@czlowiek488
Copy link

I belive that if you can create mutual enum, it would be nice to add way specify somehow const using values from referenced enum.

@jonaslagoni
Copy link
Member Author

Hmm...

@czlowiek488 just to make it perfectly clear what you are looking for, please correct me if I am wrong 😄

Given the input of the following JSON Schema:

{
  $id: "Address",
  "type": "object",
  "properties": {
    "prop1": { "$ref": "#/definitions/enum1" },
    "prop2": { "$ref": "#/definitions/enum2" },
  },
  "definitions": {
    "enum1": { "enum": ["VAL1", "VAL2"] },
    "enum2": { "enum": ["VAL3", "VAL4"] },
  }
}

You expect the output of the class to be:

export class Address {
  private _prop1?: Enum1;
  private _prop2?: Enum2;
  ...
}

Instead of what it currently will generate:

export class Address {
  private _prop1?: "VAL1" | "VAL2";
  private _prop2?: "VAL3" | "VAL3";
  ...
}

@czlowiek488
Copy link

@jonaslagoni Yes, exactly.

@czlowiek488
Copy link

Also it would be awesome if I could make use of enum to specify a value in interface.

{
  $id: "Address",
  "type": "object",
  "properties": {
    "prop1": { 
        "const": "$ref": "#/definitions/enum1/VAL1"
    },
  },
  "definitions": {
    "enum1": { "enum": ["VAL1", "VAL2"] },
  }
}
export interface Address {
 prop1: Enum1.VAL1;
}

@czlowiek488
Copy link

@jonaslagoni After a bit of thinking, it may be good to have a way to specify enums keys and values to achieve something like here.

export enum Data {
dataA = "data A value",
...
}

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
1.7% 1.7% Duplication

@jonaslagoni
Copy link
Member Author

@czlowiek488 would you mind creating an issue with your use case?

@jonaslagoni jonaslagoni merged commit 697a2d0 into asyncapi:master Apr 27, 2021
@jonaslagoni jonaslagoni deleted the feature/render_enum_in_properties branch April 27, 2021 10:01
@asyncapi-bot
Copy link
Contributor

🎉 This PR is included in version 0.4.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