Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BP-603: Use custom nswag templates to fix dateonly parsing and formatting in generated API. #1

Merged
merged 3 commits into from
Dec 6, 2023
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
2 changes: 1 addition & 1 deletion enigmatry-entry-blueprint-app/nswag.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"queryNullValue": "",
"inlineNamedDictionaries": false,
"inlineNamedAny": false,
"templateDirectory": null,
"templateDirectory": "src/app/api/custom_templates_v13",
"typeNameGeneratorType": null,
"propertyNameGeneratorType": null,
"enumNameGeneratorType": null,
Expand Down
14 changes: 10 additions & 4 deletions enigmatry-entry-blueprint-app/src/app/api/api-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ export class ProductsClient implements IProductsClient {
if (code !== undefined && code !== null)
url_ += "Code=" + encodeURIComponent("" + code) + "&";
if (expiresBefore !== undefined && expiresBefore !== null)
url_ += "ExpiresBefore=" + encodeURIComponent(expiresBefore ? "" + expiresBefore.toISOString() : "") + "&";
url_ += "ExpiresBefore=" + encodeURIComponent(expiresBefore ? "" + formatDate(expiresBefore) : "") + "&";
if (pageNumber === null)
throw new Error("The parameter 'pageNumber' cannot be null.");
else if (pageNumber !== undefined)
Expand Down Expand Up @@ -1173,7 +1173,7 @@ export class GetProductsResponseItem implements IGetProductsResponseItem {
this.contactEmail = _data["contactEmail"];
this.contactPhone = _data["contactPhone"];
this.infoLink = _data["infoLink"];
this.expiresOn = _data["expiresOn"] ? new Date(_data["expiresOn"].toString()) : <any>undefined;
this.expiresOn = _data["expiresOn"] ? parseDateOnly(_data["expiresOn"].toString()) : <any>undefined;
this.freeShipping = _data["freeShipping"];
this.hasDiscount = _data["hasDiscount"];
this.discount = _data["discount"];
Expand Down Expand Up @@ -1268,7 +1268,7 @@ export class GetProductDetailsResponse implements IGetProductDetailsResponse {
this.contactPhone = _data["contactPhone"];
this.infoLink = _data["infoLink"];
this.additionalInfo = _data["additionalInfo"];
this.expiresOn = _data["expiresOn"] ? new Date(_data["expiresOn"].toString()) : <any>undefined;
this.expiresOn = _data["expiresOn"] ? parseDateOnly(_data["expiresOn"].toString()) : <any>undefined;
this.hasDiscount = _data["hasDiscount"];
this.discount = _data["discount"];
this.freeShipping = _data["freeShipping"];
Expand Down Expand Up @@ -1580,7 +1580,7 @@ export class ProductCreateOrUpdateCommand implements IProductCreateOrUpdateComma
this.contactEmail = _data["contactEmail"];
this.contactPhone = _data["contactPhone"];
this.infoLink = _data["infoLink"];
this.expiresOn = _data["expiresOn"] ? new Date(_data["expiresOn"].toString()) : <any>undefined;
this.expiresOn = _data["expiresOn"] ? parseDateOnly(_data["expiresOn"].toString()) : <any>undefined;
this.freeShipping = _data["freeShipping"];
this.hasDiscount = _data["hasDiscount"];
this.discount = _data["discount"];
Expand Down Expand Up @@ -1702,6 +1702,12 @@ function formatDate(d: Date) {
(d.getDate() < 10 ? ('0' + d.getDate()) : d.getDate());
}

function parseDateOnly(s: string) {
const date = new Date(s);
return new Date(date.getTime() +
date.getTimezoneOffset() * 60000);
}

export class ApiException extends Error {
message: string;
status: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
let url_ = this.baseUrl + "/{{ operation.Path }}{% if operation.HasQueryParameters %}?{% endif %}";
{% for parameter in operation.PathParameters -%}
{% if parameter.IsRequired -%}
if ({{ parameter.VariableName }} === undefined || {{ parameter.VariableName }} === null)
throw new Error("The parameter '{{ parameter.VariableName }}' must be defined.");
{% else -%}
if ({{ parameter.VariableName }} !== null && {{ parameter.VariableName }} !== undefined)
{% endif -%}
{% if parameter.IsDateArray -%}
url_ = url_.replace("{{ "{" }}{{ parameter.Name }}}", encodeURIComponent({{ parameter.VariableName }}.map(s_ => s_ ? formatDate(s_) : "null").join()));
{% elsif parameter.IsDateTimeArray -%}
url_ = url_.replace("{{ "{" }}{{ parameter.Name }}}", encodeURIComponent({{ parameter.VariableName }}.map(s_ => s_ ? s_.{{ parameter.GetDateTimeToString }} : "null").join()));
{% elsif parameter.IsDate -%}
url_ = url_.replace("{{ "{" }}{{ parameter.Name }}}", encodeURIComponent({{ parameter.VariableName }} ? "" + formatDate({{ parameter.VariableName }}) : "null"));
{% elsif parameter.IsDateTime -%}
url_ = url_.replace("{{ "{" }}{{ parameter.Name }}}", encodeURIComponent({{ parameter.VariableName }} ? "" + {{ parameter.VariableName }}.{{ parameter.GetDateTimeToString }} : "null"));
{% elsif parameter.IsArray -%}
url_ = url_.replace("{{ "{" }}{{ parameter.Name }}}", encodeURIComponent({{ parameter.VariableName }}.join()));
{% else -%}
url_ = url_.replace("{{ "{" }}{{ parameter.Name }}}", encodeURIComponent("" + {{ parameter.VariableName }}));
{% endif -%}
{% if parameter.IsOptional -%}
else
url_ = url_.replace("/{{ "{" }}{{ parameter.Name }}}", "");
{% endif -%}
{% endfor -%}
{% for parameter in operation.QueryParameters -%}
{% if parameter.IsRequired -%}
{% if parameter.IsNullable -%}
if ({{ parameter.VariableName }} === undefined)
throw new Error("The parameter '{{ parameter.VariableName }}' must be defined.");
else if({{ parameter.VariableName }} !== null)
{% else -%}
if ({{ parameter.VariableName }} === undefined || {{ parameter.VariableName }} === null)
throw new Error("The parameter '{{ parameter.VariableName }}' must be defined and cannot be null.");
else
{% endif -%}
{% else -%}
{% if parameter.IsNullable -%}
if ({{ parameter.VariableName }} !== undefined && {{ parameter.VariableName }} !== null)
{% else -%}
if ({{ parameter.VariableName }} === null)
throw new Error("The parameter '{{ parameter.VariableName }}' cannot be null.");
else if ({{ parameter.VariableName }} !== undefined)
{% endif -%}
{% endif -%}
{% if parameter.IsDateArray -%}
{{ parameter.VariableName }} && {{ parameter.VariableName }}.forEach(item_ => { url_ += "{{ parameter.Name }}=" + encodeURIComponent(item_ ? "" + formatDate(item_) : "null") + "&"; });
{% elsif parameter.IsDateTimeArray -%}
{{ parameter.VariableName }} && {{ parameter.VariableName }}.forEach(item_ => { url_ += "{{ parameter.Name }}=" + encodeURIComponent(item_ ? "" + item_.{{ parameter.GetDateTimeToString }} : "null") + "&"; });
{% elsif parameter.IsObjectArray -%}
{{ parameter.VariableName }} && {{ parameter.VariableName }}.forEach((item, index) => {
for (const attr in item)
if (item.hasOwnProperty(attr)) {
url_ += "{{ parameter.Name }}[" + index + "]." + attr + "=" + encodeURIComponent("" + (item as any)[attr]) + "&";
}
});
{% elsif parameter.IsDate -%}
url_ += "{{ parameter.Name }}=" + encodeURIComponent({{ parameter.VariableName }} ? "" + formatDate({{ parameter.VariableName }}) : "{{ QueryNullValue }}") + "&";
{% elsif parameter.IsDateTime -%}
url_ += "{{ parameter.Name }}=" + encodeURIComponent({{ parameter.VariableName }} ? "" + {{ parameter.VariableName }}.{{ parameter.GetDateTimeToString }} : "{{ QueryNullValue }}") + "&";
{% elsif parameter.IsArray -%}
{{ parameter.VariableName }} && {{ parameter.VariableName }}.forEach(item => { url_ += "{{ parameter.Name }}=" + encodeURIComponent("" + item) + "&"; });
{% else -%}
url_ += "{{ parameter.Name }}=" + encodeURIComponent("" + {{ parameter.VariableName }}) + "&";
{% endif -%}
{% endfor -%}
url_ = url_.replace(/[?&]$/, "");
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{% if IsNewableObject -%}
{% if CheckNewableObject -%}
{{ Variable }} = {{ Value }} ? {{ Type }}.fromJS({{ Value }}{% if HandleReferences -%}, _mappings{% endif %}) : {% if HasDefaultValue %}{{ DefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
{% else -%}
{{ Variable }} = {{ Type }}.fromJS({{ Value }}{% if HandleReferences -%}, _mappings{% endif %});
{% endif -%}
{% elsif IsArray -%}
if (Array.isArray({{ Value }})) {
{{ Variable }} = [] as any;
for (let item of {{ Value }})
{% if IsArrayItemNewableObject -%}
{{ Variable }}{% if RequiresStrictPropertyInitialization %}!{% endif %}.push({{ ArrayItemType }}.fromJS(item{% if HandleReferences %}, _mappings{% endif %}));
{% else -%}
{% if IsArrayItemDate -%}
{{ Variable }}{% if RequiresStrictPropertyInitialization %}!{% endif %}.push({{ "parseDateOnly" }}(item));
{% elsif IsArrayItemDateTime -%}
{{ Variable }}{% if RequiresStrictPropertyInitialization %}!{% endif %}.push({{ StringToDateCode }}(item));
{% else -%}
{{ Variable }}{% if RequiresStrictPropertyInitialization %}!{% endif %}.push(item);
{% endif -%}
{% endif -%}
}
{% if NullValue != "undefined" %}else {
{{ Variable }} = <any>{{ NullValue }};
}
{% endif -%}
{% elsif IsDictionary -%}
if ({{ Value }}) {
{{ Variable }} = {} as any;
for (let key in {{ Value }}) {
if ({{ Value }}.hasOwnProperty(key))
{% if IsDictionaryValueNewableObject -%}
(<any>{{ Variable }}){% if RequiresStrictPropertyInitialization %}!{% endif %}[key] = {{ Value }}[key] ? {{ DictionaryValueType }}.fromJS({{ Value }}[key]{% if HandleReferences %}, _mappings{% endif %}) : {% if HasDictionaryValueDefaultValue %}{{ DictionaryValueDefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
{% elsif IsDictionaryValueNewableArray -%}
(<any>{{ Variable }}){% if RequiresStrictPropertyInitialization %}!{% endif %}[key] = {{ Value }}[key] ? {{ Value }}[key].map((i: any) => {{ DictionaryValueArrayItemType }}.fromJS(i{% if HandleReferences %}, _mappings{% endif %})) : {% if HasDictionaryValueDefaultValue %}{{ DictionaryValueDefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
{% elsif IsDictionaryValueDate -%}
(<any>{{ Variable }}){% if RequiresStrictPropertyInitialization %}!{% endif %}[key] = {{ Value }}[key] ? {{ "parseDateOnly" }}({{ Value }}[key].toString()) : {% if HasDictionaryValueDefaultValue %}{{ DictionaryValueDefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
{% elsif IsDictionaryValueDateTime -%}
(<any>{{ Variable }}){% if RequiresStrictPropertyInitialization %}!{% endif %}[key] = {{ Value }}[key] ? {{ StringToDateCode }}({{ Value }}[key].toString()) : {% if HasDictionaryValueDefaultValue %}{{ DictionaryValueDefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
{% else -%}
{% if HasDictionaryValueDefaultValue or NullValue != "undefined" -%}
(<any>{{ Variable }}){% if RequiresStrictPropertyInitialization %}!{% endif %}[key] = {{ Value }}[key] !== undefined ? {{ Value }}[key] : {% if HasDictionaryValueDefaultValue %}{{ DictionaryValueDefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
{% else -%}
(<any>{{ Variable }}){% if RequiresStrictPropertyInitialization %}!{% endif %}[key] = {{ Value }}[key];
{% endif -%}
{% endif -%}
}
}
{% if NullValue != "undefined" %}else {
{{ Variable }} = <any>{{ NullValue }};
}
{% endif -%}
{% else -%}
{% if IsDate -%}
{{ Variable }} = {{ Value }} ? {{ "parseDateOnly" }}({{ Value }}.toString()) : {% if HasDefaultValue %}{{ "parseDateOnly" }}({{ DefaultValue }}){% else %}<any>{{ NullValue }}{% endif %};
{% elsif IsDateTime -%}
{{ Variable }} = {{ Value }} ? {{ StringToDateCode }}({{ Value }}.toString()) : {% if HasDefaultValue %}{{ StringToDateCode }}({{ DefaultValue }}){% else %}<any>{{ NullValue }}{% endif %};
{% else -%}
{% if HasDefaultValue or NullValue != "undefined" -%}
{{ Variable }} = {{ Value }} !== undefined ? {{ Value }} : {% if HasDefaultValue %}{{ DefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
{% else -%}
{{ Variable }} = {{ Value }};
{% endif -%}
{% endif -%}
{% endif -%}