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

Model type as an enum member #1922

Closed
heycalmdown opened this issue Jul 11, 2023 · 6 comments · Fixed by #1924
Closed

Model type as an enum member #1922

heycalmdown opened this issue Jul 11, 2023 · 6 comments · Fixed by #1924
Assignees
Labels
customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that RLC

Comments

@heycalmdown
Copy link

Let's say we have an enum A;

enum A {
  AA,
  BB,
}

And a model B;

model B {
  a: A,
}

Then, a model C which extends B;

model C extends B {
  a: A.AA,
}

According to the Type relations, a can be A.AA because it can be assignable to A. It's a legit TypeSpec code, and can be compiled with @typespec/openapi3. But @azure-tools/typespec-ts emits the following error;

error @azure-tools/typespec-ts/invalid-schema: Couldn't get schema for type EnumMember
@ghost ghost added customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Jul 11, 2023
@MaryGao
Copy link
Member

MaryGao commented Jul 12, 2023

@heycalmdown Does that mean you want the sub model C has more specific value than base model B? Could you share which version and command you use to generate this code?
I tried in my local with latest emitter, and didn't find the error but with below ouput.

export interface C extends B {}

export interface B {
 /** Possible values: AA, BB */
 a: string;
}

And I think this is not perfect and we need to improve that with below output, how do you think?

export interface C extends B {
    a: "AA"
}

export interface B {
 /** Possible values: AA, BB */
 a: string; // <=== Please notice that the extensible enum would be string
}

@heycalmdown
Copy link
Author

// main.tsp
import "@typespec/rest";

using TypeSpec.Http;

// PAYMENT_METHODS
enum PAYMENT_METHODS {
  CREDIT_CARD: "01",
}

model CommonRegistrationRequest {
  payMethod: PAYMENT_METHODS,
}

model RequestRegisterCC extends CommonRegistrationRequest {
  payMethod: PAYMENT_METHODS.CREDIT_CARD,
}

@service({})
@route("/")
namespace NicepayPlatform {
  @post
  @route("/registration")
  op Registration(@body request: RequestRegisterCC): {};
}
# tspconfig.yaml
emit:
  - '@typespec/openapi3'
  - '@azure-tools/typespec-ts'
  • @azure-tools/typespec-ts" - "version": "0.13.3"
  • @typespec/compiler" - "version": "0.45.2"
$ tsp compile ./typespec/test
TypeSpec compiler v0.45.2

Diagnostics were reported during compilation:

/asdf/typespec/test/main.tsp:7:3 - error @azure-tools/typespec-ts/invalid-schema: Couldn't get schema for type EnumMember
> 7 |   CREDIT_CARD: "01",
    |   ^^^^^^^^^^^^^^^^^
/asdf/typespec/test/main.tsp:7:3 - error @azure-tools/typespec-ts/invalid-schema: Couldn't get schema for type EnumMember
> 7 |   CREDIT_CARD: "01",
    |   ^^^^^^^^^^^^^^^^^
/asdf/typespec/test/main.tsp:7:3 - error @azure-tools/typespec-ts/invalid-schema: Couldn't get schema for type EnumMember
> 7 |   CREDIT_CARD: "01",
    |   ^^^^^^^^^^^^^^^^^
/asdf/typespec/test/main.tsp:7:3 - error @azure-tools/typespec-ts/invalid-schema: Couldn't get schema for type EnumMember
> 7 |   CREDIT_CARD: "01",
    |   ^^^^^^^^^^^^^^^^^

Found 4 errors.

But I could get the same output you shared despite the errors.

@qiaozha qiaozha added the RLC label Jul 12, 2023
@ghost ghost added the needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team label Jul 12, 2023
@MaryGao
Copy link
Member

MaryGao commented Jul 12, 2023

@heycalmdown Thanks for the info! I could re-pro it now and will work on the fix to mitigate the errors and generate like below. Let me know if you have any concern.

export interface C extends B {
    a: "AA"
}

export interface B {
   /** Possible values: AA, BB */
   a: string; 
}

And I would encourage you to add @discriminator under the type CommonRegistrationRequest to explicitly tell the compiler about the polymorphism discriminator property.

// PAYMENT_METHODS
enum PAYMENT_METHODS {
  CREDIT_CARD: "01",
}
@discriminator("payMethod")
model CommonRegistrationRequest {
  payMethod: PAYMENT_METHODS,
}

model RequestRegisterCC extends CommonRegistrationRequest {
  payMethod: PAYMENT_METHODS.CREDIT_CARD,
}

@heycalmdown heycalmdown changed the title Model type as a enum member Model type as an enum member Jul 17, 2023
@MaryGao MaryGao reopened this Jul 18, 2023
@MaryGao
Copy link
Member

MaryGao commented Jul 26, 2023

@heycalmdown we release a new version to fix this, https://www.npmjs.com/package/@azure-tools/typespec-ts/v/0.14.0. Could you have a try?

@MaryGao MaryGao added needs-author-feedback Workflow: More information is needed from author to address the issue. and removed needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team labels Jul 26, 2023
@heycalmdown
Copy link
Author

@MaryGao it works well with the version as expected. Thanks!

@ghost ghost added needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team and removed needs-author-feedback Workflow: More information is needed from author to address the issue. labels Jul 27, 2023
@MaryGao
Copy link
Member

MaryGao commented Jul 27, 2023

Closed as resolved.

@MaryGao MaryGao closed this as completed Jul 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that RLC
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants