Skip to content

Commit f717281

Browse files
authored
Add optional jsonSchema field to Serde (#500)
1 parent 6295e8e commit f717281

File tree

3 files changed

+34
-67
lines changed

3 files changed

+34
-67
lines changed

packages/restate-sdk-core/src/serde_api.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,13 @@
1515

1616
export interface Serde<T> {
1717
readonly contentType: string;
18+
readonly jsonSchema?: object;
1819

1920
serialize(value: T): Uint8Array;
2021

2122
deserialize(data: Uint8Array): T;
2223
}
2324

24-
class JsonSerde<T> implements Serde<T | undefined> {
25-
contentType = "application/json";
26-
27-
serialize(value: T): Uint8Array {
28-
if (value === undefined) {
29-
return new Uint8Array(0);
30-
}
31-
return new TextEncoder().encode(JSON.stringify(value));
32-
}
33-
34-
deserialize(data: Uint8Array): T | undefined {
35-
if (data.length === 0) {
36-
return undefined;
37-
}
38-
return JSON.parse(new TextDecoder().decode(data)) as T;
39-
}
40-
}
41-
4225
class BinarySerde implements Serde<Uint8Array> {
4326
contentType = "application/octet-stream";
4427

@@ -69,7 +52,31 @@ class VoidSerde implements Serde<void> {
6952
}
7053

7154
export namespace serde {
72-
export const json: Serde<any> = new JsonSerde<any>();
55+
export class JsonSerde<T> implements Serde<T | undefined> {
56+
contentType = "application/json";
57+
58+
constructor(readonly jsonSchema?: object) {}
59+
60+
serialize(value: T): Uint8Array {
61+
if (value === undefined) {
62+
return new Uint8Array(0);
63+
}
64+
return new TextEncoder().encode(JSON.stringify(value));
65+
}
66+
67+
deserialize(data: Uint8Array): T | undefined {
68+
if (data.length === 0) {
69+
return undefined;
70+
}
71+
return JSON.parse(new TextDecoder().decode(data)) as T;
72+
}
73+
74+
schema<U>(schema: object): JsonSerde<U> {
75+
return new JsonSerde<U>(schema);
76+
}
77+
}
78+
79+
export const json: JsonSerde<any> = new JsonSerde<any>();
7380
export const binary: Serde<Uint8Array> = new BinarySerde();
7481
export const empty: Serde<void> = new VoidSerde();
7582
}

packages/restate-sdk/src/types/components.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ export class ServiceComponent implements Component {
6464
required: false,
6565
contentType:
6666
serviceHandler.handlerWrapper.accept ?? "application/json",
67-
jsonSchema: serviceHandler.handlerWrapper.inputSchema,
67+
jsonSchema: serviceHandler.handlerWrapper.inputSerde.jsonSchema,
6868
},
6969
output: {
7070
setContentTypeIfEmpty: false,
7171
contentType:
7272
serviceHandler.handlerWrapper.contentType ?? "application/json",
73-
jsonSchema: serviceHandler.handlerWrapper.outputSchema,
73+
jsonSchema: serviceHandler.handlerWrapper.outputSerde.jsonSchema,
7474
},
7575
documentation: serviceHandler.handlerWrapper.description,
7676
metadata: serviceHandler.handlerWrapper.metadata,
@@ -153,12 +153,12 @@ export class VirtualObjectComponent implements Component {
153153
input: {
154154
required: false,
155155
contentType: opts.accept ?? "application/json",
156-
jsonSchema: opts.inputSchema,
156+
jsonSchema: opts.inputSerde.jsonSchema,
157157
},
158158
output: {
159159
setContentTypeIfEmpty: false,
160160
contentType: opts.contentType ?? "application/json",
161-
jsonSchema: opts.outputSchema,
161+
jsonSchema: opts.outputSerde.jsonSchema,
162162
},
163163
ty:
164164
opts.kind === HandlerKind.EXCLUSIVE
@@ -239,12 +239,12 @@ export class WorkflowComponent implements Component {
239239
input: {
240240
required: false,
241241
contentType: handler.accept ?? "application/json",
242-
jsonSchema: handler.inputSchema,
242+
jsonSchema: handler.inputSerde.jsonSchema,
243243
},
244244
output: {
245245
setContentTypeIfEmpty: false,
246246
contentType: handler.contentType ?? "application/json",
247-
jsonSchema: handler.outputSchema,
247+
jsonSchema: handler.outputSerde.jsonSchema,
248248
},
249249
ty:
250250
handler.kind === HandlerKind.WORKFLOW

packages/restate-sdk/src/types/rpc.ts

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -284,18 +284,6 @@ export type ServiceHandlerOpts = {
284284
* Additional metadata for the handler.
285285
*/
286286
metadata?: Record<string, string>;
287-
288-
/**
289-
* An optional JSON schema definition for the input parameter.
290-
* Please note that this is not enforced, but only used as a documentation hint.
291-
*/
292-
inputSchema?: object;
293-
294-
/**
295-
* An optional JSON schema definition for the input parameter.
296-
* Please note that this is not enforced, but only used as a documentation hint.
297-
*/
298-
outputSchema?: object;
299287
};
300288

301289
export type ObjectHandlerOpts = {
@@ -337,18 +325,6 @@ export type ObjectHandlerOpts = {
337325
* Additional metadata for the handler.
338326
*/
339327
metadata?: Record<string, string>;
340-
341-
/**
342-
* An optional JSON schema definition for the input parameter.
343-
* Please note that this is not enforced, but only used as a documentation hint.
344-
*/
345-
inputSchema?: object;
346-
347-
/**
348-
* An optional JSON schema definition for the input parameter.
349-
* Please note that this is not enforced, but only used as a documentation hint.
350-
*/
351-
outputSchema?: object;
352328
};
353329

354330
export type WorkflowHandlerOpts = {
@@ -390,18 +366,6 @@ export type WorkflowHandlerOpts = {
390366
* Additional metadata for the handler.
391367
*/
392368
metadata?: Record<string, string>;
393-
394-
/**
395-
* An optional JSON schema definition for the input parameter.
396-
* Please note that this is not enforced, but only used as a documentation hint.
397-
*/
398-
inputSchema?: object;
399-
400-
/**
401-
* An optional JSON schema definition for the input parameter.
402-
* Please note that this is not enforced, but only used as a documentation hint.
403-
*/
404-
outputSchema?: object;
405369
};
406370

407371
const HANDLER_SYMBOL = Symbol("Handler");
@@ -429,9 +393,7 @@ export class HandlerWrapper {
429393
outputSerde,
430394
opts?.accept,
431395
opts?.description,
432-
opts?.metadata,
433-
opts?.inputSchema,
434-
opts?.outputSchema
396+
opts?.metadata
435397
);
436398
}
437399

@@ -450,9 +412,7 @@ export class HandlerWrapper {
450412
public readonly outputSerde: Serde<unknown>,
451413
accept?: string,
452414
public readonly description?: string,
453-
public readonly metadata?: Record<string, string>,
454-
public readonly inputSchema?: object,
455-
public readonly outputSchema?: object
415+
public readonly metadata?: Record<string, string>
456416
) {
457417
this.accept = accept ? accept : inputSerde.contentType;
458418
this.contentType = outputSerde.contentType;

0 commit comments

Comments
 (0)