davishmcclurg / json_schemer

JSON Schema validator. Supports drafts 4, 6, 7, 2019-09, 2020-12, OpenAPI 3.0, and OpenAPI 3.1.
MIT License
408 stars 64 forks source link

Nullable not working correctly when using `$ref` #199

Open bilby91 opened 2 days ago

bilby91 commented 2 days ago

Hello @davishmcclurg!

I stumble upon this issue where nullable: true is not honored when used in a field declared with $ref. This is my exact example.

Volume:
      type: object
      description: A representation of a volume in the system, which can be attached to virtual machines.
      properties:
        id:
          type: integer
          format: int32
          description: The unique identifier of the volume.
          example: 1
        object:
          type: string
          enum: [volume]
          description: The object type, which is always "volume" for volumes.
        name:
          type: string
          description: The name of the volume.
          example: swagger-vol
        size_bytes:
          type: integer
          format: int64
          description: The size of the volume, in bytes.
          example: 10737418240
        status:
          type: string
          description: The current operational status of the volume.
          example: in_use
          enum:
            - creating
            - in_use
            - available
            - deleting
        volume_attachment:
          nullable: true
          $ref: '#/components/schemas/VolumeAttachment'

I have created a test that reproduces the problem, the last assert is not passing when it should IMO => https://github.com/bilby91/json_schemer/blob/8097bd3d9a497a6803c71d31cd5bf23bf7326372/test/open_api_test.rb#L1119

I've also found a typescript implementation where they are handling this "correctly" => https://github.com/acacode/swagger-typescript-api/pull/40

bilby91 commented 2 days ago

@davishmcclurg I was able to get the test passing without breaking any other test. Here is the PR => https://github.com/davishmcclurg/json_schemer/pull/200

I know that the PR might need adjustment since I'm impacting draft202012 instead of OpenAPI 3.0 but I wasn't able to figure out how to hook the type yet.