Skip to content

[Bug]: @encode(string) int64 default becomes null, and numeric examples ignore the encoding #12059

Description

Describe the bug

With @encode(string) id: int64 = 9007199254740993, the OpenAPI output has "default": null, so the value is lost. Other defaults and @example values on @encode(string) int64 and decimal properties come out as JSON numbers, which don't match the type: string next to them.

serializeValueAsJson in packages/compiler/src/lib/examples.ts already resolves the property's @encode, but the NumericValue case returns asNumber() without checking it, and asNumber() gives null when a JS number can't hold the value exactly. Dates and durations already use the encoding in that function (#11731 fixed the milliseconds case). Happy to send a PR.

Reproduction

@example(#{ id: 42, balance: 1050.25 })
model Account {
  @encode(string) id: int64 = 9007199254740993;
  @encode(string) balance: decimal = 0;
}

OpenAPI 3.0.0 output on current main. 3.1.0 and 3.2.0 give the same defaults.

"properties": {
  "id": { "type": "string", "format": "int64", "default": null },
  "balance": { "type": "string", "format": "decimal", "default": 0 }
},
"example": { "id": 42, "balance": 1050.25 }

Expected "9007199254740993" and "0" for the defaults, and { "id": "42", "balance": "1050.25" } for the example.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcompiler:coreIssues for @typespec/compiler

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions