-
Notifications
You must be signed in to change notification settings - Fork 80
Add treat-unknown-as-record feature flag to map TypeSpec unknown to Record<string, unknown> in Modular SDK
#3869
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d71ad95
Initial plan
Copilot a90e237
Add treat-unknown-as-record feature flag for Modular SDK unknown type…
Copilot 9d3a94f
Merge branch 'main' into copilot/review-emitter-typespec-generation
MaryGao 6d87a9f
Add noWrap for unknown type when treat-unknown-as-record is enabled
Copilot 2f89791
Merge branch 'main' into copilot/review-emitter-typespec-generation
v-jiaodi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
packages/typespec-ts/test/modularUnit/scenarios/models/treatUnknownAsRecord.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| # treat-unknown-as-record maps unknown property to Record<string, unknown> | ||
|
|
||
| When `treat-unknown-as-record` is enabled, TypeSpec `unknown` type properties should be mapped to `Record<string, unknown>` instead of `any` in Modular SDK. | ||
|
|
||
| ## TypeSpec | ||
|
|
||
| ```tsp | ||
| model Foo { | ||
| bar: unknown; | ||
| baz: unknown[]; | ||
| } | ||
| op read(): Foo; | ||
| ``` | ||
|
|
||
| ```yaml | ||
| treat-unknown-as-record: true | ||
| ``` | ||
|
|
||
| ## Models | ||
|
|
||
| ```ts models interface Foo | ||
| /** | ||
| * This file contains only generated model types and their (de)serializers. | ||
| * Disable the following rules for internal models with '_' prefix and deserializers which require 'any' for raw JSON input. | ||
| */ | ||
| /* eslint-disable @typescript-eslint/naming-convention */ | ||
| /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ | ||
| /** model interface Foo */ | ||
| export interface Foo { | ||
| bar: Record<string, unknown>; | ||
| baz: Record<string, unknown>[]; | ||
| } | ||
| ``` | ||
|
|
||
| # treat-unknown-as-record disabled maps unknown to any | ||
|
|
||
| When `treat-unknown-as-record` is not set, TypeSpec `unknown` type properties should be mapped to `any`. | ||
|
|
||
| ## TypeSpec | ||
|
|
||
| ```tsp | ||
| model Foo { | ||
| bar: unknown; | ||
| baz: unknown[]; | ||
| } | ||
| op read(): Foo; | ||
| ``` | ||
|
|
||
| ## Models | ||
|
|
||
| ```ts models interface Foo | ||
| /** | ||
| * This file contains only generated model types and their (de)serializers. | ||
| * Disable the following rules for internal models with '_' prefix and deserializers which require 'any' for raw JSON input. | ||
| */ | ||
| /* eslint-disable @typescript-eslint/naming-convention */ | ||
| /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ | ||
| /** model interface Foo */ | ||
| export interface Foo { | ||
| bar: any; | ||
| baz: any[]; | ||
| } | ||
| ``` | ||
|
|
||
| # treat-unknown-as-record with wrap-non-model-return does not wrap unknown return type | ||
|
|
||
| When both `treat-unknown-as-record` and `wrap-non-model-return` are enabled, the `unknown` return type should NOT be wrapped (i.e., no `GetXxxResponse` alias is generated). | ||
|
|
||
| ## TypeSpec | ||
|
|
||
| ```tsp | ||
| @route("/any") | ||
| @get | ||
| op getAny(): unknown; | ||
| ``` | ||
|
|
||
| ```yaml | ||
| treat-unknown-as-record: true | ||
| wrap-non-model-return: true | ||
| ``` | ||
|
|
||
| ## Models | ||
|
|
||
| ```ts models | ||
| // (file was not generated) | ||
| ``` | ||
|
|
||
| ## Operations | ||
|
|
||
| ```ts operations | ||
| import { TestingContext as Client } from "./index.js"; | ||
| import { GetAnyOptionalParams } from "./options.js"; | ||
| import { | ||
| StreamableMethod, | ||
| PathUncheckedResponse, | ||
| createRestError, | ||
| operationOptionsToRequestParameters, | ||
| } from "@azure-rest/core-client"; | ||
|
|
||
| export function _getAnySend( | ||
| context: Client, | ||
| options: GetAnyOptionalParams = { requestOptions: {} }, | ||
| ): StreamableMethod { | ||
| return context | ||
| .path("/any") | ||
| .get({ | ||
| ...operationOptionsToRequestParameters(options), | ||
| headers: { accept: "application/json", ...options.requestOptions?.headers }, | ||
| }); | ||
| } | ||
|
|
||
| export async function _getAnyDeserialize( | ||
| result: PathUncheckedResponse, | ||
| ): Promise<Record<string, unknown>> { | ||
| const expectedStatuses = ["200"]; | ||
| if (!expectedStatuses.includes(result.status)) { | ||
| throw createRestError(result); | ||
| } | ||
|
|
||
| return result.body; | ||
| } | ||
|
|
||
| export async function getAny( | ||
| context: Client, | ||
| options: GetAnyOptionalParams = { requestOptions: {} }, | ||
| ): Promise<Record<string, unknown>> { | ||
| const result = await _getAnySend(context, options); | ||
| return _getAnyDeserialize(result); | ||
| } | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot when the treatUnknownAsRecord is enabled, we should generate noWrap for unknown type. also add an UT for unknow as return type for treatUnknownAsRecord on/off.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit
6d87a9f. WhentreatUnknownAsRecordis enabled,checkWrapNonModelReturnnow returnsnoWrapforunknowntype (treating it as a Dictionary/Record type, consistent with HLC'sPropertyKind.Dictionary → no body wrapperbehavior). The return type isRecord<string, unknown>directly instead of being wrapped in{ body: any }.