Skip to content

[Bug]: @encode on a nullable property emits a schema that rejects null in OpenAPI 3.1 and 3.2 #12015

Closed

Description

Describe the bug

With openapi-versions: ["3.1.0"], @encode on a nullable property writes the encoded type and format next to the anyOf, not inside it. That top level type always rejects null. When the encoding changes the type, like unixTimestamp, seconds or @encode(string) on a number, it can't match the anyOf either, so every value fails. When the type stays a string, like rfc7231 on utcDateTime, only null fails. 3.2.0 does the same. 3.0.0 is fine, and so is the property without | null. Happy to send a PR.

Reproduction

model Order {
  @encode(DateTimeKnownEncoding.unixTimestamp, int64) shippedAt: utcDateTime | null;
}

3.1.0 output. Ajv (draft 2020) rejects both 1726000000 and null:

{ "anyOf": [{ "type": "string", "format": "date-time" }, { "type": "null" }], "type": "integer", "format": "int64" }

Expected, matching 3.0.0:

{ "anyOf": [{ "type": "integer", "format": "unixtime" }, { "type": "null" }] }

Nullable query parameters fail the same way. bytes | null with base64url only rejects null, but it gets format where plain bytes gets contentEncoding.

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 workingemitter:openapi3Issues for @typespec/openapi3 emitter

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions