@typespec/emitter-framework 0.11.0-dev.2 → 0.11.0-dev.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -130,7 +130,7 @@ it("renders a class declaration with properties using component override", async
130
130
  {
131
131
  public required string Prop1 { get; set; }
132
132
  public required int Prop2 { get; set; }
133
- public Bar Prop3 { get; set; }
133
+ public Bar? Prop3 { get; set; }
134
134
  }
135
135
  }
136
136
  `);
@@ -9,7 +9,7 @@ import { getDocComments } from "../utils/doc-comments.js";
9
9
  * Create a C# property declaration from a TypeSpec property type.
10
10
  */
11
11
  export function Property(props) {
12
- const result = preprocessPropertyType(props.type.type);
12
+ const result = preprocessPropertyType(props.type);
13
13
  const {
14
14
  $
15
15
  } = useTsp();
@@ -57,7 +57,14 @@ function JsonNameAttribute(props) {
57
57
  }
58
58
  });
59
59
  }
60
- function preprocessPropertyType(type) {
60
+ function preprocessPropertyType(prop) {
61
+ const type = prop.type;
62
+ if (prop.optional) {
63
+ return {
64
+ type,
65
+ nullable: true
66
+ };
67
+ }
61
68
  const {
62
69
  $
63
70
  } = useTsp();
@@ -60,6 +60,30 @@ it("maps prop: string | null to nullable property", async () => {
60
60
  }
61
61
  `);
62
62
  });
63
+ it("maps optional properties to nullable properties", async () => {
64
+ const {
65
+ prop1
66
+ } = await tester.compile(t.code`
67
+ model TestModel {
68
+ ${t.modelProperty("prop1")}?: string;
69
+ }
70
+ `);
71
+ expect(_$createComponent(Wrapper, {
72
+ get children() {
73
+ return _$createComponent(Property, {
74
+ type: prop1
75
+ });
76
+ }
77
+ })).toRenderTo(`
78
+ namespace TestNamespace
79
+ {
80
+ class Test
81
+ {
82
+ public string? Prop1 { get; set; }
83
+ }
84
+ }
85
+ `);
86
+ });
63
87
  describe("jsonAttributes", () => {
64
88
  it("adds [JsonNameAttribute]", async () => {
65
89
  const {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typespec/emitter-framework",
3
- "version": "0.11.0-dev.2",
3
+ "version": "0.11.0-dev.3",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "repository": {
@@ -108,7 +108,7 @@ it("renders a class declaration with properties using component override", async
108
108
  {
109
109
  public required string Prop1 { get; set; }
110
110
  public required int Prop2 { get; set; }
111
- public Bar Prop3 { get; set; }
111
+ public Bar? Prop3 { get; set; }
112
112
  }
113
113
  }
114
114
  `);
@@ -47,6 +47,28 @@ it("maps prop: string | null to nullable property", async () => {
47
47
  `);
48
48
  });
49
49
 
50
+ it("maps optional properties to nullable properties", async () => {
51
+ const { prop1 } = await tester.compile(t.code`
52
+ model TestModel {
53
+ ${t.modelProperty("prop1")}?: string;
54
+ }
55
+ `);
56
+
57
+ expect(
58
+ <Wrapper>
59
+ <Property type={prop1} />
60
+ </Wrapper>,
61
+ ).toRenderTo(`
62
+ namespace TestNamespace
63
+ {
64
+ class Test
65
+ {
66
+ public string? Prop1 { get; set; }
67
+ }
68
+ }
69
+ `);
70
+ });
71
+
50
72
  describe("jsonAttributes", () => {
51
73
  it("adds [JsonNameAttribute]", async () => {
52
74
  const { prop1 } = await tester.compile(t.code`
@@ -16,7 +16,7 @@ export interface PropertyProps {
16
16
  * Create a C# property declaration from a TypeSpec property type.
17
17
  */
18
18
  export function Property(props: PropertyProps): Children {
19
- const result = preprocessPropertyType(props.type.type);
19
+ const result = preprocessPropertyType(props.type);
20
20
  const { $ } = useTsp();
21
21
 
22
22
  return (
@@ -44,7 +44,13 @@ function JsonNameAttribute(props: JsonNameAttributeProps): Children {
44
44
  return <Attribute name="System.Text.Json.JsonPropertyName" args={[JSON.stringify(jsonName)]} />;
45
45
  }
46
46
 
47
- function preprocessPropertyType(type: Type): { type: Type; nullable: boolean } {
47
+ function preprocessPropertyType(prop: ModelProperty): { type: Type; nullable: boolean } {
48
+ const type = prop.type;
49
+
50
+ if (prop.optional) {
51
+ return { type, nullable: true };
52
+ }
53
+
48
54
  const { $ } = useTsp();
49
55
 
50
56
  if (type.kind === "Union") {