Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/openapi3"
---

Import OpenAPI schemas with anyOf/oneOf containing unixtime format correctly emits `@encode(DateTimeKnownEncoding.unixTimestamp, integer)` decorator for nullable utcDateTime properties
31 changes: 29 additions & 2 deletions packages/openapi3/src/cli/actions/convert/utils/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,35 @@ export function getDecoratorsForSchema(
: schema.type;

// Handle unixtime format with @encode decorator
if (schema.format === "unixtime") {
decorators.push(...getUnixtimeSchemaDecorators(effectiveType));
// Check both direct format and format from anyOf/oneOf members
let formatToUse = schema.format;
let typeForFormat = effectiveType;

// If format is not directly on the schema, check anyOf/oneOf members for unixtime format
if (!formatToUse) {
const unionMembers = schema.anyOf || schema.oneOf;
if (unionMembers) {
for (const member of unionMembers) {
if ("$ref" in member) continue;
// Check if this is a non-null member with unixtime format
if (member.format === "unixtime" && member.type !== "null") {
formatToUse = member.format;
// Extract effective type from member (handle type arrays)
const memberType = Array.isArray(member.type)
? member.type.find((t) => t !== "null")
: member.type;
// Only use if we found a valid type
if (memberType) {
typeForFormat = memberType;
}
break;
}
}
}
}

if (formatToUse === "unixtime") {
decorators.push(...getUnixtimeSchemaDecorators(typeForFormat));
}

switch (effectiveType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,91 @@ describe("unixtime format conversion", () => {
"Expected 'created?: string' but got: " + tsp,
);
});

it("should convert anyOf with integer unixtime and null to utcDateTime | null with @encode decorator", async () => {
const tsp = await convertOpenAPI3Document({
openapi: "3.1.0",
info: {
title: "Test Service",
version: "0.0.0",
},
paths: {},
components: {
schemas: {
Foo: {
type: "object",
properties: {
finished_at: {
anyOf: [
{
type: "integer",
format: "unixtime",
},
{
type: "null",
},
],
},
},
},
},
},
});

// Should contain "@encode(DateTimeKnownEncoding.unixTimestamp, integer)" and "finished_at?: utcDateTime | null"
strictEqual(
tsp.includes("@encode(DateTimeKnownEncoding.unixTimestamp, integer)"),
true,
"Expected '@encode(DateTimeKnownEncoding.unixTimestamp, integer)' but got: " + tsp,
);
strictEqual(
tsp.includes("finished_at?: utcDateTime | null"),
true,
"Expected 'finished_at?: utcDateTime | null' but got: " + tsp,
);
});

it("should convert oneOf with integer unixtime and null to utcDateTime | null with @encode decorator", async () => {
const tsp = await convertOpenAPI3Document({
openapi: "3.1.0",
info: {
title: "Test Service",
version: "0.0.0",
},
paths: {},
components: {
schemas: {
Foo: {
type: "object",
properties: {
started_at: {
oneOf: [
{
type: "integer",
format: "unixtime",
},
{
type: "null",
},
],
},
},
},
},
},
});

// Should contain "@encode(DateTimeKnownEncoding.unixTimestamp, integer)" and "@oneOf" and "started_at?: utcDateTime | null"
strictEqual(
tsp.includes("@encode(DateTimeKnownEncoding.unixTimestamp, integer)"),
true,
"Expected '@encode(DateTimeKnownEncoding.unixTimestamp, integer)' but got: " + tsp,
);
strictEqual(tsp.includes("@oneOf"), true, "Expected '@oneOf' but got: " + tsp);
strictEqual(
tsp.includes("started_at?: utcDateTime | null"),
true,
"Expected 'started_at?: utcDateTime | null' but got: " + tsp,
);
});
});
Loading