OpenAPI.NET icon indicating copy to clipboard operation
OpenAPI.NET copied to clipboard

Validation: false-positive schema validation error for long type

Open AlexMihalev opened this issue 4 years ago • 2 comments

For example validation for this schema:

var schema = new OpenApiSchema()
{
    Example = new OpenApiInteger(55),
    Type = "integer",
    Format = "int64"
};

will bring "SchemaMismatchedDataType" which looks fine.

But in the case when schema loaded from the JSON we can have exactly this situation.

Because loading proper data type requires schema but schema for field example can be expressed as a reference and resolving referenced schemas was done after loading the whole document, which means in this case proper primitive type can't be deduced when it should be, according to the current implementation.

Here full schema example:

{
  "openapi": "3.0.1",
  "info": { "title": "api", "version": "v2" },
  "paths": {
    "/match/document/by-query": {
      "get": {
        "responses": {
          "200": {
            "description": "Ok",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Reply"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Bucket": {
        "required": [ "key" ],
        "type": "object",
        "properties": {
          "count": { "type": "integer", "format": "int64" },
          "key": { "type": "string" }
        },
        "additionalProperties": false
      },
      "Facet": {
        "required": [ "field" ],
        "type": "object",
        "properties": {
          "field": {  "type": "string", "example": "title" },
          "buckets": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/Bucket" },
            "nullable": true
          }
        },
        "additionalProperties": false
      },
      "Reply": {
        "required": [ "total" ],
        "type": "object",
        "properties": {
          "total": { "type": "integer", "format": "int32", "example": 42 },
          "facets": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/Facet" },
            "nullable": true,
            "example": [
              {
                "field": "name",
                "buckets": [
                  {
                    "count": 1,  // <-- this value loaded as OpenApiInteger but should be as OpenApiLong
                    "key": "2020-22"
                  }
                ]
              }
            ]
          }
        },
        "additionalProperties": false
      }
    }
  }
}
// as result: 
// {Data and type mismatch found. [#/components/schemas/Reply/properties/facets/example/0/buckets/0/count]}

I think that just slightly relaxing schema validation for example here will fix this issue

AlexMihalev avatar Nov 03 '21 19:11 AlexMihalev

This validation was supposed to be removed in 1.2.0 https://github.com/microsoft/OpenAPI.NET/issues/429#issuecomment-587182584 Can you confirm which version of the library you are using?

darrelmiller avatar Nov 23 '21 14:11 darrelmiller

I am working with 1.2.1. Example of type integer in a schema works fine. In components/examples and components/headers it results as a string. I tried to load the API of GitHub (https://github.com/github/rest-api-description/tree/main/descriptions/ghes-2.18). It results in 147 diagnostic errors.

Extractions of the yaml file:

components:
  examples:
    global-hook-items:
      value:
      - type: Global
        id: 1 # <---------------------------------------------
        name: web
        active: true
        events:
        - organization
        - user
        config:
          url: https://example.com
          content_type: json
          insecure_ssl: '0'
          secret: "********"
        updated_at: '2017-12-07T00:14:59Z'
        created_at: '2017-12-07T00:14:59Z'
        url: https://api.github.com/admin/hooks/1
        ping_url: https://api.github.com/admin/hooks/1/pings

components:
  headers:
    x-rate-limit-limit:
      example: 5000 # <-------------------
      schema:
        type: integer

richardkaspar avatar Feb 03 '22 08:02 richardkaspar