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

Support the @encode TypeSpec decorator with "unixTimestamp" #3492

Closed
joseharriaga opened this issue Jun 12, 2023 · 0 comments · Fixed by #3507
Closed

Support the @encode TypeSpec decorator with "unixTimestamp" #3492

joseharriaga opened this issue Jun 12, 2023 · 0 comments · Fixed by #3507
Assignees
Labels
customer-reported Issues that are reported by GitHub users external to the Azure organization. DPG GA-Required needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team

Comments

@joseharriaga
Copy link
Member

Consider the @encode decorator in TypeSpec used to specify that a utcDateTime property is encoded as a Unix timestamp:

model MyModel {
    @encode(DateTimeKnownEncoding.unixTimestamp, int32)
    myProperty: utcDateTime;    
}

The generated deserialization code for this property would look like this:

if (property.NameEquals("my_property"u8))
{
  myProperty = property.Value.GetDateTimeOffset("O");
  continue;
}

The problem is that Unix timestamps are represented as the number of seconds that have elapsed since the Unix epoch (January 1st, 1970, 00:00:00 UTC), but GetDateTimeOffset() expects a string. This causes the deserialization code above to fail.

More specifically, it seems like .NET's System.Text.Json does not offer first-class support for Unix timestamps; it defaults to ISO 8601-1:2019 instead (which is always a string; not a number). From the documentation, it seems that a custom converter is necessary:
🔗 https://learn.microsoft.com/en-us/dotnet/standard/datetime/system-text-json-support#using-unix-epoch-date-format

@m-nash m-nash added the DPG label Jun 12, 2023
@lirenhe lirenhe added the customer-reported Issues that are reported by GitHub users external to the Azure organization. label Jun 13, 2023
@ghost ghost added the needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team label Jun 13, 2023
@chunyu3 chunyu3 self-assigned this Jun 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customer-reported Issues that are reported by GitHub users external to the Azure organization. DPG GA-Required needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants