Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions packages/typespec-ts/src/modular/emitSamples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,20 +571,27 @@ function getParameterValue(
}
let propRetValue;

if (property?.flatten && property.type.kind === "model") {
if (
property?.flatten &&
property.type.kind === "model" &&
options?.overrides?.enableFlatten !== false
) {
// For flatten property, we need to recursively get its properties
// but disable further flattening to match the TypeScript interface structure
const paramValue = getParameterValue(context, propValue, {
overrides: {
propertyRenames:
useContext("sdkTypes").flattenProperties.get(property)
?.conflictMap
?.conflictMap,
enableFlatten: false
}
});
propRetValue =
paramValue.length > 2 ? paramValue.slice(1, -1) : undefined;
} else {
propRetValue =
`"${mapper.get(propName) ?? propName}": ` +
getParameterValue(context, propValue);
getParameterValue(context, propValue, options);
}
if (propRetValue) values.push(propRetValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ export interface ModelOverrideOptions {
// The <Property, Client_Name> map for any renamed properties.
// Mainly because the original client name has collision with other property names during flattening.
propertyRenames?: Map<SdkModelPropertyType, string>;

// If true (default), enable flattening for nested flatten properties when generating samples.
// When false, nested flatten properties are not further flattened to match the TypeScript interface structure.
enableFlatten?: boolean;
}

export function getPropertyWithOverrides(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,14 @@ export interface Test {
}

export function testSerializer(item: Test): any {
return {
bar: item["bar"],
baz: item["baz"],
properties: _testPropertiesSerializer(item)
};
return { bar: item["bar"], baz: item["baz"], properties: _testPropertiesSerializer(item) };
}

export function testDeserializer(item: any): Test {
return {
bar: item["bar"],
baz: item["baz"],
..._testPropertiesDeserializer(item["properties"])
..._testPropertiesDeserializer(item["properties"]),
};
}

Expand All @@ -79,14 +75,14 @@ export function fooPropertiesSerializer(item: FooProperties): any {
export function fooPropertiesDeserializer(item: any): FooProperties {
return {
bar: item["bar"],
baz: item["baz"]
baz: item["baz"],
};
}

/** Known values of {@link Versions} that the service accepts. */
export enum KnownVersions {
/** 2022-05-15-preview */
V20220515Preview = "2022-05-15-preview"
V20220515Preview = "2022-05-15-preview",
}

export function _testPropertiesSerializer(item: Test): any {
Expand All @@ -96,7 +92,7 @@ export function _testPropertiesSerializer(item: Test): any {
export function _testPropertiesDeserializer(item: any) {
return {
barPropertiesBar: item["bar"],
bazPropertiesBaz: item["baz"]
bazPropertiesBaz: item["baz"],
};
}
```
Expand Down Expand Up @@ -171,7 +167,7 @@ export function testSerializer(item: Test): any {
properties: _testPropertiesSerializer(item),
anotherProperties: areAllPropsUndefined(item, ["bar", "baz", "x"])
? undefined
: _testAnotherPropertiesSerializer(item)
: _testAnotherPropertiesSerializer(item),
};
}

Expand All @@ -181,7 +177,7 @@ export function testDeserializer(item: any): Test {
..._testPropertiesDeserializer(item["properties"]),
...(!item["anotherProperties"]
? item["anotherProperties"]
: _testAnotherPropertiesDeserializer(item["anotherProperties"]))
: _testAnotherPropertiesDeserializer(item["anotherProperties"])),
};
}

Expand All @@ -198,7 +194,7 @@ export function fooPropertiesSerializer(item: FooProperties): any {
export function fooPropertiesDeserializer(item: any): FooProperties {
return {
bar: item["bar"],
baz: item["baz"]
baz: item["baz"],
};
}

Expand All @@ -217,14 +213,14 @@ export function xPropertiesDeserializer(item: any): XProperties {
return {
bar: item["bar"],
baz: item["baz"],
x: item["x"]
x: item["x"],
};
}

/** Known values of {@link Versions} that the service accepts. */
export enum KnownVersions {
/** 2022-05-15-preview */
V20220515Preview = "2022-05-15-preview"
V20220515Preview = "2022-05-15-preview",
}

export function _testPropertiesSerializer(item: Test): any {
Expand All @@ -234,23 +230,23 @@ export function _testPropertiesSerializer(item: Test): any {
export function _testPropertiesDeserializer(item: any) {
return {
bar: item["bar"],
baz: item["baz"]
baz: item["baz"],
};
}

export function _testAnotherPropertiesSerializer(item: Test): any {
return {
bar: item["barAnotherPropertiesBar"],
baz: item["bazAnotherPropertiesBaz"],
x: item["x"]
x: item["x"],
};
}

export function _testAnotherPropertiesDeserializer(item: any) {
return {
barAnotherPropertiesBar: item["bar"],
bazAnotherPropertiesBaz: item["baz"],
x: item["x"]
x: item["x"],
};
}
```
Expand Down Expand Up @@ -331,7 +327,7 @@ export function testSerializer(item: Test): any {
properties: _testPropertiesSerializer(item),
anotherProperties: areAllPropsUndefined(item, ["bar", "baz", "result"])
? undefined
: _testAnotherPropertiesSerializer(item)
: _testAnotherPropertiesSerializer(item),
};
}

Expand All @@ -343,7 +339,7 @@ export function testDeserializer(item: any): Test {
..._testPropertiesDeserializer(item["properties"]),
...(!item["anotherProperties"]
? item["anotherProperties"]
: _testAnotherPropertiesDeserializer(item["anotherProperties"]))
: _testAnotherPropertiesDeserializer(item["anotherProperties"])),
};
}

Expand All @@ -360,7 +356,7 @@ export function fooPropertiesSerializer(item: FooProperties): any {
export function fooPropertiesDeserializer(item: any): FooProperties {
return {
bar: item["bar"],
baz: item["baz"]
baz: item["baz"],
};
}

Expand All @@ -379,14 +375,14 @@ export function xPropertiesDeserializer(item: any): XProperties {
return {
bar: item["bar"],
baz: item["baz"],
result: item["result"]
result: item["result"],
};
}

/** Known values of {@link Versions} that the service accepts. */
export enum KnownVersions {
/** 2022-05-15-preview */
V20220515Preview = "2022-05-15-preview"
V20220515Preview = "2022-05-15-preview",
}

export function _testPropertiesSerializer(item: Test): any {
Expand All @@ -396,23 +392,23 @@ export function _testPropertiesSerializer(item: Test): any {
export function _testPropertiesDeserializer(item: any) {
return {
barPropertiesBar: item["bar"],
bazPropertiesBaz: item["baz"]
bazPropertiesBaz: item["baz"],
};
}

export function _testAnotherPropertiesSerializer(item: Test): any {
return {
bar: item["barAnotherPropertiesBar"],
baz: item["bazAnotherPropertiesBaz"],
result: item["resultAnotherPropertiesResult"]
result: item["resultAnotherPropertiesResult"],
};
}

export function _testAnotherPropertiesDeserializer(item: any) {
return {
barAnotherPropertiesBar: item["bar"],
bazAnotherPropertiesBaz: item["baz"],
resultAnotherPropertiesResult: item["result"]
resultAnotherPropertiesResult: item["result"],
};
}
```
Expand Down Expand Up @@ -478,15 +474,15 @@ export function testSerializer(item: Test): any {
return {
result: item["result"],
name: item["name"],
properties: _testPropertiesSerializer(item)
properties: _testPropertiesSerializer(item),
};
}

export function testDeserializer(item: any): Test {
return {
result: item["result"],
name: item["name"],
..._testPropertiesDeserializer(item["properties"])
..._testPropertiesDeserializer(item["properties"]),
};
}

Expand All @@ -505,29 +501,25 @@ export function fooPropertiesDeserializer(item: any): FooProperties {
return {
name: item["name"],
prop1: item["prop1"],
prop2: item["prop2"]
prop2: item["prop2"],
};
}

/** Known values of {@link Versions} that the service accepts. */
export enum KnownVersions {
/** 2022-05-15-preview */
V20220515Preview = "2022-05-15-preview"
V20220515Preview = "2022-05-15-preview",
}

export function _testPropertiesSerializer(item: Test): any {
return {
name: item["namePropertiesName"],
prop1: item["prop1"],
prop2: item["prop2"]
};
return { name: item["namePropertiesName"], prop1: item["prop1"], prop2: item["prop2"] };
}

export function _testPropertiesDeserializer(item: any) {
return {
namePropertiesName: item["name"],
prop1: item["prop1"],
prop2: item["prop2"]
prop2: item["prop2"],
};
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ async function read(): Promise<void> {
baz: "body name",
bazPropertiesBaz: [{ x: "bbb" }],
bar: [{ x: "xx" }],
bazPropertiesBaz: 222,
properties: { baz: 222 },
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls double confirm this is aligned with model gen.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be expected,
we should flatten the properties from the line 65, but not flatten the line 76
image

bazProperties2Baz: 111,
},
});
Expand Down
Loading
Loading