Skip to content

Commit 7a372ac

Browse files
committed
Fixed date-time and date handling
1 parent 56ca583 commit 7a372ac

20 files changed

Lines changed: 193 additions & 567 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenOperation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public class CodegenOperation {
3030
isResponseBinary = false, isResponseFile = false, hasReference = false,
3131
isRestfulIndex, isRestfulShow, isRestfulCreate, isRestfulUpdate, isRestfulDestroy,
3232
isRestful, isDeprecated, isCallbackRequest;
33-
public String path, operationId, returnType, httpMethod, returnBaseType,
34-
returnContainer, summary, unescapedNotes, notes, baseName, defaultResponse;
33+
public String path, operationId, returnType, returnFormat, httpMethod, returnBaseType,
34+
returnContainer, summary, unescapedNotes, notes, baseName, defaultResponse;
3535
public CodegenDiscriminator discriminator;
3636
public List<Map<String, String>> consumes, produces, prioritizedContentTypes;
3737
public List<CodegenServer> servers = new ArrayList<CodegenServer>();

modules/openapi-generator/src/main/resources/typescript/api/api.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ export class {{classname}}RequestFactory extends BaseAPIRequestFactory {
4040
// Query Params
4141
{{#queryParams}}
4242
if ({{paramName}} !== undefined) {
43-
requestContext.setQueryParam("{{baseName}}", ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}"));
43+
requestContext.setQueryParam("{{baseName}}", ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}", "{{dataFormat}}"));
4444
}
4545
{{/queryParams}}
4646

4747
// Header Params
4848
{{#headerParams}}
49-
requestContext.setHeaderParam("{{baseName}}", ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}"));
49+
requestContext.setHeaderParam("{{baseName}}", ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}", "{{dataFormat}}"));
5050
{{/headerParams}}
5151

5252
// Form Params
@@ -133,7 +133,7 @@ export class {{classname}}ResponseProcessor {
133133
if (isCodeInRange("{{code}}", response.httpStatusCode)) {
134134
{{#dataType}}
135135
const jsonBody = JSON.parse(response.body);
136-
const body: {{{dataType}}} = ObjectSerializer.deserialize(jsonBody, "{{{dataType}}}") as {{{dataType}}};
136+
const body: {{{dataType}}} = ObjectSerializer.deserialize(jsonBody, "{{{dataType}}}", "{{returnFormat}}") as {{{dataType}}};
137137
{{#isSuccessCode}}
138138
return body;
139139
{{/isSuccessCode}}
@@ -156,7 +156,7 @@ export class {{classname}}ResponseProcessor {
156156
if (response.httpStatusCode >= 200 && response.httpStatusCode <= 299) {
157157
{{#returnType}}
158158
const jsonBody = JSON.parse(response.body);
159-
const body: {{{returnType}}} = ObjectSerializer.deserialize(jsonBody, "{{{returnType}}}") as {{{returnType}}};
159+
const body: {{{returnType}}} = ObjectSerializer.deserialize(jsonBody, "{{{returnType}}}", "{{returnFormat}}") as {{{returnType}}};
160160
return body;
161161
{{/returnType}}
162162
{{^returnType}}

modules/openapi-generator/src/main/resources/typescript/generators/types/ObservableAPI.mustache

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ import { {{classname}}RequestFactory, {{classname}}ResponseProcessor} from "../a
1717
export class Observable{{classname}} {
1818
private requestFactory: {{classname}}RequestFactory;
1919
private responseProcessor: {{classname}}ResponseProcessor;
20-
21-
public constructor(private configuration: Configuration) {
22-
this.requestFactory = new {{classname}}RequestFactory(configuration);
23-
this.responseProcessor = new {{classname}}ResponseProcessor();
20+
private configuration: Configuration;
21+
22+
public constructor(configuration: Configuration, requestFactory?: {{classname}}RequestFactory, responseProcessor?: {{classname}}ResponseProcessor) {
23+
this.configuration = configuration;
24+
this.requestFactory = requestFactory || new {{classname}}RequestFactory(configuration);
25+
this.responseProcessor = responseProcessor || new {{classname}}ResponseProcessor();
2426
}
2527

2628
{{#operation}}

modules/openapi-generator/src/main/resources/typescript/generators/types/PromiseAPI.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import { Observable{{classname}} } from './ObservableAPI';
1616
import { {{classname}}RequestFactory, {{classname}}ResponseProcessor} from "../apis/{{classname}}";
1717
export class Promise{{classname}} {
1818
private api: Observable{{classname}}
19-
20-
public constructor(configuration: Configuration) {
21-
this.api = new Observable{{classname}}(configuration);
19+
20+
public constructor(configuration: Configuration, requestFactory?: {{classname}}RequestFactory, responseProcessor?: {{classname}}ResponseProcessor) {
21+
this.api = new Observable{{classname}}(configuration, requestFactory, responseProcessor);
2222
}
2323

2424
{{#operation}}

modules/openapi-generator/src/main/resources/typescript/model/ObjectSerializer.mustache

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export class ObjectSerializer {
8080
}
8181
}
8282
83-
public static serialize(data: any, type: string) {
83+
public static serialize(data: any, type: string, format: string) {
8484
if (data == undefined) {
8585
return data;
8686
} else if (primitives.indexOf(type.toLowerCase()) !== -1) {
@@ -91,11 +91,20 @@ export class ObjectSerializer {
9191
let transformedData: any[] = [];
9292
for (let index in data) {
9393
let date = data[index];
94-
transformedData.push(ObjectSerializer.serialize(date, subType));
94+
transformedData.push(ObjectSerializer.serialize(date, subType, format));
9595
}
9696
return transformedData;
9797
} else if (type === "Date") {
98-
return data.toISOString();
98+
if (format == "date") {
99+
let month = data.getMonth()+1
100+
month = month < 10 ? "0" + month.toString() : month.toString()
101+
let day = data.getDate();
102+
day = day < 10 ? "0" + day.toString() : day.toString();
103+
104+
return data.getFullYear() + "-" + month + "-" + day;
105+
} else {
106+
return data.toISOString();
107+
}
99108
} else {
100109
if (enumsMap.has(type)) {
101110
return data;
@@ -112,13 +121,13 @@ export class ObjectSerializer {
112121
let instance: {[index: string]: any} = {};
113122
for (let index in attributeTypes) {
114123
let attributeType = attributeTypes[index];
115-
instance[attributeType.baseName] = ObjectSerializer.serialize(data[attributeType.name], attributeType.type);
124+
instance[attributeType.baseName] = ObjectSerializer.serialize(data[attributeType.name], attributeType.type, attributeType.format);
116125
}
117126
return instance;
118127
}
119128
}
120129
121-
public static deserialize(data: any, type: string) {
130+
public static deserialize(data: any, type: string, format: string) {
122131
// polymorphism may change the actual type.
123132
type = ObjectSerializer.findCorrectType(data, type);
124133
if (data == undefined) {
@@ -131,7 +140,7 @@ export class ObjectSerializer {
131140
let transformedData: any[] = [];
132141
for (let index in data) {
133142
let date = data[index];
134-
transformedData.push(ObjectSerializer.deserialize(date, subType));
143+
transformedData.push(ObjectSerializer.deserialize(date, subType, format));
135144
}
136145
return transformedData;
137146
} else if (type === "Date") {
@@ -148,7 +157,7 @@ export class ObjectSerializer {
148157
let attributeTypes = typeMap[type].getAttributeTypeMap();
149158
for (let index in attributeTypes) {
150159
let attributeType = attributeTypes[index];
151-
instance[attributeType.name] = ObjectSerializer.deserialize(data[attributeType.baseName], attributeType.type);
160+
instance[attributeType.name] = ObjectSerializer.deserialize(data[attributeType.baseName], attributeType.type, attributeType.format);
152161
}
153162
return instance;
154163
}

modules/openapi-generator/src/main/resources/typescript/model/model.mustache

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ export class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{
2828
{{/discriminator}}
2929

3030
{{^isArrayModel}}
31-
static readonly attributeTypeMap: Array<{name: string, baseName: string, type: string}> = [
31+
static readonly attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string}> = [
3232
{{#vars}}
3333
{
3434
"name": "{{name}}",
3535
"baseName": "{{baseName}}",
36-
"type": "{{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}"
36+
"type": "{{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}",
37+
"format": "{{dataFormat}}"
3738
}{{#hasMore}},
3839
{{/hasMore}}
3940
{{/vars}}

0 commit comments

Comments
 (0)