json-editor icon indicating copy to clipboard operation
json-editor copied to clipboard

Don't include properties that are not required in the value

Open mohsen1 opened this issue 10 years ago • 7 comments

With this schema:

{
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "age": {
      "type": "integer"
    }
  }
}

If user doesn't put any data in, the output value should be {}. None of the properties are required.

If a property is required, then it makes sense to fill in the value for required property with some default value:

{
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "age": {
      "type": "integer"
    }
  },
  "required": ["age"]
}

This should output {"age": 0} since "age" is required and empty object will not conform to the schema

mohsen1 avatar Jun 01 '15 20:06 mohsen1

There seems to be an undocumented option, remove_empty_properties: true that basically does this. It doesn't seem to check whether the property is required though, so if a user leaves a blank value, you will get JSON out that doesn't pass validation. Which could then cause dramas if you round-trip.

stevage avatar Jun 04 '15 04:06 stevage

@stevage Thank you very much! That saved me a lot of work. It would be nice if remove_empty_properties is true then it will do what it was doing for required properties.

mohsen1 avatar Jun 04 '15 17:06 mohsen1

Yeah. Btw, I was slightly wrong - it's not "undocumented", but it's documented as an editor-level property (not for the whole schema).

stevage avatar Jun 05 '15 00:06 stevage

Also, now that I look at this more closely, I think there is a bug:

{
  "type": "object",
  "options": { "remove_empty_properties": true} ,
  "required": [ "age" ],
  "properties": {
    "name": {
      "type": "string"
    },
    "age": {
      "type": "integer"
    }
  }
}

This doesn't let you use an age of 0 because it gets removed, even though it's required.

stevage avatar Jun 18 '15 02:06 stevage

Right, https://github.com/swagger-api/swagger-editor/issues/482 is exactly this issue

mohsen1 avatar Jun 18 '15 04:06 mohsen1

Any update on this?

centromere avatar Aug 06 '16 13:08 centromere

Hello; For C# (.net 6 ), when you correct your bool value as follows, the false value swagger also works. public bool? IsBlock { get; set; } = false; "worked after adding a question mark"

good work

bozkurt30 avatar Dec 01 '22 11:12 bozkurt30