Skip to content

Validation pass when object, list or integer returned instead of string for object string property (must fail) #105

Closed
@andyceo

Description

@andyceo

Hi!

We faced with following bug. Some object's property declared as string, If this object's property returned by API is object itself, or integer, or list, no errors generated.

If API returns wrong property type for example int property, InvalidMediaTypeValue thrown (as expected).

Tested on version 0.5.0, 0.7.1, master - same results.

Expected behavior

InvalidMediaType should be thrown

Steps to reproduce

  1. Install openapi-core:

     pip3 install openapi-core
    

2.. Create and save following fatless specification as test.yml:

openapi: "3.0.1"

info:
  version: "0.1"
  title: Object instead of string
  description: Test for if returns objects instead of string

components:
  schemas:
	SomeObject:
	  type: object
	  properties:
		someprop:
		  description: Some property
		  type: string
		someint:
		  type: integer

paths:
  /getsomeobject:
	get:
	  summary: Get the SomeObject
	  operationId: getSomeObject
	  responses:
		'200':
		  description: This is SomeObject
		  content:
			application/json:
			  schema:
				$ref: '#/components/schemas/SomeObject'
  1. Create and save following script as test.py:

     #!/usr/bin/env python3
     
     # -*- coding: utf-8 -*-
     import json
     import sys
     import yaml
     from openapi_core import create_spec
     from openapi_core.shortcuts import RequestValidator, ResponseValidator
     from openapi_core.wrappers.mock import MockRequest, MockResponse
     
     
     def validate(openapi_file):
         with open(openapi_file, 'r') as myfile:
             spec_dict = yaml.safe_load(myfile)
             spec = create_spec(spec_dict)
     
             openapi_request = MockRequest('localhost', 'get', '/getsomeobject')
             validator = RequestValidator(spec)
             result = validator.validate(openapi_request)
             request_errors = result.errors
     
             # PASS (must PASS)
             data = json.dumps({
                 'someprop': 'content'
             })
     
             # PASS (must FAIL)
             data = json.dumps({
                 'someprop': {
                     'nested_object_property': 'content',
                     'nested_object_another property': 13,
                 }
             })
     
             # PASS (must FAIL)
             data = json.dumps({
                 'someprop': ['dfdfdf', 'dfdfdfsssss']
             })
     
             # PASS (must FAIL)
             data = json.dumps({
                 'someprop': 123
             })
     
             # PASS (must FAIL)
             data = json.dumps({
                 'someprop': 123
             })
     
             # FAIL (must FAIL)
             data = json.dumps({
                 'someint': 'content'
             })
     
             # FAIL (must FAIL)
             data = json.dumps({
                 'someprop': 'dsdsd',
                 'someint': 123,
                 'not_in_scheme_prop': 123
             })
     
             openapi_response = MockResponse(data)
             validator = ResponseValidator(spec)
             result = validator.validate(openapi_request, openapi_response)
             response_errors = result.errors
     
             print('Request errors: {} Response errors: {}'.format(request_errors, response_errors))
     
     
     if __name__ == "__main__":
         if len(sys.argv) < 2:
             print("Specify path to openapi.yaml file!")
             exit(1)
         else:
             validate(sys.argv[1])
    
  2. Execute script to validate spec:

     python3 test.py test.yml
    
  3. Try to comment out test payloads to see actual results.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions