Skip to content

Commit 976c6d0

Browse files
authored
Fix flatten property generation error for all readOnly (#3668)
* fix * regen code * update * revert * Fix missing newline at end of package.json
1 parent 09452f5 commit 976c6d0

2 files changed

Lines changed: 83 additions & 3 deletions

File tree

packages/typespec-ts/src/modular/helpers/operationHelpers.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,9 +1219,6 @@ function getSerializationExpressionForFlatten(
12191219
!isReadOnly(p) &&
12201220
!isMetadata(context.program, p.__raw!)
12211221
);
1222-
if (validProps.length === 0) {
1223-
return `undefined`;
1224-
}
12251222
const optionalPrefix = property.optional
12261223
? `${resolveReference(SerializationHelpers.areAllPropsUndefined)}(${propertyPath}, [${validProps
12271224
.map((p) => `"${p.name}"`)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Should handle flatten model with all readonly properties correctly
2+
3+
## TypeSpec
4+
5+
This is tsp definition.
6+
7+
```tsp
8+
9+
model SolutionProperties {
10+
@visibility(Lifecycle.Read)
11+
solutionId?: string;
12+
13+
@visibility(Lifecycle.Read)
14+
title?: string;
15+
16+
@visibility(Lifecycle.Read)
17+
content?: string;
18+
}
19+
model Solution{
20+
@Azure.ClientGenerator.Core.Legacy.flattenProperty
21+
properties: SolutionProperties;
22+
@Azure.ClientGenerator.Core.Legacy.flattenProperty
23+
propertiesOptional?: SolutionProperties;
24+
}
25+
op test(@body body:Solution):void;
26+
27+
```
28+
29+
Enable the raw content with TCGC dependency.
30+
31+
```yaml
32+
needTCGC: true
33+
```
34+
35+
## Models
36+
37+
```ts models
38+
import { areAllPropsUndefined } from "../static-helpers/serialization/check-prop-undefined.js";
39+
40+
/**
41+
* This file contains only generated model types and their (de)serializers.
42+
* Disable the following rules for internal models with '_' prefix and deserializers which require 'any' for raw JSON input.
43+
*/
44+
/* eslint-disable @typescript-eslint/naming-convention */
45+
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
46+
/** model interface Solution */
47+
export interface Solution {
48+
readonly solutionId?: string;
49+
readonly title?: string;
50+
readonly content?: string;
51+
readonly solutionIdPropertiesOptionalSolutionId?: string;
52+
readonly titlePropertiesOptionalTitle?: string;
53+
readonly contentPropertiesOptionalContent?: string;
54+
}
55+
56+
export function solutionSerializer(item: Solution): any {
57+
return {
58+
properties: _solutionPropertiesSerializer(item),
59+
propertiesOptional: areAllPropsUndefined(item, [])
60+
? undefined
61+
: _solutionPropertiesOptionalSerializer(item),
62+
};
63+
}
64+
65+
/** model interface SolutionProperties */
66+
export interface SolutionProperties {
67+
readonly solutionId?: string;
68+
readonly title?: string;
69+
readonly content?: string;
70+
}
71+
72+
export function solutionPropertiesSerializer(item: SolutionProperties): any {
73+
return item;
74+
}
75+
76+
export function _solutionPropertiesSerializer(item: Solution): any {
77+
return item;
78+
}
79+
80+
export function _solutionPropertiesOptionalSerializer(item: Solution): any {
81+
return item;
82+
}
83+
```

0 commit comments

Comments
 (0)