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

Add support for discriminator #32

Merged
merged 8 commits into from
May 20, 2019
Merged

Conversation

Ladas
Copy link
Contributor

@Ladas Ladas commented May 15, 2019

Discriminator option is supported for oneOf and anyOf. Only
discriminator with mapping defined is supported now.

Openapi docs for discriminator and mapping:
https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/

TODOs:

  • add specs
  • special exception class for discriminator mapping that wasn't found in schema

Discriminator option is supported for oneOf and anyOf. Only
discriminator with mapping defined is supported now.

Openapi docs for discriminator and mapping:
https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/
@Ladas
Copy link
Contributor Author

Ladas commented May 15, 2019

@ota42y hello, I still need to finish specs, but I wanted to get an early feedback from you, mainly on the parsing step. I'd probably need to do a custom parser, since the mapping schema is a bit weird (no $ref keyword)

Also not sure if I can call in the parser:

discriminator.root.find_object(mapping[value[property_name]])

is that lazily evaluated?

Anyway, let me know what is acceptable for you and I'll finish the specs tomorrow.

@Ladas Ladas force-pushed the add_discriminator_support branch from 6d58f3b to 10e9070 Compare May 16, 2019 12:18
@Ladas Ladas changed the title [WIP]Add support for discriminator [WIP] Add support for discriminator May 16, 2019
end

# TODO: this is likely O(n) for each validation, we need to find it once during parsing
resolved_schema = discriminator.root.find_object(mapping_target)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ota42y so I am trying to move this to https://github.com/Ladas/openapi_parser/blob/c844e37dba4cbe605f9a3f8d565f84843acfe5bb/lib/openapi_parser/schemas/schema.rb#L115, to my own parser, but after spending several hours there I didn't figure out how it works. :-) Could you give me some hints? Is it even possible to do the lookup there?

If the discriminator.root.find_object(mapping_target) is or would be O(1), I don't really need to do that. :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like the find_object does O(n) lookup at worst, then caches the result, so this should be ok performance wise

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, find_object method dose O(n) lookup at worst, but we cached result so I think there is no problem 👍
https://github.com/ota42y/openapi_parser/blob/master/lib/openapi_parser/concerns/findable.rb#L4

@Ladas Ladas changed the title [WIP] Add support for discriminator Add support for discriminator May 17, 2019
@ota42y ota42y self-requested a review May 17, 2019 11:40
Copy link
Owner

@ota42y ota42y left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for sending PR!!!

I am sorry for the late reply 🙇
There is no problem where the question was asked.
But I want you to fix only one place in data structure.

@@ -101,6 +102,18 @@ class Schema < Base
# @return [Hash{String => Schema}, nil]
openapi_attr_hash_object :properties, Schema, reference: true

# @!attribute [r] discriminator
# @return [Hash{String => Schema}, nil]
openapi_attr_objects :discriminator, Schema
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, we defines schemas to be the same attribute in OpenAPI 3 specification as much as possible.
So please create Discriminator class and move property_name and mapping to it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that make sense, Discriminator class added

end

# TODO: this is likely O(n) for each validation, we need to find it once during parsing
resolved_schema = discriminator.root.find_object(mapping_target)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, find_object method dose O(n) lookup at worst, but we cached result so I think there is no problem 👍
https://github.com/ota42y/openapi_parser/blob/master/lib/openapi_parser/concerns/findable.rb#L4

"name" => "Mr. Cat",
"born_at" => "2019-05-16T11 =>37 =>02.160Z",
"description" => "Cat gentleman",
"nut_stock" => 10 # passing squirrel attribute here, but discriminator still picks cats and fails
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perfect!! 🙆 🙆 🙆 🙆

@Ladas Ladas force-pushed the add_discriminator_support branch 2 times, most recently from c693ed4 to bdb90ed Compare May 17, 2019 12:36
@Ladas
Copy link
Contributor Author

Ladas commented May 17, 2019

@ota42y that you for the review, the data structure issue has been fixed. :-)

@ota42y
Copy link
Owner

ota42y commented May 20, 2019

This is a great feature!!!
Thank you very much!

@ota42y ota42y merged commit 0f1db9e into ota42y:master May 20, 2019
@ota42y
Copy link
Owner

ota42y commented May 20, 2019

I just released 0.2.6 and which includes this feature 🙋‍♂

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

Successfully merging this pull request may close these issues.

2 participants