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

Properties in different classes inheriting same base classes #22

Closed
andreas-aeschlimann opened this issue Jul 31, 2017 · 7 comments
Closed
Assignees

Comments

@andreas-aeschlimann
Copy link
Member

I should look into the following issue:

If two classes inherit from the same base class and use different mappings for the same property name, there will be a conflict in json2typescript.

@andreas-aeschlimann andreas-aeschlimann self-assigned this Jul 31, 2017
@kirakishin
Copy link

Hello,
i use abstract base class : child class A extends B. its seems it doesn't work with this use case.
could check that please ?

regards

@andreas-aeschlimann
Copy link
Member Author

Can you please provide a minimal example (just the classes you would like to use) so that it fails?

@kirakishin
Copy link

sorry,
i found why : forgot undefined on my properties :/
best regards

@andreas-aeschlimann
Copy link
Member Author

Glad you fixed it. Yes, it's annoying to initialize all properties, but it's necessary.

@Sooulm8
Copy link

Sooulm8 commented Sep 4, 2017

Hi,

I have the same problem even with properties initialized.
Can you give me some help ?

My classes

import {JsonObject, JsonProperty} from 'json2typescript';

@JsonObject
export class TestAbstract {
  @JsonProperty('title', String)
  private _title: string = undefined;`

  constructor() {
  }

  get title(): string {
    return this._title;
  }

  set title(value: string) {
    this._title = value;
  }
}


@JsonObject
export class TestString extends TestAbstract {
  @JsonProperty('value', String)
  private _value: string = undefined;

  constructor() {
    super();
  }

  get value(): string {
    return this._value;
  }

  set value(value: string) {
    this._value = value;
  }
}

@JsonObject
export class TestNumber extends TestAbstract {
  @JsonProperty('value', Number)
  private _value: number = undefined;

  constructor() {
    super();
  }


  get value(): number {
    return this._value;
  }

  set value(value: number) {
    this._value = value;
  }
}

My test exemple

let jsonConvert = new JsonConvert();
jsonConvert.operationMode = OperationMode.LOGGING; 
jsonConvert.valueCheckingMode = ValueCheckingMode.ALLOW_NULL; 
const test = jsonConvert.deserialize({title: 'Title1', value: 1}, TestNumber);
const testString = this.jsonConvert.deserialize({title: 'Title2', value: 'test string'}, TestString);
console.log(test);
console.log(testString);

The error I get

core.es5.js:1020 ERROR Error: Uncaught (in promise): Error: Fatal error in JsonConvert. Failed to map the JSON object to the class "TestString" because of a type error.

	Class property: 
		_value

	Expected type: 
		number

	JSON property: 
		value

	JSON type: 
		string

	JSON value: 
		"test string"

	Reason: Given object does not match the expected primitive type.

Error: Fatal error in JsonConvert. Failed to map the JSON object to the class "TestString" because of a type error.

	Class property: 
		_value

	Expected type: 
		number

	JSON property: 
		value

	JSON type: 
		string

	JSON value: 
		"test string"

	Reason: Given object does not match the expected primitive type.

You can see I have 2 children with attribute _value.
In TestNumber is typed as Number
In TestString is typed as String

But when I try to deserialize, json2typeScript try to convert my string in number for TestString.

Thank you in advance for your answer.

@andreas-aeschlimann
Copy link
Member Author

andreas-aeschlimann commented Sep 4, 2017

As mentioned in the first post, this issue is to be expected. Right now, it is not possible to use the same property name with class inheritance.

There is an easy workaround though:

No one (except you) will care about the name of the private property. You will be exposing only the public getter/setter. Just rename the _value of the second class to _value2 or whatever, but keep the getter/setter names.

I will fix this issue in the next release, of course.

@andreas-aeschlimann
Copy link
Member Author

andreas-aeschlimann commented Sep 9, 2017

@Sooulm8 the current version (1.0.3) fixes this problem.

There is only one case I couldn't properly "fix". If you consider your example, it is important that if you write the decorator JsonProperty at _value in TestString, you must also write it in TestNumber. Otherwise it will make a "fallback" and cause an error.

That means: Your example should work exactly like you posted it, but it will fail if you remove the decorator from one but not both TestStringand TestNumber property _value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants