Skip to content

Commit

Permalink
Add type info for additional properties in model type (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
tadelesh authored Dec 14, 2023
1 parent d6b6a7b commit 92766df
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@azure-tools/typespec-client-generator-core",
"comment": "Add type info for additional properties in model type.",
"type": "none"
}
],
"packageName": "@azure-tools/typespec-client-generator-core"
}
4 changes: 2 additions & 2 deletions packages/typespec-client-generator-core/doc/types.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ model SdkUnionType extends SdkType {
* @property properties: List of properties on the model
* @property access: Access level of the model
* @property usage: Usage cases for the model. Used to filter when people only want input or output models.
* @property additionalProperties: Whether the model has additional properties
* @property additionalProperties: Model's additional properties type, if no additional properties, then undefined
* @property discriminatorValue: Value of the discriminator if this is a discriminated subtype. Will be undefined if not.
* @property discriminatedSubtypes: Mapping of discriminator value to this models discriminated subtypes if there are any.
* @property baseModel: The base model class of this model type if one exists.
Expand All @@ -233,7 +233,7 @@ model SdkModelType extends SdkType {
properties: SdkModelPropertyType[];
access: AccessFlags;
usage: UsageFlags;
additionalProperties: boolean;
additionalProperties?: SdkType;
dicriminatorValue?: string;
discriminatedSubtypes?: Record<SdkModelType>;
baseModel?: SdkModelType;
Expand Down
2 changes: 1 addition & 1 deletion packages/typespec-client-generator-core/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export interface SdkModelType extends SdkTypeBase {
details?: string;
access: AccessFlags | undefined;
usage: UsageFlags;
additionalProperties: boolean;
additionalProperties: SdkType | undefined;
discriminatorValue?: string;
discriminatedSubtypes?: Record<string, SdkModelType>;
baseModel?: SdkModelType;
Expand Down
10 changes: 7 additions & 3 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ export function getSdkModel(context: SdkContext, type: Model, operation?: Operat
description: docWrapper.description,
details: docWrapper.details,
properties: [],
additionalProperties: false, // going to set additional properties in the next few lines when we look at base model
additionalProperties: undefined, // going to set additional properties in the next few lines when we look at base model
access: undefined, // dummy value since we need to update models map before we can set this
usage: UsageFlags.None, // dummy value since we need to update models map before we can set this
crossLanguageDefinitionId: getCrossLanguageDefinitionId(type),
Expand All @@ -502,7 +502,11 @@ export function getSdkModel(context: SdkContext, type: Model, operation?: Operat
updateModelsMap(context, type, sdkType, operation);
// model MyModel is Record<> {} should be model with additional properties
if (type.sourceModel?.kind === "Model" && type.sourceModel?.name === "Record") {
sdkType.additionalProperties = true;
sdkType.additionalProperties = getClientType(
context,
type.sourceModel!.indexer!.value!,
operation
);
}
if (type.baseModel) {
sdkType.baseModel = context.modelsMap?.get(type.baseModel) as SdkModelType | undefined;
Expand All @@ -512,7 +516,7 @@ export function getSdkModel(context: SdkContext, type: Model, operation?: Operat
| SdkModelType;
if (baseModel.kind === "dict") {
// model MyModel extends Record<> {} should be model with additional properties
sdkType.additionalProperties = true;
sdkType.additionalProperties = baseModel.valueType;
} else {
sdkType.baseModel = baseModel;
updateModelsMap(context, type.baseModel, sdkType.baseModel, operation);
Expand Down
10 changes: 5 additions & 5 deletions packages/typespec-client-generator-core/test/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1727,7 +1727,7 @@ describe("typespec-client-generator-core: types", () => {
const Test6 = models.find((x) => x.name === "Test6")!;
strictEqual(Test6.access, undefined);
});
it("additionalProperties true", async () => {
it("additionalProperties string", async () => {
await runner.compileWithBuiltInService(`
@usage(Usage.input | Usage.output)
@access(Access.public)
Expand All @@ -1736,7 +1736,7 @@ describe("typespec-client-generator-core: types", () => {
}
@usage(Usage.input | Usage.output)
@access(Access.public)
model AdditionalPropertiesModel2 is Record<string> {
model AdditionalPropertiesModel2 is Record<unknown> {
prop: string;
}
@usage(Usage.input | Usage.output)
Expand All @@ -1756,11 +1756,11 @@ describe("typespec-client-generator-core: types", () => {
const NonAdditionalPropertiesModel = models.find(
(x) => x.name === "NoAdditionalPropertiesModel"
)! as SdkModelType;
strictEqual(AdditionalPropertiesModel.additionalProperties, true);
strictEqual(AdditionalPropertiesModel.additionalProperties?.kind, "string");
strictEqual(AdditionalPropertiesModel.baseModel, undefined);
strictEqual(AdditionalPropertiesModel2.additionalProperties, true);
strictEqual(AdditionalPropertiesModel2.additionalProperties?.kind, "any");
strictEqual(AdditionalPropertiesModel2.baseModel, undefined);
strictEqual(NonAdditionalPropertiesModel.additionalProperties, false);
strictEqual(NonAdditionalPropertiesModel.additionalProperties, undefined);
});
it("crossLanguageDefinitionId", async () => {
await runner.compile(`
Expand Down

0 comments on commit 92766df

Please sign in to comment.