Swagger schema properties ignored when using $ref - why?

前端 未结 1 1555
你的背包
你的背包 2020-12-16 20:40

I\'m trying to build a Swagger model for a time interval, using a simple string to store the time (I know that there is also datetime):

definitions:
  Time:
         


        
相关标签:
1条回答
  • 2020-12-16 21:03

    $ref works by replacing itself and all of its sibling elements with the definition it is pointing at. That is why

          lowerBound:
            $ref: "#/definitions/Time"
            description: Lower bound on the time interval.
            default: "00:00"
    

    becomes

          lowerBound:
            type: string
            description: Time in 24 hour format "hh:mm".
    


    A possible workaround is to wrap $ref into allOf - this can be used to "add" attributes to a $ref but not override existing attributes.

          lowerBound:
            allOf:
              - $ref: "#/definitions/Time"
            description: Lower bound on the time interval.
            default: "00:00"
    

    Another way is to use replace the $ref with an inline definition.

    definitions:
      TimeInterval:
        type: object
        properties:
          lowerBound:
            type: string  # <------
            description: Lower bound on the time interval, using 24 hour format "hh:mm".
            default: "00:00"
          upperBound:
            type: string  # <------
            description: Upper bound on the time interval, using 24 hour format "hh:mm".
            default: "24:00"
    


    The usability of $ref is being discussed in the OpenAPI spec repository here and here so maybe this will be improved in one of the future versions.

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