Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve issue 4296 #4599

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/TypeSpec.Extension/Emitter.Csharp/src/lib/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ export function getInputType(
} as InputPrimitiveType;
}
} else if (type.kind === "Union") {
return getInputTypeForUnion(type);
return getInputTypeForUnion(type, formattedType.encode);
} else if (type.kind === "UnionVariant") {
return getInputType(
context,
Expand Down Expand Up @@ -808,7 +808,10 @@ export function getInputType(
}
}

function getInputTypeForUnion(union: Union): InputUnionType | InputType {
function getInputTypeForUnion(
union: Union,
encodeData: EncodeData | undefined = undefined
): InputUnionType | InputType {
var clientType = getClientType(context, union);
if (clientType.kind === "enum" && clientType.isFixed === false) {
return fromSdkEnumType(clientType, context, enums);
Expand All @@ -819,9 +822,12 @@ export function getInputType(

let hasNullType = false;
for (const variant of variants) {
var formattedType = getFormattedType(program, variant.type);
// propagate the encode data from parent.
if (!formattedType.encode) formattedType.encode = encodeData;
const inputType = getInputType(
context,
getFormattedType(program, variant.type),
formattedType,
models,
enums
);
Expand Down
30 changes: 30 additions & 0 deletions src/TypeSpec.Extension/Emitter.Csharp/test/Unit/encode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,34 @@ describe("Test encode duration", () => {
)
);
});
it("encode iso8601 on duration|null union model property", async () => {
const program = await typeSpecCompile(
`
@doc("This is a model.")
model ISO8601DurationProperty {
@encode(DurationKnownEncoding.ISO8601)
value: duration | null;
}
`,
runner
);
const context = createEmitterContext(program);
const sdkContext = createNetSdkContext(context);
const [services] = getAllHttpServices(program);
const modelMap = new Map<string, InputModelType>();
const enumMap = new Map<string, InputEnumType>();
navigateModels(sdkContext, services[0].namespace, modelMap, enumMap);
const durationProperty = modelMap.get("ISO8601DurationProperty");
assert(durationProperty !== undefined);
assert(
isEqual(
{
Kind: InputTypeKind.Primitive,
Name: InputPrimitiveTypeKind.DurationISO8601,
IsNullable: true
} as InputPrimitiveType,
durationProperty.Properties[0].Type
)
);
});
});
10 changes: 10 additions & 0 deletions test/TestProjects/FirstTest-TypeSpec/FirstTest-TypeSpec.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -548,3 +548,13 @@ model AzureLocationModel
@put
@convenientAPI(true)
op azureLocationOp(@query location: azureLocation, @header regenLocation: azureLocation, @body body?: AzureLocationModel): void;

model EncodedUnionProperty{
@encode("unixTimestamp", int32)
started_at: utcDateTime | null;
}
@route("/encodedUnionProperty")
@doc("test nullableTime.")
@put
@convenientAPI(true)
op EncodedUnionPropertyOp(@body body: EncodedUnionProperty):EncodedUnionProperty;
Original file line number Diff line number Diff line change
Expand Up @@ -2221,6 +2221,106 @@ using RequestContent content = RequestContent.Create(new
Response response = client.AzureLocationOp(default, default, content);

Console.WriteLine(response.Status);
]]></code></example>
</member>
<member name="EncodedUnionPropertyOpAsync(EncodedUnionProperty,CancellationToken)">
<example>
This sample shows how to call EncodedUnionPropertyOpAsync.
<code><![CDATA[
Uri endpoint = new Uri("<https://my-service.azure.com>");
FirstTestTypeSpecClient client = new FirstTestTypeSpecClient(endpoint);

EncodedUnionProperty body = new EncodedUnionProperty(DateTimeOffset.FromUnixTimeSeconds(1652209051L));
Response<EncodedUnionProperty> response = await client.EncodedUnionPropertyOpAsync(body);
]]></code>
This sample shows how to call EncodedUnionPropertyOpAsync with all parameters.
<code><![CDATA[
Uri endpoint = new Uri("<https://my-service.azure.com>");
FirstTestTypeSpecClient client = new FirstTestTypeSpecClient(endpoint);

EncodedUnionProperty body = new EncodedUnionProperty(DateTimeOffset.FromUnixTimeSeconds(1652209051L));
Response<EncodedUnionProperty> response = await client.EncodedUnionPropertyOpAsync(body);
]]></code></example>
</member>
<member name="EncodedUnionPropertyOp(EncodedUnionProperty,CancellationToken)">
<example>
This sample shows how to call EncodedUnionPropertyOp.
<code><![CDATA[
Uri endpoint = new Uri("<https://my-service.azure.com>");
FirstTestTypeSpecClient client = new FirstTestTypeSpecClient(endpoint);

EncodedUnionProperty body = new EncodedUnionProperty(DateTimeOffset.FromUnixTimeSeconds(1652209051L));
Response<EncodedUnionProperty> response = client.EncodedUnionPropertyOp(body);
]]></code>
This sample shows how to call EncodedUnionPropertyOp with all parameters.
<code><![CDATA[
Uri endpoint = new Uri("<https://my-service.azure.com>");
FirstTestTypeSpecClient client = new FirstTestTypeSpecClient(endpoint);

EncodedUnionProperty body = new EncodedUnionProperty(DateTimeOffset.FromUnixTimeSeconds(1652209051L));
Response<EncodedUnionProperty> response = client.EncodedUnionPropertyOp(body);
]]></code></example>
</member>
<member name="EncodedUnionPropertyOpAsync(RequestContent,RequestContext)">
<example>
This sample shows how to call EncodedUnionPropertyOpAsync and parse the result.
<code><![CDATA[
Uri endpoint = new Uri("<https://my-service.azure.com>");
FirstTestTypeSpecClient client = new FirstTestTypeSpecClient(endpoint);

using RequestContent content = RequestContent.Create(new
{
started_at = 1652209051,
});
Response response = await client.EncodedUnionPropertyOpAsync(content);

JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement;
Console.WriteLine(result.GetProperty("started_at").ToString());
]]></code>
This sample shows how to call EncodedUnionPropertyOpAsync with all request content and parse the result.
<code><![CDATA[
Uri endpoint = new Uri("<https://my-service.azure.com>");
FirstTestTypeSpecClient client = new FirstTestTypeSpecClient(endpoint);

using RequestContent content = RequestContent.Create(new
{
started_at = 1652209051,
});
Response response = await client.EncodedUnionPropertyOpAsync(content);

JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement;
Console.WriteLine(result.GetProperty("started_at").ToString());
]]></code></example>
</member>
<member name="EncodedUnionPropertyOp(RequestContent,RequestContext)">
<example>
This sample shows how to call EncodedUnionPropertyOp and parse the result.
<code><![CDATA[
Uri endpoint = new Uri("<https://my-service.azure.com>");
FirstTestTypeSpecClient client = new FirstTestTypeSpecClient(endpoint);

using RequestContent content = RequestContent.Create(new
{
started_at = 1652209051,
});
Response response = client.EncodedUnionPropertyOp(content);

JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement;
Console.WriteLine(result.GetProperty("started_at").ToString());
]]></code>
This sample shows how to call EncodedUnionPropertyOp with all request content and parse the result.
<code><![CDATA[
Uri endpoint = new Uri("<https://my-service.azure.com>");
FirstTestTypeSpecClient client = new FirstTestTypeSpecClient(endpoint);

using RequestContent content = RequestContent.Create(new
{
started_at = 1652209051,
});
Response response = client.EncodedUnionPropertyOp(content);

JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement;
Console.WriteLine(result.GetProperty("started_at").ToString());
]]></code></example>
</member>
</members>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.