Skip to content

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

Description

@NieXiaoyi

Actual Behavior

the spec:

components:
schemas:
User:
type: objectproperties:
id:
type: stringformat: uuidusername:
type: stringage:
type: integerrequired:
- 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.3info:
title: User Information APIversion: 1.0.0servers:
- url: http://localhost:8000/apidescription: Development Serverpaths:
/users/{userId}:
get:
summary: Get User Informationdescription: Get user details by user IDparameters:
- name: userIdrequired: truein: pathdescription: User IDschema:
type: stringformat: uuidresponses:
'200':
description: Successfully retrieved user informationcontent:
application/json:
schema:
$ref: '#/components/schemas/User''404':
description: User not foundcontent:
application/json:
schema:
$ref: '#/components/schemas/Error'components:
schemas:
User:
type: objectproperties:
id:
type: stringformat: uuidusername:
type: stringage:
type: integerrequired:
- id
- username
- ageError:
type: objectproperties:
code:
type: stringdescription: Error codemessage:
type: stringdescription: Error messagerequired:
- code
- message

usecase:

importpytestfromopenapi_coreimportSpecfromopenapi_core.validation.requestimportV30RequestValidatorfromopenapi_core.validation.responseimportV30ResponseValidatorfromopenapi_core.exceptionsimportOpenAPIErrorfromopenapi_core.contrib.requestsimportRequestsOpenAPIRequest, RequestsOpenAPIResponsefromrequestsimportRequest, Responsebase_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)
classTestGetUser:
deftest_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 integerresponse._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 specwithpytest.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