Skip to content

Commit

Permalink
setObjectNotExists also should replace non allowed chars (#2460)
Browse files Browse the repository at this point in the history
* prepare release

* setObjectNotExists also should replace non allowed chars

- closes #2459
- see iobroker-community-adapters/ioBroker.mqtt-client#233
  • Loading branch information
foxriver76 authored Sep 28, 2023
1 parent fd34503 commit 5d3ad27
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 34 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Placeholder for the next version (at the beginning of the line):
## __WORK IN PROGRESS__
-->
## 5.0.12 (2023-08-18) - Jana
## __WORK IN PROGRESS__ - Jana
**BREAKING CHANGES**
* Support for Node.js 12 and 14 is dropped! Supported are Node.js 16.4.0+ and 18.x
* Backups created with the new js-controller version cannot be restored on hosts with lower js-controller version!
Expand Down
64 changes: 31 additions & 33 deletions packages/adapter/src/lib/adapter/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3103,12 +3103,7 @@ export class AdapterClass extends EventEmitter {
}

options.id = this._utils.fixId(options.id, false);

const mId = options.id.replace(FORBIDDEN_CHARS, '_');
if (mId !== options.id) {
this._logger.warn(`${this.namespaceLog} Used invalid characters: ${options.id} changed to ${mId}`);
options.id = mId;
}
options.id = this.fixForbiddenCharsInId(options.id);

if ('children' in options.obj || 'parent' in options.obj) {
this._logger.warn(`${this.namespaceLog} Do not use parent or children for ${options.id}`);
Expand Down Expand Up @@ -3316,13 +3311,7 @@ export class AdapterClass extends EventEmitter {
obj.user = obj.user || options?.user || SYSTEM_ADMIN_USER;
obj.ts = obj.ts || Date.now();

if (id) {
const mId = id.replace(FORBIDDEN_CHARS, '_');
if (mId !== id) {
this._logger.warn(`${this.namespaceLog} Used invalid characters: ${id} changed to ${mId}`);
id = mId;
}
}
id = this.fixForbiddenCharsInId(id);

// check that alias is valid if given
if (obj.common && 'alias' in obj.common && obj.common.alias.id) {
Expand Down Expand Up @@ -3405,12 +3394,6 @@ export class AdapterClass extends EventEmitter {
return tools.maybeCallbackWithError(callback, err);
}

const mId = id.replace(FORBIDDEN_CHARS, '_');
if (mId !== id) {
this._logger.warn(`${this.namespaceLog} Used invalid characters: ${id} changed to ${mId}`);
id = mId;
}

Validator.assertString(id, 'id');

if (!obj) {
Expand All @@ -3424,7 +3407,12 @@ export class AdapterClass extends EventEmitter {
}
Validator.assertOptionalCallback(callback, 'callback');

return this._extendForeignObjectAsync({ id, obj: obj as ioBroker.SettableObject, callback, options });
return this._extendForeignObjectAsync({
id: this.fixForbiddenCharsInId(id),
obj: obj as ioBroker.SettableObject,
callback,
options
});
}

private async _extendForeignObjectAsync(
Expand Down Expand Up @@ -4878,9 +4866,12 @@ export class AdapterClass extends EventEmitter {
return tools.maybeCallbackWithError(callback, err);
}

id = this._utils.fixId(id);

return this._setObjectNotExists({ id: id as string, obj: obj as any, options, callback });
return this._setObjectNotExists({
id: this.fixForbiddenCharsInId(this._utils.fixId(id)),
obj: obj as any,
options,
callback
});
}

private async _setObjectNotExists(
Expand Down Expand Up @@ -8369,11 +8360,7 @@ export class AdapterClass extends EventEmitter {
return tools.maybeCallbackWithError(callback, warn);
}

const mId = id.replace(FORBIDDEN_CHARS, '_');
if (mId !== id) {
this._logger.warn(`${this.namespaceLog} Used invalid characters: ${id} changed to ${mId}`);
id = mId;
}
id = this.fixForbiddenCharsInId(id);

if (options?.user && options.user !== SYSTEM_ADMIN_USER) {
let obj: ioBroker.StateObject;
Expand Down Expand Up @@ -8680,11 +8667,7 @@ export class AdapterClass extends EventEmitter {
typeof state.from === 'string' && state.from !== '' ? state.from : `system.adapter.${this.namespace}`;
state.user = options?.user || SYSTEM_ADMIN_USER;

const mId = id.replace(FORBIDDEN_CHARS, '_');
if (mId !== id) {
this._logger.warn(`${this.namespaceLog} Used invalid characters: ${id} changed to ${mId}`);
id = mId;
}
id = this.fixForbiddenCharsInId(id);

if (options && options.user && options.user !== SYSTEM_ADMIN_USER) {
try {
Expand Down Expand Up @@ -12102,6 +12085,21 @@ export class AdapterClass extends EventEmitter {
}
}

/**
* Replaces forbidden chars in an id if present
* Additionally logs a warning
*
* @param id the id which will be replaced
*/
private fixForbiddenCharsInId(id: string): string {
const mId = id.replace(FORBIDDEN_CHARS, '_');
if (mId !== id) {
this._logger.warn(`${this.namespaceLog} Used invalid characters: ${id} changed to ${mId}`);
}

return mId;
}

private async _init(): Promise<void> {
/**
* Initiates the databases
Expand Down

0 comments on commit 5d3ad27

Please sign in to comment.