Make schema object compatible with JSON Schema draft4
According to spec swagger inside schema object support only limited subset of JSON Schema draft4 and extend it with custom keywords.
This isssue was hidden, before 68c6777dc5eba98dc1eb791983693f089e9d3366 commit any swagger model with arbitrary JSON schema was valid(against Swagger schema).
These create a problem, because in reality most of tools use standard JSON schema validators(for e.g. swagger-tools use z-schema, swagger-express-middleware use tv4). And I didn't found any true Swagger 2.0 validator in the wild.
Another problem is that you can't reference existing schemes inside your swagger files. It became real problem, if you want to use some standard scheme or simply reuse your existing validation scheme. For example if you make API that return swagger definitions and want to reuse existing schema, you can't simply use '$ref' or even copy-paste it.
Finally it brake compatibility with all tooling around JSON Schema not only validators but also documentation generators, UI form generators, etc.
On technical side, this is list of Draft4 keywords doesn't supported by Swagger: id, $schema, additionalItems, definitions, patternProperties, dependencies, anyOf, oneOf, not.
Also there are "discriminator" and "readOnly" Swagger specific keywords that don't supported by JSON Schema validators. "discriminator" could be safely substitute with "oneOf" construction, as for "readOnly" same behaviour could be achieved by using "allOf" with "write part" and "read-only part". My proposal it either to remove these keywords or make them optional to support.
Also there are "safe" custom keywords namely "example" and "externalDocs", they shouldn't cause any serious trouble, but should be submitted as requests for Draft5 to ensure future compatibility.
Last keyword is "xml", I didn't make any research about it, so I can't say if it used in any Swagger related project or if it brake JSON schema compatibility.
@jamiehannaford, it just one of options and maybe it will be useful for other people reading this issue. Right now, there no public plans for extending official Swagger spec, so I think common protocol extensions is good alternative.
But if you're writing a Swagger file to power a frontend (e.g. swagger-ui), will they know to strip out the x- prefix? Will validators work out of the box too?
No, almost every tool will simply ignore x-oneOf. But adding such support is easy, just map 'x-oneOf' => 'oneOf' before validation.
I ended up simplifying up the JSON patch schema like so  Not great but it works...
But you still need to validate the same constraints that you express with oneOf.
IMHO, it better solution than separate JSON Schema file or do validation inside code.
@IvanGoncharov Would you be able to provide two samples which fully explain and show your explanation of the problem in https://github.com/swagger-api/swagger-spec/issues/333#issuecomment-94594048 please? Specifically discriminator. I'm not sure I fully understand the problem, and would like to before trying to explain to others.
@Relequestual Right now it has some problems(see #403) and they definitely could be solved.
Disclaimer: All below text is my personal opinion.
I think discriminator is broken on conceptual level.
Main problem is reinventing the wheel, JSON Schema already provide oneOf which is superset discriminator(that mean any schema using discriminator can be rewrited with oneOf).
All other problems is consequence of the first one:
- I didn't saw any real support in tools and according to @webron: "The whole model inheritance is unfortunately underdeveloped in the tools and we're behind."
- It less flexible than
oneOf, main thing it require you to have dedicated field in your JSON payload. This mean that in majority of cases you should redesign your implementation. Which isn't what people want than they try to document existing implementations. - Moreover it break separation between data and metadata. You data become aware about of some types in JSON Schema.
And generally this feature not extend JSON Schema but replace oneOf. This lead to situation where in order to use both Swagger and JSON Schema(for example on DB level) you need to maintain two incompatible specifications.
In my particular example I tried to create API interface to collection of Swagger files, so I need to document function which return Swagger file. I naively thought that I could reuse official JSON Schema to describe payload, but it use a lot of oneOf. So in that sense Swagger 2.0 it isn't self hosted :)
I personally solve this issues using this approach https://github.com/swagger-api/swagger-spec/issues/333#issuecomment-142031569.
P.S. If you I didn't answer your question or you have some other questions we can chat here
Well said. +1
On Oct 1, 2015, at 8:31 AM, Ivan Goncharov [email protected] wrote:
@Relequestual Right now it has some problems(see #403) and they definitely could be solved.
Disclaimer: All below text is my personal opinion. I think discriminator is broken on conceptual level. Main problem is reinventing the wheel, JSON Schema already provide oneOf which is superset discriminator(that mean any schema using discriminator can be rewrited with oneOf).
All other problems is consequence of the first one:
It doesn't define well in spec, see #403 I didn't saw any real support in tools and according to @webron: "The whole model inheritance is unfortunately underdeveloped in the tools and we're behind." It less flexible than oneOf, main thing it require you to have dedicated filed in your JSON payload. This mean that in majority of cases you should redesign your implementation. Which isn't what people want than they try to document existing implementations. Moreover it break separation between data and metadata. You data become aware about of some types in JSON Schema. And generally this feature not extend JSON Schema but replace oneOf. This lead to situation where in order to use both Swagger and JSON Schema(for example on DB level) you need to maintain two incompatible specifications.
In my particular example I tried to create API interface to collection of Swagger files, so I need to document function which return Swagger file. I naively thought that I could reuse official JSON Schema to describe payload, but it use a lot of oneOf. So in that sense Swagger 2.0 it isn't self hosted :)
I personally solve this issues using this approach #333 (comment).
P.S. If you I didn't answer your question or you have some other questions we can chat here
— Reply to this email directly or view it on GitHub.
I believe this comment is relevant for this issue so I reference it here. It basically present new problem(at least for this thread) with adoption of Swagger, that is not so oblivious.
I'd like to throw my hat in the ring for at least including patternProperties in the specification as the argument seems to be centering around oneOf, anyOf, and discriminator. I know everyone has their own use case, but not having patternProperties and failing validation is quite frustrating.
I guess, in the case of patternProperties, I'm not sure why it was left out -- what was the reason for this? It is extremely similar to properties with the difference being that the names of the children are regular expressions. Maybe it was validating that regular expression as being properly formed?
@mm-gmbd Actually patternProperties is very similar to the oneOf.
You don't know type of property until you received data, so you can't generate class/structure for it.
In case of additionalProperties everything is simpler you just have map with string as key and fixed type as value.
@mm-gmbd I would suggest you to use x-patternProperties as vendor extension. And convert to patternProperties before validation.
@IvanGoncharov, so, essentially, the reason for leaving these out is to enforce users to better define specifications rather than leave "grey" areas in APIs. I'm okay with that :+1:
Glad to find I'm not the only one struggling with the "discriminator" as a poor replacement for oneOf. It's been feeling like you either have to change your API to align with Swagger's opinions, or add a lot of non-machine readable descriptions to the docs.
+1 on getting oneOf or an equivalent working....you simply need polymorphism support to document APIs implemented in an OO language
Only that oneOf allow for far more than polymorphism. I'm inclined to agree that discriminator doesn't cover everything and there's room for improvement, and yet, I don't think adding support for oneOf (or anyOf) is the right solution. We'll definitely be discussing this in the next version and it may get included. I'd just rather see a different solution.
I think oneOf makes sense knowing that standard JSON Schema tooling would support it as-is without needing any special casing. In fact, the tools that I've been involved with would require no changes to support oneOf from a Swagger validation perspective. (This is because those tools were written for a non-typed language.) Where I agree with @webron is that as simple as this change would seem to be from a Swagger specification perspective, this has a big impact on tooling like swagger-codegen and other integrations/tools, like those built for typed languages.
That being said, I can understand the frustration of being told we can't support something because the tooling built around it would become harder to write. But we do have to take things like this into consideration.
I think @whitlockjc summed up the frustration pretty clearly. I can't accurately describe my existing APIs because swagger wants to limit itself to APIs that are easy to generate code for in statically typed languages. I can understand why the folks on the static side want the restriction but it feels very artificially limiting.
@drewish anything is on the table for the next version of the spec. The flip side of your frustration is that supporting oneOf in the response schema would make code generation for static typed languages nearly impossible, and therefore remove that whole functionality for all users. We did choose--for 1.x and 2.x--to require that static typed languages could be generated. Should that constraint go away in the future?
Remember you can do anything by returning object in the schema. Then static or not, you can do whatever you like with the response. Like Burger King, you can have it your way
I do think that the suggested workaround is somewhat problematic because then the benefit of using Swagger is lost. If I have to use an empty schema to describe the parameters/responses that Swagger cannot support, I basically do not benefit from Swagger in those cases. No documentation, other than the existence of some parameter/response, and no validation because any value would pass validation based on the provided schema. So while this is a valid workaround for Swagger 1.x and 2.x, it is not ideal for the reasons mentioned above.
I bring this up not to question previous design decisions because what's done is done. I bring it up only because information like this might have an impact on whether it makes sense to support oneOf in the future.
@drewish - don't get me wrong, the need is clear, the frustration is clear. I really wish it made sense to address everything, but I don't believe we can or should.
Let's say I provide you with the following blueprint for a structure:

Is it beautiful? Sure. Is it usable? Perhaps in some dimensions (not kidding here, just trying to convey an idea). But it's definitely not usable by many commons.
One of the goals we need to keep in mind is whether we want to just be able to describe everything, usable or not, or do we still want to say that some things just can't be described. In both cases some users (either as producers or consumers) will end up not enjoying the spec. I prefer it to be the producer that 'suffers' because to it me it is more important for the consumer to know that no matter what spec is thrown at them, they could still use it.
With all that in mind, oneOf is a solution to a problem. It doesn't mean it has to be the solution. I'd rather people described their use cases in details to see where the spec falls short so we can see if and how we can solve their issues without potentially introducing new ones. Can't promise we'll be successful, but at least it would be an educated process.
It's clear everything can't be supported but on the flip side it's essential to provide some sort of way to document these edge use cases. People will get frustrated and go elsewhere and nobody wants that.
I'm afraid that's not accurate. Our goal is not to cover all use cases or edge cases (especially if those conflict with our other goals). There's simply no way to make everyone happy. We realize it may not be suitable for everyone and that some may look for alternatives. On the flip side, providing support for some of these edge cases may cause frustration to other users and they'll go elsewhere. Personally, the goal of having API consumers being able to rely on the ability to implement any spec supersedes the goal of being to describe all API designs.
I'll restate though - it looks like everyone is focused on a specific solution to a problem, when there can be other solutions that would satisfy both goals. I'm afraid not many explicit use cases are shared, so it would be difficult to propose such alternatives.
I'm sorry but these things you're calling edge cases are not edge cases. They are commonly used patterns for quite a long time. I agree some of the requests are not feasible and don't make sense but when you have people trying to evangelize your product at their organization hut we have no way to document a simple Map or polymorphism you find yourself in a corner defending something you believe in. Then we come on here and politely ask for enhancements which may or may not get shot down. It's a risk we take understandably. I want to get my point across that I'm not frustrated or have any disdain about Swagger I truly love it and the people I interact with on here. I think it's important for everyone to show patience and keep an open mind to drive people towards this, not away