@typespec/emitter-framework 0.13.0-dev.0 → 0.13.0-dev.2

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.
Files changed (35) hide show
  1. package/dist/src/csharp/components/index.d.ts +2 -0
  2. package/dist/src/csharp/components/index.d.ts.map +1 -1
  3. package/dist/src/csharp/components/index.js +2 -0
  4. package/dist/src/csharp/components/index.js.map +1 -1
  5. package/dist/src/csharp/components/json-converter/json-converter-resolver.d.ts +28 -0
  6. package/dist/src/csharp/components/json-converter/json-converter-resolver.d.ts.map +1 -0
  7. package/dist/src/csharp/components/json-converter/json-converter-resolver.js +82 -0
  8. package/dist/src/csharp/components/json-converter/json-converter-resolver.js.map +1 -0
  9. package/dist/src/csharp/components/json-converter/json-converter-resolver.test.d.ts +2 -0
  10. package/dist/src/csharp/components/json-converter/json-converter-resolver.test.d.ts.map +1 -0
  11. package/dist/src/csharp/components/json-converter/json-converter-resolver.test.js +134 -0
  12. package/dist/src/csharp/components/json-converter/json-converter-resolver.test.js.map +1 -0
  13. package/dist/src/csharp/components/json-converter/json-converter.d.ts +28 -0
  14. package/dist/src/csharp/components/json-converter/json-converter.d.ts.map +1 -0
  15. package/dist/src/csharp/components/json-converter/json-converter.js +168 -0
  16. package/dist/src/csharp/components/json-converter/json-converter.js.map +1 -0
  17. package/dist/src/csharp/components/json-converter/json-converter.test.d.ts +2 -0
  18. package/dist/src/csharp/components/json-converter/json-converter.test.d.ts.map +1 -0
  19. package/dist/src/csharp/components/json-converter/json-converter.test.js +134 -0
  20. package/dist/src/csharp/components/json-converter/json-converter.test.js.map +1 -0
  21. package/dist/src/csharp/components/property/property.d.ts +4 -1
  22. package/dist/src/csharp/components/property/property.d.ts.map +1 -1
  23. package/dist/src/csharp/components/property/property.js +46 -11
  24. package/dist/src/csharp/components/property/property.js.map +1 -1
  25. package/dist/src/csharp/components/property/property.test.js +125 -8
  26. package/dist/src/csharp/components/property/property.test.js.map +1 -1
  27. package/package.json +8 -8
  28. package/package.json.bak +8 -8
  29. package/src/csharp/components/index.ts +2 -0
  30. package/src/csharp/components/json-converter/json-converter-resolver.test.tsx +120 -0
  31. package/src/csharp/components/json-converter/json-converter-resolver.tsx +117 -0
  32. package/src/csharp/components/json-converter/json-converter.test.tsx +141 -0
  33. package/src/csharp/components/json-converter/json-converter.tsx +146 -0
  34. package/src/csharp/components/property/property.test.tsx +111 -7
  35. package/src/csharp/components/property/property.tsx +47 -6
@@ -1,15 +1,26 @@
1
- import { type Children } from "@alloy-js/core";
1
+ import { code, REFKEYABLE, type Children } from "@alloy-js/core";
2
2
  import * as cs from "@alloy-js/csharp";
3
3
  import { Attribute } from "@alloy-js/csharp";
4
- import { getProperty, type ModelProperty, resolveEncodedName, type Type } from "@typespec/compiler";
4
+ import { Serialization } from "@alloy-js/csharp/global/System/Text/Json";
5
+ import {
6
+ getEncode,
7
+ getProperty,
8
+ resolveEncodedName,
9
+ type ModelProperty,
10
+ type Type,
11
+ } from "@typespec/compiler";
5
12
  import { useTsp } from "../../../core/index.js";
13
+ import { useJsonConverterResolver } from "../json-converter/json-converter-resolver.jsx";
6
14
  import { TypeExpression } from "../type-expression.jsx";
7
15
  import { getDocComments } from "../utils/doc-comments.jsx";
8
16
  import { getNullableUnionInnerType } from "../utils/nullable-util.js";
9
17
 
10
18
  export interface PropertyProps {
11
19
  type: ModelProperty;
12
- /** If set the property will add the json serialization attributes(using System.Text.Json). */
20
+ /** If set the property will add the json serialization attributes(using System.Text.Json.Serialization).
21
+ * - the JsonPropertyName attribute
22
+ * - the JsonConverter attribute if the property has encoding and a JsonConverterResolver context is available
23
+ * */
13
24
  jsonAttributes?: boolean;
14
25
  }
15
26
 
@@ -17,8 +28,8 @@ export interface PropertyProps {
17
28
  * Create a C# property declaration from a TypeSpec property type.
18
29
  */
19
30
  export function Property(props: PropertyProps): Children {
20
- const result = preprocessPropertyType(props.type);
21
31
  const { $ } = useTsp();
32
+ const result = preprocessPropertyType(props.type);
22
33
 
23
34
  let overrideType: "" | "override" | "new" = "";
24
35
  let isVirtual = false;
@@ -49,6 +60,22 @@ export function Property(props: PropertyProps): Children {
49
60
  });
50
61
  }
51
62
  }
63
+ const attributes = [];
64
+ if (props.jsonAttributes) {
65
+ attributes.push(<JsonNameAttribute type={props.type} />);
66
+ const encodeData = getEncode($.program, props.type);
67
+ if (encodeData) {
68
+ const JsonConverterResolver = useJsonConverterResolver();
69
+ if (JsonConverterResolver) {
70
+ const converter = JsonConverterResolver.resolveJsonConverter(result.type, encodeData);
71
+ if (converter) {
72
+ attributes.push(
73
+ <JsonConverterAttribute type={<cs.Reference refkey={converter.namekey} />} />,
74
+ );
75
+ }
76
+ }
77
+ }
78
+ }
52
79
 
53
80
  return (
54
81
  <cs.Property
@@ -61,7 +88,7 @@ export function Property(props: PropertyProps): Children {
61
88
  required={!props.type.optional}
62
89
  nullable={result.nullable}
63
90
  doc={getDocComments($, props.type)}
64
- attributes={props.jsonAttributes ? [<JsonNameAttribute type={props.type} />] : undefined}
91
+ attributes={attributes}
65
92
  get
66
93
  set
67
94
  />
@@ -75,7 +102,12 @@ export interface JsonNameAttributeProps {
75
102
  function JsonNameAttribute(props: JsonNameAttributeProps): Children {
76
103
  const { program } = useTsp();
77
104
  const jsonName = resolveEncodedName(program, props.type, "application/json");
78
- return <Attribute name="System.Text.Json.JsonPropertyName" args={[JSON.stringify(jsonName)]} />;
105
+ return (
106
+ <Attribute
107
+ name={Serialization.JsonPropertyNameAttribute[REFKEYABLE]()}
108
+ args={[JSON.stringify(jsonName)]}
109
+ />
110
+ );
79
111
  }
80
112
 
81
113
  function preprocessPropertyType(prop: ModelProperty): { type: Type; nullable: boolean } {
@@ -92,3 +124,12 @@ function preprocessPropertyType(prop: ModelProperty): { type: Type; nullable: bo
92
124
  return { type, nullable: prop.optional };
93
125
  }
94
126
  }
127
+
128
+ function JsonConverterAttribute(props: { type: Children }): Children {
129
+ return (
130
+ <Attribute
131
+ name={Serialization.JsonConverterAttribute[REFKEYABLE]()}
132
+ args={[code`typeof(${props.type})`]}
133
+ />
134
+ );
135
+ }