Skip to content

Commit 584a790

Browse files
stephentoubCopilot
andauthored
Strip Ms suffix for duration properties (#1339)
* Strip Ms suffix for duration properties Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Rename shared session event codegen test Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent ac85df1 commit 584a790

7 files changed

Lines changed: 230 additions & 70 deletions

File tree

dotnet/src/Generated/Rpc.cs

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dotnet/src/Generated/SessionEvents.cs

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dotnet/test/Unit/SessionEventSerializationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public class SessionEventSerializationTests
8585
{
8686
ShutdownType = ShutdownType.Routine,
8787
TotalPremiumRequests = 1,
88-
TotalApiDurationMs = TimeSpan.FromMilliseconds(100),
88+
TotalApiDuration = TimeSpan.FromMilliseconds(100),
8989
SessionStartTime = 1773609948932,
9090
CodeChanges = new ShutdownCodeChanges
9191
{
Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { generateGoSessionEventsCode } from "../../scripts/codegen/go.ts";
66
import { generatePythonSessionEventsCode } from "../../scripts/codegen/python.ts";
77
import { generateSessionEventsCode as generateRustSessionEventsCode } from "../../scripts/codegen/rust.ts";
88

9-
describe("python session event codegen", () => {
9+
describe("session event codegen", () => {
1010
it("maps special schema formats to the expected Python types", () => {
1111
const schema: JSONSchema7 = {
1212
definitions: {
@@ -86,6 +86,91 @@ describe("python session event codegen", () => {
8686
expect(code).toContain("count: int");
8787
});
8888

89+
it("strips Ms suffixes from duration member names while preserving JSON names", () => {
90+
const schema: JSONSchema7 = {
91+
definitions: {
92+
SessionEvent: {
93+
anyOf: [
94+
{
95+
type: "object",
96+
required: ["type", "data"],
97+
properties: {
98+
type: { const: "session.synthetic" },
99+
data: {
100+
type: "object",
101+
required: ["durationMs", "integerDurationMs", "URLMs"],
102+
properties: {
103+
durationMs: { type: "number", format: "duration" },
104+
integerDurationMs: { type: "integer", format: "duration" },
105+
optionalDurationMs: {
106+
type: ["number", "null"],
107+
format: "duration",
108+
},
109+
nullableDurationMs: {
110+
anyOf: [
111+
{ type: "number", format: "duration" },
112+
{ type: "null" },
113+
],
114+
},
115+
URLMs: { type: "number", format: "duration" },
116+
},
117+
},
118+
},
119+
},
120+
],
121+
},
122+
},
123+
};
124+
125+
const pythonCode = generatePythonSessionEventsCode(schema);
126+
127+
expect(pythonCode).toContain("duration: timedelta");
128+
expect(pythonCode).toContain("integer_duration: timedelta");
129+
expect(pythonCode).toContain("optional_duration: timedelta | None = None");
130+
expect(pythonCode).toContain("nullable_duration: timedelta | None = None");
131+
expect(pythonCode).toContain("urlms: timedelta");
132+
expect(pythonCode).toContain('duration = from_timedelta(obj.get("durationMs"))');
133+
expect(pythonCode).toContain('result["durationMs"] = to_timedelta(self.duration)');
134+
expect(pythonCode).toContain(
135+
'integer_duration = from_timedelta(obj.get("integerDurationMs"))'
136+
);
137+
expect(pythonCode).toContain(
138+
'result["integerDurationMs"] = to_timedelta_int(self.integer_duration)'
139+
);
140+
expect(pythonCode).toContain(
141+
'optional_duration = from_union([from_none, from_timedelta], obj.get("optionalDurationMs"))'
142+
);
143+
expect(pythonCode).toContain(
144+
'result["optionalDurationMs"] = from_union([from_none, to_timedelta], self.optional_duration)'
145+
);
146+
expect(pythonCode).toContain(
147+
'nullable_duration = from_union([from_none, from_timedelta], obj.get("nullableDurationMs"))'
148+
);
149+
expect(pythonCode).toContain(
150+
'result["nullableDurationMs"] = from_union([from_none, to_timedelta], self.nullable_duration)'
151+
);
152+
expect(pythonCode).toContain('urlms = from_timedelta(obj.get("URLMs"))');
153+
expect(pythonCode).toContain('result["URLMs"] = to_timedelta(self.urlms)');
154+
155+
const csharpCode = generateCSharpSessionEventsCode(schema);
156+
157+
expect(csharpCode).toContain(
158+
'[JsonPropertyName("durationMs")]\n public required TimeSpan Duration { get; set; }'
159+
);
160+
expect(csharpCode).toContain(
161+
'[JsonPropertyName("integerDurationMs")]\n public required TimeSpan IntegerDuration { get; set; }'
162+
);
163+
expect(csharpCode).toContain(
164+
'[JsonPropertyName("optionalDurationMs")]\n public TimeSpan? OptionalDuration { get; set; }'
165+
);
166+
expect(csharpCode).toContain(
167+
'[JsonPropertyName("nullableDurationMs")]\n public TimeSpan? NullableDuration { get; set; }'
168+
);
169+
expect(csharpCode).toContain(
170+
'[JsonPropertyName("URLMs")]\n public required TimeSpan URLMs { get; set; }'
171+
);
172+
});
173+
89174
it("collapses redundant callable wrapper lambdas", () => {
90175
const schema: JSONSchema7 = {
91176
definitions: {

0 commit comments

Comments
 (0)