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

Models not generated in typescript SDK #2033

Closed
zoeyzuo-se opened this issue Sep 22, 2023 · 6 comments · Fixed by #2054
Closed

Models not generated in typescript SDK #2033

zoeyzuo-se opened this issue Sep 22, 2023 · 6 comments · Fixed by #2054
Assignees
Labels
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 RLC

Comments

@zoeyzuo-se
Copy link

Hi team,

I'm using @azure-tools/typespec-ts emitter to generate RLC SDK from typespec files directly but facing an issue where some of the models are not generated in the RLC SDK. This can be reproduced by using the below typespec. Identifers and SearchFilter were not generated even though they are referenced by another property.

import "@typespec/openapi";
import "@typespec/http";
import "@typespec/json-schema";
using TypeSpec.Http;
using OpenAPI;
using TypeSpec.JsonSchema;

@service({
  title: "Sample API",  
  termsOfService: "http://swagger.io/terms/",
  version: "0.1",
})
@server("https://example.com", "Single server endpoint")

@route("/")
namespace SampleAPI {
    @route("/pets")
    interface PetOps {
        @get list(...ItemPerPage, @query search?: SearchFilter, ...FieldsFilter) : {@header Schema: string;...CustomResponse} | Errors;
    }

    @route("/cat")
    interface CatOps extends ResourceService<CatData,CustomResponse> {}
}

interface ResourceService <CatData, CustomResponse> {
    @doc("Read resource")
    @get read(...CatData): {@header Schema: string;...CustomResponse}| Errors;
}

@jsonSchema
model CatData is Resource <"CatData"> {}

model Resource <resourceType extends string> extends Identifiers{
    @visibility("read") type: resourceType;
    @visibility("read") id: string;
    summary: string;
    tags: string[];
}  

model CustomResponse {
    body: string;
    id: string;
    name: string;
}

model Identifiers {
    @visibility("read") type: string;
    id?: string;
    space?: string;
    name?: string;
}
model ItemPerPage {
    @query
    @minValue(1)
    @maxValue(100)
    itemPerPage: int32 = 50,
}
model SearchFilter{
    names: string[],
    codes: string[],
    tags: string[],
}
model FieldsFilter{
    @query fields?: string
}
alias Errors = {
@statusCode statusCode: 400 | 401 | 403 | 404 | 405 | 406 | 408 | 410 |  428 | 429 |
                        500 | 501 | 503 ; ...ServiceErrorResponse};

model ServiceErrorResponse {
    error: InnerErrorResponse
}

@error
model InnerErrorResponse {
    code: int32 | null;
    message: string | null;
    id: string | null,
    status: string | null
}

Dependencies (please note that I didn't use the latest version with @typespec/compiler v0.48 because it's throwing out error when compiling)

    "@typespec/compiler": "^0.47.0",
    "@typespec/json-schema": "^0.47.0",
    "@typespec/openapi3": "^0.47.0",
    "@typespec/rest": "^0.47.0",
    "@azure-tools/typespec-client-generator-core": "~0.33.0",
    "@azure-tools/typespec-azure-core": "~0.33.0",
    "@azure-tools/typespec-ts": "~0.15.0"

Would be appreciated if someone could take a look :) And please let me know if you need more information on this.

@microsoft-github-policy-service microsoft-github-policy-service bot 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 Sep 22, 2023
@qiaozha qiaozha added the RLC label Sep 25, 2023
@qiaozha qiaozha self-assigned this Sep 25, 2023
@qiaozha
Copy link
Member

qiaozha commented Sep 25, 2023

@zoeyzuo-se There're two kinds of issues here, the first one is about template arguments, the second one is about using a model in query parameters.
For the first one, I want to know if you prefer the generated code also use templates ? something like:

interface Identifiers {
  id?: string;
  space?: string;
  name?: string;
}
interface Resource<T extends string> extends Identifiers {
  readonly type: T;
  readonly id: string;
  summary: string;
  tags: string[];
}
type CatDate = Resource<"CatData">;

or just generate something like the below is also okay?

interface Identifiers {
  id?: string;
  space?: string;
  name?: string;
}
interface CatData extends Identifiers {
  readonly type: "CatData";
  readonly id: string;
  summary: string;
  tags: string[];
}

For the second one, I wonder how you want to deserialize the model in query parameter ? According to the OpenAPI3 definition about query parameters here https://swagger.io/docs/specification/describing-parameters/#query-parameters
when the query parameter is an object, the object can be serialized as the following way:

form – /points?color=R,100,G,200,B,150 or /points?R=100&G=200&B=150, depending on the explode keyword
deepObject – /points?color[R]=100&color[G]=200&color[B]=150

@xirzec as this is https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/core/core-client-rest/src/urlHelpers.ts#L36-L58 how we build the query parameters in the url now, I think we should also expand the type to object for it in core-client-rest ? Or codegen should just generate some serializationHelper and pass it as a string to the core-client-rest.

@qiaozha
Copy link
Member

qiaozha commented Sep 25, 2023

May also need some design input from TypeSpec about how to specify the serialization for object in query parameters. see microsoft/typespec#2476

@zoeyzuo-se
Copy link
Author

Hi @qiaozha, thanks for your reply.

For the first issue (with Identifiers) the second option would be ok for us.

For the second issue (with SearchFilter being an object in query parameters) we would go for the format using "form".

@zoeyzuo-se
Copy link
Author

Hi @qiaozha, wondering if there are any updates on this issue?

We can bypass the second issue by redesigning our implementation in typespec by not using an object in query parameters.

But the first issue still exists. I guess it's because using extends makes typespec-ts emitter think that the base model isn't being used.

I have another example of this:

model VegetableCarrot extends Record<Carrots[]> {}
model VegetableBeans extends Record<Beans[]> {}

model Vegetables {
  carrots: VegetableCarrot,
  beans: VegetableBeans
}
model Carrots {
  color: string,
  id: string
}
model Beans {
  expiry: string,
  id: string
}

In the above example, both Carrots and Beans didn't get generated in the typescript models

@qiaozha
Copy link
Member

qiaozha commented Oct 8, 2023

sorry, we are on a holiday leave, I will take a look.

@qiaozha
Copy link
Member

qiaozha commented Oct 19, 2023

@zoeyzuo-se I think the model missing issue should have been resolved with the latest release https://www.npmjs.com/package/@azure-tools/typespec-ts/v/0.17.1. Also create another issue #2071 to track the object type parameters in path and query support. Feel free to let me know if there's anything I am missing :)

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. 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.

2 participants