Skip to content

Commit 26dc626

Browse files
committed
feat: map string formats (date-time, date, time) to Date in TypeScript generator
1 parent 05b1d59 commit 26dc626

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"dependencies": {
3838
"@apidevtools/json-schema-ref-parser": "^11.1.0",
3939
"@apidevtools/swagger-parser": "^10.1.0",
40+
"@asyncapi/modelina": "^5.10.1",
4041
"@asyncapi/multi-parser": "^2.2.0",
4142
"@asyncapi/parser": "^3.4.0",
4243
"alterschema": "^1.1.2",

src/generators/typescript/TypeScriptConstrainer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ export const TypeScriptDefaultTypeMapping: TypeScriptTypeMapping = {
3131
return applyNullable(constrainedModel, 'number');
3232
},
3333
String({ constrainedModel }): string {
34+
const format = constrainedModel?.options?.format;
35+
if(format ==="date-time" || format ==="date"|| format ==='time'){
36+
return applyNullable(constrainedModel, 'Date');
37+
}
3438
return applyNullable(constrainedModel, 'string');
3539
},
3640
Boolean({ constrainedModel }): string {

test/generators/typescript/__snapshots__/TypeScriptGenerator.spec.ts.snap

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ Array [
9797
private _specversion: string;
9898
private _reservedType: DogType.DOG = DogType.DOG;
9999
private _dataschema?: string;
100-
private _time?: string;
100+
private _time?: Date;
101101
private _additionalProperties?: Map<string, any>;
102102
103103
constructor(input: {
104104
id: string,
105105
source: string,
106106
specversion: string,
107107
dataschema?: string,
108-
time?: string,
108+
time?: Date,
109109
additionalProperties?: Map<string, any>,
110110
}) {
111111
this._id = input.id;
@@ -130,8 +130,8 @@ Array [
130130
get dataschema(): string | undefined { return this._dataschema; }
131131
set dataschema(dataschema: string | undefined) { this._dataschema = dataschema; }
132132
133-
get time(): string | undefined { return this._time; }
134-
set time(time: string | undefined) { this._time = time; }
133+
get time(): Date | undefined { return this._time; }
134+
set time(time: Date | undefined) { this._time = time; }
135135
136136
get additionalProperties(): Map<string, any> | undefined { return this._additionalProperties; }
137137
set additionalProperties(additionalProperties: Map<string, any> | undefined) { this._additionalProperties = additionalProperties; }
@@ -145,15 +145,15 @@ Array [
145145
private _specversion: string;
146146
private _reservedType: CatType.CAT = CatType.CAT;
147147
private _dataschema?: string;
148-
private _time?: string;
148+
private _time?: Date;
149149
private _additionalProperties?: Map<string, any>;
150150
151151
constructor(input: {
152152
id: string,
153153
source: string,
154154
specversion: string,
155155
dataschema?: string,
156-
time?: string,
156+
time?: Date,
157157
additionalProperties?: Map<string, any>,
158158
}) {
159159
this._id = input.id;
@@ -178,8 +178,8 @@ Array [
178178
get dataschema(): string | undefined { return this._dataschema; }
179179
set dataschema(dataschema: string | undefined) { this._dataschema = dataschema; }
180180
181-
get time(): string | undefined { return this._time; }
182-
set time(time: string | undefined) { this._time = time; }
181+
get time(): Date | undefined { return this._time; }
182+
set time(time: Date | undefined) { this._time = time; }
183183
184184
get additionalProperties(): Map<string, any> | undefined { return this._additionalProperties; }
185185
set additionalProperties(additionalProperties: Map<string, any> | undefined) { this._additionalProperties = additionalProperties; }
@@ -379,13 +379,13 @@ Array [
379379
"class ReservedEvent {
380380
private _id: string;
381381
private _action?: Action;
382-
private _eventTime?: string;
382+
private _eventTime?: Date;
383383
private _additionalProperties?: Map<string, any>;
384384
385385
constructor(input: {
386386
id: string,
387387
action?: Action,
388-
eventTime?: string,
388+
eventTime?: Date,
389389
additionalProperties?: Map<string, any>,
390390
}) {
391391
this._id = input.id;
@@ -400,8 +400,8 @@ Array [
400400
get action(): Action | undefined { return this._action; }
401401
set action(action: Action | undefined) { this._action = action; }
402402
403-
get eventTime(): string | undefined { return this._eventTime; }
404-
set eventTime(eventTime: string | undefined) { this._eventTime = eventTime; }
403+
get eventTime(): Date | undefined { return this._eventTime; }
404+
set eventTime(eventTime: Date | undefined) { this._eventTime = eventTime; }
405405
406406
get additionalProperties(): Map<string, any> | undefined { return this._additionalProperties; }
407407
set additionalProperties(additionalProperties: Map<string, any> | undefined) { this._additionalProperties = additionalProperties; }

0 commit comments

Comments
 (0)