Hi,
Some OpenEHR REST API methods require the specific Content-Type header, for example, “/rest/openehr/v1/definition/template/adl1.4” (createTemplateClassic)
Let’s take a look at the code generated by NSwag:
createTemplateClassic(openEHR_VERSION: string | undefined, openEHR_AUDIT_DETAILS: string | undefined, content_Type: string, accept: string | undefined, prefer: string | undefined, body: string): Promise<string> {
let url_ = this.baseUrl + "/rest/openehr/v1/definition/template/adl1.4";
url_ = url_.replace(/[?&]$/, "");
const content_ = JSON.stringify(body);
let options_: RequestInit = {
body: content_,
method: "POST",
headers: {
"openEHR-VERSION": openEHR_VERSION !== undefined && openEHR_VERSION !== null ? "" + openEHR_VERSION : "",
"openEHR-AUDIT_DETAILS": openEHR_AUDIT_DETAILS !== undefined && openEHR_AUDIT_DETAILS !== null ? "" + openEHR_AUDIT_DETAILS : "",
"Content-Type": content_Type !== undefined && content_Type !== null ? "" + content_Type : "", /// !!!
"Accept": accept !== undefined && accept !== null ? "" + accept : "",
"Prefer": prefer !== undefined && prefer !== null ? "" + prefer : "",
"Content-Type": "application/json", /// !!!
}
};
return this.http.fetch(url_, options_).then((_response: Response) => {
return this.processCreateTemplateClassic(_response);
});
}
Generated code marked with !!! is wrong, as here you can see that user-defined content_Type header will be overwritten with “application/json”.
Here is a code generated with swagger-codegen-cli:
createTemplateClassic: async (body: string, content_type: string, open_ehr_version?: string, open_ehr_audit_details?: string, accept?: string, prefer?: string, options: AxiosRequestConfig = {}): Promise<RequestArgs> => {
// verify required parameter 'body' is not null or undefined
if (body === null || body === undefined) {
throw new RequiredError('body','Required parameter body was null or undefined when calling createTemplateClassic.');
}
// verify required parameter 'content_type' is not null or undefined
if (content_type === null || content_type === undefined) {
throw new RequiredError('content_type','Required parameter content_type was null or undefined when calling createTemplateClassic.');
}
const localVarPath = `/rest/openehr/v1/definition/template/adl1.4`;
// use dummy base URL string because the URL constructor only accepts absolute URLs.
const localVarUrlObj = new URL(localVarPath, 'https://example.com');
let baseOptions;
if (configuration) {
baseOptions = configuration.baseOptions;
}
const localVarRequestOptions :AxiosRequestConfig = { method: 'POST', ...baseOptions, ...options};
const localVarHeaderParameter = {} as any;
const localVarQueryParameter = {} as any;
if (open_ehr_version !== undefined && open_ehr_version !== null) {
localVarHeaderParameter['openEHR-VERSION'] = String(open_ehr_version);
}
if (open_ehr_audit_details !== undefined && open_ehr_audit_details !== null) {
localVarHeaderParameter['openEHR-AUDIT_DETAILS'] = String(open_ehr_audit_details);
}
if (content_type !== undefined && content_type !== null) {
localVarHeaderParameter['Content-Type'] = String(content_type); // !!!
}
if (accept !== undefined && accept !== null) {
localVarHeaderParameter['Accept'] = String(accept);
}
if (prefer !== undefined && prefer !== null) {
localVarHeaderParameter['Prefer'] = String(prefer);
}
localVarHeaderParameter['Content-Type'] = 'application/json'; /// !!!
const query = new URLSearchParams(localVarUrlObj.search);
for (const key in localVarQueryParameter) {
query.set(key, localVarQueryParameter[key]);
}
for (const key in options.params) {
query.set(key, options.params[key]);
}
localVarUrlObj.search = (new URLSearchParams(query)).toString();
let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers};
const needsSerialization = (typeof body !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");
return {
url: localVarUrlObj.pathname + localVarUrlObj.search + localVarUrlObj.hash,
options: localVarRequestOptions,
};
},
Same problem.
Could that be fixed somehow on server side?
PS NSwag does one more wrong thing:
const content_ = JSON.stringify(body);
This will not work with XML, so I guess that there is something wrong with server Accept headers as well.