apiaryio/drafter.js

Missing MSON example value and type doesn't compile to `null` type in JSON schema

Closed this issue · 10 comments

Some users based on MSON documentation when they leave type and example value empty, they expect MSON will compile to type null in the JSON schema, but it unexpectedly compiles to type string. My opinion and users expectation is it should compile to null and null should be introduced into MSON as a valid primitive type.

zdne commented

Per my comment here apiaryio/mson#26 (comment) there is no such a thing as null type in MSON. In this case I believe the JSON schema is correct in the sense that it expect "string" as the type of a property.

MSON documentation / README should be updated to show empty string values instead of null where applicable.

Just FWIW, this behavior is tested in Boutique using these samples:

zdne commented

Thanks @honzajavorek I am afraid we should change it from

{
  "address": {
    "street": null,
    "city": null,
    "state": null
  }
}

to

{
  "address": {
    "street": "",
    "city": "",
    "state": ""
  }
}

cc @klokane @kylef @pksunkara

👌

I don't think generating empty strings for properties without sample values makes sense, neither that it's useful to anyone. I see two ways to approach this:

  1. Introduce some sort of "no value" as an integral part of MSON. I made a more elaborate comment under #26.
  2. In case we don't want to support null at all and we don't want to generate null in JSON, I'd go for not generating properties without values. Anything which is optional and without sample value wouldn't be present in resulting JSON object. That would make much more sense to me than empty strings.
zdne commented

Introduce some sort of "no value" as an integral part of MSON.

It actually does. You cannot have a boolean without value.

Anything which is optional and without sample value wouldn't be present in resulting JSON object. That would make much more sense to me than empty strings.

That is what I think is missing – a set of flags to control rendering of following:

  1. Render only required and fixed entities (= do not render optional entities)
  2. Do not render entities without values
zdne commented

But again @honzajavorek staying on point on what this issue is about: The type in schema is generated correctly. It is – and should be – string. The value of a string in JSON is NOT a null (because that is not a string).

nevir commented

Hmm, how about a drafter option for appending "null" to the JSON Schema type of optional properties?

Pragmatically, not doing this is pretty annoying when validating real requests/responses. A project I'm working is running the schemas against our requests/responses (as middleware).

We'd prefer to not have to recurse all our emitted/received objects to strip out nulls; and it's incredibly difficult to enforce that all of the logic that emits values won't have nulls in them.

Just a note that their is now a pull request in the MSON specifications to allow null as a type - apiaryio/dredd#194

This has been fixed by adding nullable in drafter.