Skip to content

[Bug]: The string in integer format cannot be validated by the "type integer" rule. #1017

@NieXiaoyi

Description

@NieXiaoyi

Actual Behavior

the spec:

components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: string
          format: uuid
        username:
          type: string
        age:
          type: integer
      required:
        - id
        - username
        - age

the body of response:

{
    "id": "123e4567-e89b-12d3-a456-426614174000",
    "username": "Test User",
    "age": "30"
}

the verification result:
pass

Expected Behavior

Get an exception InvalidData, <ValidationError: "'30' is not of type 'integer'">, just like "openapi-core 0.18.2"

Steps to Reproduce

the spec:

openapi: 3.0.3
info:
  title: User Information API
  version: 1.0.0
servers:
  - url: http://localhost:8000/api
    description: Development Server
paths:
  /users/{userId}:
    get:
      summary: Get User Information
      description: Get user details by user ID
      parameters:
        - name: userId
          required: true
          in: path
          description: User ID
          schema:
            type: string
            format: uuid
      responses:
        '200':
          description: Successfully retrieved user information
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/User'
        '404':
          description: User not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: string
          format: uuid
        username:
          type: string
        age:
          type: integer
      required:
        - id
        - username
        - age
    Error:
      type: object
      properties:
        code:
          type: string
          description: Error code
        message:
          type: string
          description: Error message
      required:
        - code
        - message

usecase:

import pytest

from openapi_core import Spec
from openapi_core.validation.request import V30RequestValidator
from openapi_core.validation.response import V30ResponseValidator
from openapi_core.exceptions import OpenAPIError
from openapi_core.contrib.requests import RequestsOpenAPIRequest, RequestsOpenAPIResponse
from requests import Request, Response


base_api_url = 'http://localhost:8000/api'
openapi_spec = Spec.from_file_path('openapi/user_copy.yml')
request_validator = V30RequestValidator(openapi_spec)
response_validator = V30ResponseValidator(openapi_spec)


class TestGetUser:
    def test_invalid_type_of_age(self):
        """Test validation fails with invalid type of age"""
        url = f'{base_api_url}/users/123e4567-e89b-12d3-a456-426614174000'
        request = Request('GET', url)
        openapi_request = RequestsOpenAPIRequest(request)

        response = Response()
        response.status_code = 200
        # the type of age is string, not integer
        response._content = b'{"id": "123e4567-e89b-12d3-a456-426614174000", "username": "Test User", "age": "30"}'
        response.headers = {'Content-Type': 'application/json'}
        openapi_response = RequestsOpenAPIResponse(response)
        # Validate response against OpenAPI spec
        with pytest.raises(OpenAPIError):
            response_validator.validate(openapi_request, openapi_response)

OpenAPI Core Version

0.19.5

OpenAPI Core Integration

requests

Affected Area(s)

No response

References

No response

Anything else we need to know?

No response

Would you like to implement a fix?

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugIndicates an issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions