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

Introduce SIngleton type #413

Closed
wants to merge 2 commits into from
Closed

Introduce SIngleton type #413

wants to merge 2 commits into from

Conversation

Bert-Proesmans
Copy link

I'm currently working with JSON schemas and encountered a use case where an object property is allowed to contain ONLY ONE of the provided objects. Which I named the Singleton type, but another name might fit better.

The generated code declares the property type of a Singleton as a (generic) object, which is enough since resolving/downcasting that object falls under application logic.

@RicoSuter
Copy link
Owner

RicoSuter commented Jun 26, 2017

Hi, thanks for the PR. But I don't think using "object" or "any" if a schema is a "singleton" is correct.

#13

In your sample, it is only any, if the oneOf schema is also any... Maybe we just have to check in IsAnyType:

IsAnyType => [...current logic] && OneOf.All(s => s.IsAnyType)

@Bert-Proesmans
Copy link
Author

Bert-Proesmans commented Jun 26, 2017

You're right, a lot of information is lost by using object. I thought about it for some time but couldn't find a clean way to implement it in C#. I haven't used typescript so i can't attribute to the topic regarding it.

As far as i know the options are building a C-like enum [1], using templates [2], or a very verbose wrapper class [1-Peter N Roth's answer]. All three options have downsides and a compromis should be made for the least unnatural way of writing C#.

My preference lies not with the template option.

update: Regarding your suggestion on the code of my PR; What you propose sounds right but i don't think AnyType definition should change.
That definition with a changed OneOf check wouldn't pass the test for what i meant by Singleton. It's really difficult to abstract the definition to encompass both the current and 'Singleton' domain.

@RicoSuter
Copy link
Owner

Your unit test passes even when no other changes are made in the codebase... What exactly is your problem?

I think oneOf should be handled with inheritance, i.e. all classes/schemas in oneOf inherit from the base schema...

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