nullable fields in swagger on node.js

Instead of add null in type property, you can use default property instead.

Swagger.json property definition example:

"due_date": {
  "type": "string",
  "description": "Due date",
  "default": "null"
},

It's a valid Swagger type definition, and still appears as expected in Swagger UI.


nullable field is supported in OpenAPI (fka Swagger) Specification v3.0.0, but not in v2.0. Nullable types are defined as follows:

# Can be string or null
type: string
nullable: true

Just as a hint, because I stumpled upon this: When I added

type: string
nullable: true`

as described in the answer https://stackoverflow.com/a/42797352/2750563 my service returned only "fieldName": { "present": true } instead of an actual value!

If you see this, simply add the JsonNullableModule to your Jackson serializer, for example, if using Spring:

@Component
public class JacksonConfiguration {

    @Autowired
    public void configureJackson(ObjectMapper mapper) {
        mapper.registerModule(new JsonNullableModule());
    }

}

Then everything looks fine again.


SwaggerUI doesn't support nullable types (please, see here). But I used nullable properties as:

type: ['string','null']

After that this property disappears from UI, but validation still worked.