nullable fields in swagger on node.js

前端 未结 4 660
[愿得一人]
[愿得一人] 2021-01-04 02:42

I\'ve spent a bunch of time trying to find a solution for creating swagger docs in Node.JS. The main library is swagger-node, in which you create a swagger yaml file and the

相关标签:
4条回答
  • 2021-01-04 02:47

    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.

    0 讨论(0)
  • 2021-01-04 02:54

    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.

    0 讨论(0)
  • 2021-01-04 02:56

    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.

    0 讨论(0)
  • 2021-01-04 03:09

    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
    
    0 讨论(0)
提交回复
热议问题