From 778140abdac5641107235ae0bb221814d125802d Mon Sep 17 00:00:00 2001 From: Qiaoqiao Zhang <55688292+qiaozha@users.noreply.github.com> Date: Mon, 27 May 2024 15:21:09 +0800 Subject: [PATCH] treat-unknown-as-any-in-modular (#2546) * treat-unknown-as-any-in-modular * regen smoke test * update unit test case * fix ci error * fix smoke test ci error --------- Co-authored-by: Jiao Di (MSFT) <80496810+v-jiaodi@users.noreply.github.com> --- .../review/ai-chat-protocol.api.md | 20 +++++++++---------- .../typespec-ts/src/models/models.ts | 20 +++++++++---------- .../typespec-ts/review/eventgrid.api.md | 2 +- .../typespec-ts/src/models/models.ts | 2 +- .../typespec-ts/review/openai-generic.api.md | 2 +- .../typespec-ts/src/models/models.ts | 3 +-- .../typespec-ts/review/openai_modular.api.md | 2 +- .../typespec-ts/src/models/models.ts | 2 +- .../review/openai-non-branded.api.md | 2 +- .../typespec-ts/src/models/models.ts | 3 +-- .../src/modular/helpers/typeHelpers.ts | 2 +- .../type/array/src/api/unknownValue/index.ts | 8 ++++---- .../array/src/classic/unknownValue/index.ts | 9 +++------ .../src/models/models.ts | 4 ++-- .../generated/src/models/models.ts | 8 ++++---- .../type/scalar/src/api/unknown/index.ts | 8 ++++---- .../type/scalar/src/classic/unknown/index.ts | 6 +++--- .../test/modularUnit/typeHelpers.spec.ts | 4 ++-- 18 files changed, 51 insertions(+), 56 deletions(-) diff --git a/packages/typespec-test/test/chatApi_modular/generated/typespec-ts/review/ai-chat-protocol.api.md b/packages/typespec-test/test/chatApi_modular/generated/typespec-ts/review/ai-chat-protocol.api.md index 79715c3bb8..8b4a445fc4 100644 --- a/packages/typespec-test/test/chatApi_modular/generated/typespec-ts/review/ai-chat-protocol.api.md +++ b/packages/typespec-test/test/chatApi_modular/generated/typespec-ts/review/ai-chat-protocol.api.md @@ -15,11 +15,11 @@ export type APIVersion = "2023-10-01-preview"; // @public export interface ChatChoiceRecord { - context?: Record; + context?: Record; finishReason: FinishReason; index: number; message: ChatMessage; - sessionState?: unknown; + sessionState?: any; } // @public @@ -29,9 +29,9 @@ export interface ChatCompletionChunkRecord { // @public export interface ChatCompletionOptionsRecord { - context?: Record; + context?: Record; messages: ChatMessage[]; - sessionState?: unknown; + sessionState?: any; stream: false; } @@ -44,14 +44,14 @@ export interface ChatCompletionRecord { export interface ChatMessage { content: string; role: ChatRole; - sessionState?: unknown; + sessionState?: any; } // @public export interface ChatMessageDelta { content?: string; role?: ChatRole; - sessionState?: unknown; + sessionState?: any; } // @public (undocumented) @@ -71,11 +71,11 @@ export type ChatRole = "user" | "system" | "assistant"; // @public export interface ChoiceDeltaRecord { - context?: Record; + context?: Record; delta: ChatMessageDelta; finishReason?: FinishReason; index: number; - sessionState?: unknown; + sessionState?: any; } // @public (undocumented) @@ -91,9 +91,9 @@ export type FinishReason = "stop" | "length"; // @public export interface StreamingChatCompletionOptionsRecord { - context?: Record; + context?: Record; messages: ChatMessage[]; - sessionState?: unknown; + sessionState?: any; stream: true; } diff --git a/packages/typespec-test/test/chatApi_modular/generated/typespec-ts/src/models/models.ts b/packages/typespec-test/test/chatApi_modular/generated/typespec-ts/src/models/models.ts index ebf8433659..4c251689c5 100644 --- a/packages/typespec-test/test/chatApi_modular/generated/typespec-ts/src/models/models.ts +++ b/packages/typespec-test/test/chatApi_modular/generated/typespec-ts/src/models/models.ts @@ -13,7 +13,7 @@ export interface ChatMessage { * sends a new one. The data in this field can be used to implement stateful services, such as remembering previous * conversations or user preferences. */ - sessionState?: unknown; + sessionState?: any; } /** A representation of the intended purpose of a message. */ @@ -32,12 +32,12 @@ export interface StreamingChatCompletionOptionsRecord { * sends a new one. The data in this field can be used to implement stateful services, such as remembering previous * conversations or user preferences. */ - sessionState?: unknown; + sessionState?: any; /** * Context allows the chat app to receive extra parameters from the client, such as temperature, functions, or * customer_info. These parameters are specific to the chat app and not understood by the generic clients. */ - context?: Record; + context?: Record; } /** A single response to a streaming completion request. */ @@ -58,12 +58,12 @@ export interface ChoiceDeltaRecord { * sends a new one. The data in this field can be used to implement stateful services, such as remembering previous * conversations or user preferences. */ - sessionState?: unknown; + sessionState?: any; /** * Context allows the chat app to receive extra parameters from the client, such as temperature, functions, or * customer_info. These parameters are specific to the chat app and not understood by the generic clients. */ - context?: Record; + context?: Record; /** The reason this chat completion completed its generation. */ finishReason?: FinishReason; } @@ -80,7 +80,7 @@ export interface ChatMessageDelta { * sends a new one. The data in this field can be used to implement stateful services, such as remembering previous * conversations or user preferences. */ - sessionState?: unknown; + sessionState?: any; } /** Representation of the reason why a chat session has finished processing. */ @@ -99,12 +99,12 @@ export interface ChatCompletionOptionsRecord { * sends a new one. The data in this field can be used to implement stateful services, such as remembering previous * conversations or user preferences. */ - sessionState?: unknown; + sessionState?: any; /** * Context allows the chat app to receive extra parameters from the client, such as temperature, functions, or * customer_info. These parameters are specific to the chat app and not understood by the generic clients. */ - context?: Record; + context?: Record; } /** Representation of the response to a chat completion request. */ @@ -125,12 +125,12 @@ export interface ChatChoiceRecord { * sends a new one. The data in this field can be used to implement stateful services, such as remembering previous * conversations or user preferences. */ - sessionState?: unknown; + sessionState?: any; /** * Context allows the chat app to receive extra parameters from the client, such as temperature, functions, or * customer_info. These parameters are specific to the chat app and not understood by the generic clients. */ - context?: Record; + context?: Record; /** The reason this chat completion completed its generation. */ finishReason: FinishReason; } diff --git a/packages/typespec-test/test/eventgrid_modular/generated/typespec-ts/review/eventgrid.api.md b/packages/typespec-test/test/eventgrid_modular/generated/typespec-ts/review/eventgrid.api.md index 9afa0f4667..56f9ee66d1 100644 --- a/packages/typespec-test/test/eventgrid_modular/generated/typespec-ts/review/eventgrid.api.md +++ b/packages/typespec-test/test/eventgrid_modular/generated/typespec-ts/review/eventgrid.api.md @@ -33,7 +33,7 @@ export interface BrokerProperties { // @public export interface CloudEvent { - data?: unknown; + data?: any; dataBase64?: Uint8Array; datacontenttype?: string; dataschema?: string; diff --git a/packages/typespec-test/test/eventgrid_modular/generated/typespec-ts/src/models/models.ts b/packages/typespec-test/test/eventgrid_modular/generated/typespec-ts/src/models/models.ts index 4c955ad706..a49314530e 100644 --- a/packages/typespec-test/test/eventgrid_modular/generated/typespec-ts/src/models/models.ts +++ b/packages/typespec-test/test/eventgrid_modular/generated/typespec-ts/src/models/models.ts @@ -8,7 +8,7 @@ export interface CloudEvent { /** Identifies the context in which an event happened. The combination of id and source must be unique for each distinct event. */ source: string; /** Event data specific to the event type. */ - data?: unknown; + data?: any; /** Event data specific to the event type, encoded as a base64 string. */ dataBase64?: Uint8Array; /** Type of event related to the originating occurrence. */ diff --git a/packages/typespec-test/test/openai_generic/generated/typespec-ts/review/openai-generic.api.md b/packages/typespec-test/test/openai_generic/generated/typespec-ts/review/openai-generic.api.md index 517ca4cb58..ab8a03898d 100644 --- a/packages/typespec-test/test/openai_generic/generated/typespec-ts/review/openai-generic.api.md +++ b/packages/typespec-test/test/openai_generic/generated/typespec-ts/review/openai-generic.api.md @@ -47,7 +47,7 @@ export interface ChatCompletionFunctionCallOption { } // @public (undocumented) -export interface ChatCompletionFunctionParameters extends Record { +export interface ChatCompletionFunctionParameters extends Record { } // @public (undocumented) diff --git a/packages/typespec-test/test/openai_generic/generated/typespec-ts/src/models/models.ts b/packages/typespec-test/test/openai_generic/generated/typespec-ts/src/models/models.ts index 0a0ad58589..5373dea72e 100644 --- a/packages/typespec-test/test/openai_generic/generated/typespec-ts/src/models/models.ts +++ b/packages/typespec-test/test/openai_generic/generated/typespec-ts/src/models/models.ts @@ -773,8 +773,7 @@ export interface ChatCompletionFunctions { parameters: ChatCompletionFunctionParameters; } -export interface ChatCompletionFunctionParameters - extends Record {} +export interface ChatCompletionFunctionParameters extends Record {} export interface ChatCompletionFunctionCallOption { /** The name of the function to call. */ diff --git a/packages/typespec-test/test/openai_modular/generated/typespec-ts/review/openai_modular.api.md b/packages/typespec-test/test/openai_modular/generated/typespec-ts/review/openai_modular.api.md index 1521659d64..009df1e822 100644 --- a/packages/typespec-test/test/openai_modular/generated/typespec-ts/review/openai_modular.api.md +++ b/packages/typespec-test/test/openai_modular/generated/typespec-ts/review/openai_modular.api.md @@ -689,7 +689,7 @@ export type FunctionCallPreset = "auto" | "none"; export interface FunctionDefinition { description?: string; name: string; - parameters?: unknown; + parameters?: any; } // @public diff --git a/packages/typespec-test/test/openai_modular/generated/typespec-ts/src/models/models.ts b/packages/typespec-test/test/openai_modular/generated/typespec-ts/src/models/models.ts index e43bbec48a..344fa6e520 100644 --- a/packages/typespec-test/test/openai_modular/generated/typespec-ts/src/models/models.ts +++ b/packages/typespec-test/test/openai_modular/generated/typespec-ts/src/models/models.ts @@ -660,7 +660,7 @@ export interface FunctionDefinition { */ description?: string; /** The parameters the function accepts, described as a JSON Schema object. */ - parameters?: unknown; + parameters?: any; } /** diff --git a/packages/typespec-test/test/openai_non_branded/generated/typespec-ts/review/openai-non-branded.api.md b/packages/typespec-test/test/openai_non_branded/generated/typespec-ts/review/openai-non-branded.api.md index c46941e3ec..526daf448c 100644 --- a/packages/typespec-test/test/openai_non_branded/generated/typespec-ts/review/openai-non-branded.api.md +++ b/packages/typespec-test/test/openai_non_branded/generated/typespec-ts/review/openai-non-branded.api.md @@ -47,7 +47,7 @@ export interface ChatCompletionFunctionCallOption { } // @public (undocumented) -export interface ChatCompletionFunctionParameters extends Record { +export interface ChatCompletionFunctionParameters extends Record { } // @public (undocumented) diff --git a/packages/typespec-test/test/openai_non_branded/generated/typespec-ts/src/models/models.ts b/packages/typespec-test/test/openai_non_branded/generated/typespec-ts/src/models/models.ts index 524f4b2aca..fb6cce2da7 100644 --- a/packages/typespec-test/test/openai_non_branded/generated/typespec-ts/src/models/models.ts +++ b/packages/typespec-test/test/openai_non_branded/generated/typespec-ts/src/models/models.ts @@ -772,8 +772,7 @@ export interface ChatCompletionFunctions { parameters: ChatCompletionFunctionParameters; } -export interface ChatCompletionFunctionParameters - extends Record {} +export interface ChatCompletionFunctionParameters extends Record {} export interface ChatCompletionFunctionCallOption { /** The name of the function to call. */ diff --git a/packages/typespec-ts/src/modular/helpers/typeHelpers.ts b/packages/typespec-ts/src/modular/helpers/typeHelpers.ts index 6131c2ee4c..45c39e3806 100644 --- a/packages/typespec-ts/src/modular/helpers/typeHelpers.ts +++ b/packages/typespec-ts/src/modular/helpers/typeHelpers.ts @@ -30,7 +30,7 @@ const simpleTypeMap: Record = { "byte-array": { name: "Uint8Array" }, string: { name: "string" }, any: { name: "Record" }, - unknown: { name: "unknown" } + unknown: { name: "any" } }; function handleAnomymousModelName(type: Type) { diff --git a/packages/typespec-ts/test/modularIntegration/generated/type/array/src/api/unknownValue/index.ts b/packages/typespec-ts/test/modularIntegration/generated/type/array/src/api/unknownValue/index.ts index cf6792204d..db9319be65 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/type/array/src/api/unknownValue/index.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/type/array/src/api/unknownValue/index.ts @@ -27,7 +27,7 @@ export function _unknownValueGetSend( export async function _unknownValueGetDeserialize( result: UnknownValueGet200Response, -): Promise { +): Promise { if (result.status !== "200") { throw createRestError(result); } @@ -38,14 +38,14 @@ export async function _unknownValueGetDeserialize( export async function unknownValueGet( context: Client, options: UnknownValueGetOptionalParams = { requestOptions: {} }, -): Promise { +): Promise { const result = await _unknownValueGetSend(context, options); return _unknownValueGetDeserialize(result); } export function _unknownValuePutSend( context: Client, - body: unknown[], + body: any[], options: UnknownValuePutOptionalParams = { requestOptions: {} }, ): StreamableMethod { return context @@ -65,7 +65,7 @@ export async function _unknownValuePutDeserialize( export async function unknownValuePut( context: Client, - body: unknown[], + body: any[], options: UnknownValuePutOptionalParams = { requestOptions: {} }, ): Promise { const result = await _unknownValuePutSend(context, body, options); diff --git a/packages/typespec-ts/test/modularIntegration/generated/type/array/src/classic/unknownValue/index.ts b/packages/typespec-ts/test/modularIntegration/generated/type/array/src/classic/unknownValue/index.ts index 14fc8261c6..87c97be578 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/type/array/src/classic/unknownValue/index.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/type/array/src/classic/unknownValue/index.ts @@ -12,18 +12,15 @@ import { } from "../../models/options.js"; export interface UnknownValueOperations { - get: (options?: UnknownValueGetOptionalParams) => Promise; - put: ( - body: unknown[], - options?: UnknownValuePutOptionalParams, - ) => Promise; + get: (options?: UnknownValueGetOptionalParams) => Promise; + put: (body: any[], options?: UnknownValuePutOptionalParams) => Promise; } export function getUnknownValue(context: ArrayContext) { return { get: (options?: UnknownValueGetOptionalParams) => unknownValueGet(context, options), - put: (body: unknown[], options?: UnknownValuePutOptionalParams) => + put: (body: any[], options?: UnknownValuePutOptionalParams) => unknownValuePut(context, body, options), }; } diff --git a/packages/typespec-ts/test/modularIntegration/generated/type/property/additional-properties/src/models/models.ts b/packages/typespec-ts/test/modularIntegration/generated/type/property/additional-properties/src/models/models.ts index ad22399b50..4f459fa9a4 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/type/property/additional-properties/src/models/models.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/type/property/additional-properties/src/models/models.ts @@ -220,7 +220,7 @@ export interface IsUnknownAdditionalPropertiesDerived /** The model extends from Record with a discriminator. */ export interface ExtendsUnknownAdditionalPropertiesDiscriminated - extends Record { + extends Record { /** The name property */ name: string; /** the discriminator possible values: derived */ @@ -239,7 +239,7 @@ export interface ExtendsUnknownAdditionalPropertiesDiscriminatedDerived /** The model extends from Record type. */ export interface ExtendsUnknownAdditionalProperties - extends Record { + extends Record { /** The name property */ name: string; } diff --git a/packages/typespec-ts/test/modularIntegration/generated/type/property/value-types/generated/src/models/models.ts b/packages/typespec-ts/test/modularIntegration/generated/type/property/value-types/generated/src/models/models.ts index c877c62881..bf0b56b735 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/type/property/value-types/generated/src/models/models.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/type/property/value-types/generated/src/models/models.ts @@ -56,25 +56,25 @@ export interface StringLiteralProperty { /** Model with a property unknown, and the data is an array. */ export interface UnknownArrayProperty { /** Property */ - property: unknown; + property: any; } /** Model with a property unknown, and the data is a dictionnary. */ export interface UnknownDictProperty { /** Property */ - property: unknown; + property: any; } /** Model with a property unknown, and the data is a int32. */ export interface UnknownIntProperty { /** Property */ - property: unknown; + property: any; } /** Model with a property unknown, and the data is a string. */ export interface UnknownStringProperty { /** Property */ - property: unknown; + property: any; } /** Model with a property never. (This property should not be included). */ diff --git a/packages/typespec-ts/test/modularIntegration/generated/type/scalar/src/api/unknown/index.ts b/packages/typespec-ts/test/modularIntegration/generated/type/scalar/src/api/unknown/index.ts index 19d9ca2fbe..83856fa0d2 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/type/scalar/src/api/unknown/index.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/type/scalar/src/api/unknown/index.ts @@ -27,7 +27,7 @@ export function _unknownGetSend( export async function _unknownGetDeserialize( result: UnknownGet200Response, -): Promise { +): Promise { if (result.status !== "200") { throw createRestError(result); } @@ -39,14 +39,14 @@ export async function _unknownGetDeserialize( export async function unknownGet( context: Client, options: UnknownGetOptionalParams = { requestOptions: {} }, -): Promise { +): Promise { const result = await _unknownGetSend(context, options); return _unknownGetDeserialize(result); } export function _unknownPutSend( context: Client, - body: unknown, + body: any, options: UnknownPutOptionalParams = { requestOptions: {} }, ): StreamableMethod { return context @@ -67,7 +67,7 @@ export async function _unknownPutDeserialize( /** put unknown value */ export async function unknownPut( context: Client, - body: unknown, + body: any, options: UnknownPutOptionalParams = { requestOptions: {} }, ): Promise { const result = await _unknownPutSend(context, body, options); diff --git a/packages/typespec-ts/test/modularIntegration/generated/type/scalar/src/classic/unknown/index.ts b/packages/typespec-ts/test/modularIntegration/generated/type/scalar/src/classic/unknown/index.ts index 64565e6ece..71ff6e6a07 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/type/scalar/src/classic/unknown/index.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/type/scalar/src/classic/unknown/index.ts @@ -9,14 +9,14 @@ import { } from "../../models/options.js"; export interface UnknownOperations { - get: (options?: UnknownGetOptionalParams) => Promise; - put: (body: unknown, options?: UnknownPutOptionalParams) => Promise; + get: (options?: UnknownGetOptionalParams) => Promise; + put: (body: any, options?: UnknownPutOptionalParams) => Promise; } export function getUnknown(context: ScalarContext) { return { get: (options?: UnknownGetOptionalParams) => unknownGet(context, options), - put: (body: unknown, options?: UnknownPutOptionalParams) => + put: (body: any, options?: UnknownPutOptionalParams) => unknownPut(context, body, options), }; } diff --git a/packages/typespec-ts/test/modularUnit/typeHelpers.spec.ts b/packages/typespec-ts/test/modularUnit/typeHelpers.spec.ts index a3a55c4329..2b785ed4cc 100644 --- a/packages/typespec-ts/test/modularUnit/typeHelpers.spec.ts +++ b/packages/typespec-ts/test/modularUnit/typeHelpers.spec.ts @@ -497,10 +497,10 @@ describe("typeHelpers", () => { expect(result.name).to.equal("number"); }); - it("should return `unknown` for unknown types", () => { + it("should return `any` for unknown types", () => { const type: Type = { type: "unknown" as any }; // Forcing an unknown type. const result = getType(type); - expect(result.name).to.equal("unknown"); + expect(result.name).to.equal("any"); }); it("should handle polymorphism base", () => {