How to specify a property can be null or a reference with swagger

后端 未结 2 1850
余生分开走
余生分开走 2020-12-11 15:33

How to specify a property as null or a reference? discusses how to specify a property as null or a reference using jsonschema.

I\'m looking to do the same thing with

相关标签:
2条回答
  • 2020-12-11 16:19

    Not easy to do that. Even almost impossible. Your options :

    Wait

    There is a very long discussion about this point, maybe one day it will be done...

    Use vendors extensions

    You can use vendors extensions like x-oneOf and x-anyOf. I have already taken this hard way: You must to upgrade all used 'swagger tools' to take into account these vendors extensions.

    In my case, we needed 'only' to :

    • Develops our own Jax-RS parser with customized annotations in order to extract swagger API file from sources
    • Extends swagger-codegen to take into account these extensions to generate java code for our clients
    • Develops our own swagger-ui: to facilitate this work, we added a preprocessing step to convert our swagger schema with our extensions to a valid json schema. It's easier to find a module to represent json schemas than swagger schemas in javascript. By cons we gave up the idea to test the API with the 'try it' button.

    It was a year ago, maybe now ...

    Refactor your APIs

    Many projects don't need anyOf and oneOf, why not us ?

    0 讨论(0)
  • 2020-12-11 16:22

    In OpenAPI 3.0, you can use:

    "foo": {
        "nullable": true,
        "allOf": [
            {
                "$ref": "#/definitions/Foo"
            }
        ]
    }
    

    YAML version:

    foo:
      nullable: true
      allOf:
      - $ref: '#/definitions/Foo'
    

    Wrapping $ref into allOf is needed to combine the $ref with other keywords - because $ref overwrites any sibling keywords. This is further discussed in the OpenAPI Specification repository: Reference objects don't combine well with “nullable”

    0 讨论(0)
提交回复
热议问题