Skip to content

Example generator emits invalid Go for RFC7231 datetime values (regression from #1992) #4924

Description

@tadelesh

Migrated from Azure/autorest.go#2026


Description

The example test generator produces uncompilable Go code for utcDateTime properties encoded as rfc7231. The generated value is wrapped in strconv.ParseInt (the Unix-timestamp path) with an unquoted, non-numeric date string.

Repro

Generate armdomainservices (or any spec with an rfc7231-encoded datetime, e.g. ConfigDiagnostics.lastExecuted).

Actual (broken) output

LastExecuted: to.Ptr(func() time.Time { t, _ := strconv.ParseInt(05 May 2021 12:00:23 GMT, 10, 64); return time.Unix(t, 0).UTC()}()),
  • 05 May 2021 12:00:23 GMT is unquoted -> does not compile
  • Wrong parse path entirely (Unix timestamp instead of RFC1123 layout)

Expected output

LastExecuted: to.Ptr(func() time.Time { t, _ := time.Parse(time.RFC1123, "05 May 2021 12:00:23 GMT"); return t }()),

Root cause

PR Azure/autorest.go#1992 ("Use RFC7231 for tsp instead of RFC1123", commit d9189b9ac) changed the TypeSpec adapter to emit the RFC7231 TimeFormat value instead of RFC1123. It updated the consumers in servers.ts, operations.ts, models.ts, and type.ts, but missed packages/codegen.go/src/core/example.ts.

getTimeValue's formatMap only handled RFC1123 and RFC3339, so RFC7231 fell through to the else branch, which assumes a Unix timestamp:

const formatMap: Record<string, string> = {
  PlainDate: helpers.plainDateFormat,
  PlainTime: helpers.plainTimeFormat,
  RFC1123: helpers.RFC1123Format,
  RFC3339: helpers.RFC3339Format,
  // RFC7231 missing
};

Fix

Add RFC7231: helpers.RFC1123Format to the map (mirrors the existing mapping in fake/servers.ts). Both share the Go time.RFC1123 layout.

Regression range

Introduced in Azure/autorest.go#1992 (emitter versions after 0.14.0).

Metadata

Metadata

Labels

bugSomething isn't workingemitter:goIssues for @azure-tools/typespec-go emitter

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions