Skip to content

Add Spector case for Flatten property#3722

Merged
v-jiaodi merged 19 commits intoAzure:mainfrom
v-jiaodi:add-flatten-case
Mar 12, 2026
Merged

Add Spector case for Flatten property#3722
v-jiaodi merged 19 commits intoAzure:mainfrom
v-jiaodi:add-flatten-case

Conversation

@v-jiaodi
Copy link
Member

@v-jiaodi v-jiaodi commented Feb 3, 2026

related pr: Azure/typespec-azure#3729
add case:

  1. Flatten property case for an non-model type like unknown, typespec like this:
model XXX {
  #suppress "@azure-tools/typespec-azure-core/no-unknown" "FIXME: Update justification, follow aka.ms/tsp/conversion-fix for details"
  properties?: unknown;
}

@@Azure.ClientGenerator.Core.Legacy.flattenProperty(XXX.properties);
  1. Flatten property case for all readOnly, typespec like this:
model SolutionProperties {
  @visibility(Lifecycle.Read)
  solutionId?: string;
  @visibility(Lifecycle.Read)
  title?: string;
  @visibility(Lifecycle.Read)
  content?: string;
}
model Solution{
  @Azure.ClientGenerator.Core.Legacy.flattenProperty
  properties: SolutionProperties;
  @Azure.ClientGenerator.Core.Legacy.flattenProperty
  propertiesOptional?: SolutionProperties;
}
op test(@body body:Solution):void;

@MaryGao
Copy link
Member

MaryGao commented Feb 13, 2026

Should be merged Azure/typespec-azure#3729

Comment on lines +505 to +524
// For flatten properties, if all properties are readonly, return empty object
// Otherwise, return the item itself
if (options.flatten) {
// Change parameter name to _item to indicate it's intentionally unused
serializerFunction.parameters = [
{
name: "_item",
type: options.flatten
? resolveReference(refkey(options.flatten.baseModel))
: resolveReference(refkey(type))
}
];
output.push(`
return {};
`);
} else {
output.push(`
return item;
`);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

For flatten serializer, shouldn’t return item.

Image

@v-jiaodi v-jiaodi marked this pull request as ready for review March 6, 2026 09:45
output.push(`
return ${serializeContent}
`);
} else if (options.flatten) {
Copy link
Member

@MaryGao MaryGao Mar 9, 2026

Choose a reason for hiding this comment

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

why this is for all read-only ones?

do we have a case for not all read-only ones?

Copy link
Member Author

Choose a reason for hiding this comment

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

add ut for not all read-only ones

Copy link
Member Author

Choose a reason for hiding this comment

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

Co-authored-by: Mary Gao <yanmeigao1210@gmail.com>
return ${serializeContent}
`);
} else if (options.flatten) {
} else if (propertiesStr.length && options.flatten) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This logic is the same as the first one, which means this code path will never be executed.

@qiaozha qiaozha assigned MaryGao and v-jiaodi and unassigned v-jiaodi Mar 11, 2026
@qiaozha qiaozha added HRLC p0 priority 0 labels Mar 11, 2026
@v-jiaodi v-jiaodi merged commit 69253d2 into Azure:main Mar 12, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

HRLC p0 priority 0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants