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

Failing Validation: Inheritance #224

Open
sasis-pedramkhorsandy opened this issue Oct 14, 2016 · 6 comments
Open

Failing Validation: Inheritance #224

sasis-pedramkhorsandy opened this issue Oct 14, 2016 · 6 comments
Milestone

Comments

@sasis-pedramkhorsandy
Copy link

There seems to be a problem with the validation of inherited values.

By adding the following two lines of code

var schema = JsonSchema4.FromType<Container>();
var errors = schema.Validate(json);

at the end of the test method When_JsonInheritanceConverter_is_used_then_inheritance_is_correctly_serialized_and_deserialized() validation will return 1 error.

{NotOneOf: #/Animal
{
  NoAdditionalPropertiesAllowed: #/Animal.Bar
  NoAdditionalPropertiesAllowed: #/Animal.SubElements
}
{
  NullExpected: #/Animal
}
}

Am I doing something wrong or is this a known issue?

@RicoSuter
Copy link
Owner

Yes, this is a problem.. but because I mainly use NJsonSchema for code generation (http://nswag.org) it was not an issue...

RicoSuter added a commit that referenced this issue Oct 18, 2016
@RicoSuter
Copy link
Owner

The problem is that for validation we need oneOf inheritance (ie define the child classes): #13

... and for swagger generation we need allOf inheritance (ie define parent classes)

@RicoSuter
Copy link
Owner

But oneOf inheritance is not supported by swagger (oneOf is not defined)

@sasis-pedramkhorsandy
Copy link
Author

sasis-pedramkhorsandy commented Oct 24, 2016

As there is yet no real property inheritance / overwrite support in the json schema standard I handle validation with a custom json schema structure.

For instance: I have two classes Person and Player. The Player class inherits all the properties from the Person class. Validaton should pass for instances of both classes. AddtionalProperties should be restricted.

My working json schema looks as follows:

{ "additionalProperties": true, "definitions": { "Person": { "additionalProperties": false, "properties": { "PersonProperty": { "type": "string" }, "Type": { "type": "string", "enum": ["Person"] } } }, "Player": { "additionalProperties": false, "properties": { "PersonProperty": { "type": "string" }, "PlayerProperty": { "type": "string" }, "Type": { "type": "string", "enum": ["Player"] } } } }, "oneOf": [{ "$ref": "#/definitions/Person" }, { "$ref": "#/definitions/Player" }] }

As you can see, I had to duplicate the Person properties within the Player schema and use a enum restricted Type string property as suggested here.

Leaving out the allOf reference in the Player schema to the Person schema will somehow break the inheritance configuration, but using allOf does then only pass validation if I set addtionalProperties to true on the Person and Player schemas. Something I want to avoid.

I also tried out other allOf, oneOf configurations and relocations of the Type property without any success. Property inheritance and overwrites are not yet supported.

There might be a better approach here and I would be happy for any inputs. However, I am sure that the output provided by NJsonSchema does not yet create the json schema structure for the validations I need.

@RicoSuter
Copy link
Owner

Did you also try the FlattenInheritanceHierarchy option?

@RicoSuter
Copy link
Owner

For validation we need: #13

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

No branches or pull requests

2 participants