Skip to content

$ref schemas ignore other constraints #281

@janrueth

Description

@janrueth

I was test driving the library using the pet-store example on swagger.io:
https://petstore31.swagger.io/api/v31/openapi.json

This contains things like:

        "requestBody" : {
          "description" : "Pet object that needs to be updated in the store",
          "content" : {
            "application/json" : {
              "schema" : {
                "$ref" : "#/components/schemas/Pet",
                "description" : "A Pet in JSON Format",
                "required" : [ "id" ],
                "writeOnly" : true
              }
            },

"Pet" : {
        "$schema" : "https://json-schema.org/draft/2020-12/schema",
        "description" : "Pet",
        "properties" : {
          "id" : {
            "type" : "integer",
            "format" : "int64"
          },
          "category" : {
            "$ref" : "#/components/schemas/Category",
            "description" : "Pet Category"
          },
          "name" : {
            "type" : "string",
            "examples" : [ "doggie" ]
          },
          "photoUrls" : {
            "type" : "array",
            "items" : {
              "type" : "string",
              "xml" : {
                "name" : "photoUrl"
              }
            },
            "xml" : {
              "wrapped" : true
            }
          },
          "tags" : {
            "type" : "array",
            "items" : {
              "$ref" : "#/components/schemas/Tag"
            },
            "xml" : {
              "wrapped" : true
            }
          },
          "status" : {
            "type" : "string",
            "description" : "pet status in the store",
            "enum" : [ "available", "pending", "sold" ]
          },
          "availableInstances" : {
            "type" : "integer",
            "format" : "int32",
            "examples" : [ "7" ],
            "exclusiveMaximum" : 10,
            "exclusiveMinimum" : 1,
            "swagger-extension" : true
          },
          "petDetailsId" : {
            "type" : "integer",
            "format" : "int64",
            "$ref" : "/api/v31/components/schemas/petdetails#pet_details_id"
          },
          "petDetails" : {
            "$ref" : "/api/v31/components/schemas/petdetails"
          }
        },
        "required" : [ "name", "photoUrls" ],
        "xml" : {
          "name" : "Pet"
        }
      },

I initially thought, that the schema is buggy, since it defined $ref and other constraints/fields in the schema object which in OpenAPI 3.0 was not allowed.

However, this is now allowed in OpenAPI 3.1 as it is allowed in the underlying json schema spec.

So for example, for the Pet in the requestBody above, [ "name", "photoUrls" ] and [ "id" ] fields need to be present.

When this crate resolves references it just takes the reference and ignores all other fields ( here ignores that id is also required).

I think it would need to merge constraints selecting the more strict constraint and overwrite "informational" fields such as description

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions