cyprieng / swagger-parser

Give useful informations about your swagger files
MIT License
62 stars 59 forks source link

Object of type 'datetime' is not JSON serializable #50

Open kigsmtua opened 7 years ago

kigsmtua commented 7 years ago

unable to parse valid file with error Object of type 'datetime' is not JSON serializable

flavianh commented 7 years ago

@kigsmtua Can you give us steps to reproduce your issue?

kigsmtua commented 7 years ago

@flavianh when the swagger yaml file has a definition with the u.t.c format as I will show below is when the error actually occurs.

Steps to reproduce: Use the swagger parser to parse a valid yaml with the datetime field in the definitions as below:

"releaseDate": {
          "type": "string",
          "format": "int32",
          "example": "2016-08-29T09:12:33.001Z"
  },

My digging shows thats its because of how pyaml understands such a format. While parsing it will give an instance of the datetime object which swagger validator library seems not to understand on https://github.com/Trax-air/swagger-parser/blob/master/swagger_parser/swagger_parser.py#L59

when yaml.load is called it returns that field as below .

datetime.datetime(2016, 8, 29, 9, 12, 33, 1000)

I would glad to pull a pr if you can guide on how we can go about this

flavianh commented 7 years ago

@kigsmtua could you include the traceback as well? I'm not sure if the problem occurs at line 59 in our library or somewhere else from your account @cyprieng can you help with that?

kigsmtua commented 7 years ago

@flavianh


  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/common.py", line 17, in wrapper
    return method(*args, **kwargs)
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/validator20.py", line 125, in validate_json
    cls=Draft4Validator)
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 34, in validate
    instance_cls(schema, *args, **kwargs).validate(instance)
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 129, in validate
    for error in self.iter_errors(*args, **kwargs):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 105, in iter_errors
    for error in errors:
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 117, in validator_wrapper
    default_validator_callable,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 159, in deref_and_validate
    validator, schema_element, instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/_validators.py", line 304, in properties_draft4
    schema_path=property,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 121, in descend
    for error in self.iter_errors(instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 105, in iter_errors
    for error in errors:
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 117, in validator_wrapper
    default_validator_callable,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 159, in deref_and_validate
    validator, schema_element, instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/_validators.py", line 216, in ref
    for error in validator.descend(instance, resolved):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 121, in descend
    for error in self.iter_errors(instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 105, in iter_errors
    for error in errors:
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 117, in validator_wrapper
    default_validator_callable,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 159, in deref_and_validate
    validator, schema_element, instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/_validators.py", line 16, in patternProperties
    v, subschema, path=k, schema_path=pattern,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 121, in descend
    for error in self.iter_errors(instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 105, in iter_errors
    for error in errors:
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 117, in validator_wrapper
    default_validator_callable,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 159, in deref_and_validate
    validator, schema_element, instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/_validators.py", line 216, in ref
    for error in validator.descend(instance, resolved):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 121, in descend
    for error in self.iter_errors(instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 105, in iter_errors
    for error in errors:
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 117, in validator_wrapper
    default_validator_callable,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 159, in deref_and_validate
    validator, schema_element, instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/_validators.py", line 304, in properties_draft4
    schema_path=property,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 121, in descend
    for error in self.iter_errors(instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 105, in iter_errors
    for error in errors:
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 117, in validator_wrapper
    default_validator_callable,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 159, in deref_and_validate
    validator, schema_element, instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/_validators.py", line 216, in ref
    for error in validator.descend(instance, resolved):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 121, in descend
    for error in self.iter_errors(instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 105, in iter_errors
    for error in errors:
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 117, in validator_wrapper
    default_validator_callable,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 159, in deref_and_validate
    validator, schema_element, instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/_validators.py", line 304, in properties_draft4
    schema_path=property,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 121, in descend
    for error in self.iter_errors(instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 105, in iter_errors
    for error in errors:
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 117, in validator_wrapper
    default_validator_callable,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 159, in deref_and_validate
    validator, schema_element, instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/_validators.py", line 216, in ref
    for error in validator.descend(instance, resolved):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 121, in descend
    for error in self.iter_errors(instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 105, in iter_errors
    for error in errors:
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 117, in validator_wrapper
    default_validator_callable,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 159, in deref_and_validate
    validator, schema_element, instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/_validators.py", line 25, in additionalProperties
    extras = set(_utils.find_additional_properties(instance, schema))
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/_utils.py", line 104, in find_additional_properties
    if patterns and re.search(patterns, property):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/re.py", line 182, in search
    return _compile(pattern, flags).search(string)
TypeError: expected string or bytes-like object

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_parser/swagger_parser.py", line 70, in __init__
    validate_spec(self.specification, '')
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/validator20.py", line 81, in validate_spec
    http_handlers=http_handlers,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/common.py", line 22, in wrapper
    sys.exc_info()[2])
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/common.py", line 17, in wrapper
    return method(*args, **kwargs)
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/validator20.py", line 125, in validate_json
    cls=Draft4Validator)
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 34, in validate
    instance_cls(schema, *args, **kwargs).validate(instance)
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 129, in validate
    for error in self.iter_errors(*args, **kwargs):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 105, in iter_errors
    for error in errors:
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 117, in validator_wrapper
    default_validator_callable,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 159, in deref_and_validate
    validator, schema_element, instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/_validators.py", line 304, in properties_draft4
    schema_path=property,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 121, in descend
    for error in self.iter_errors(instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 105, in iter_errors
    for error in errors:
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 117, in validator_wrapper
    default_validator_callable,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 159, in deref_and_validate
    validator, schema_element, instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/_validators.py", line 216, in ref
    for error in validator.descend(instance, resolved):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 121, in descend
    for error in self.iter_errors(instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 105, in iter_errors
    for error in errors:
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 117, in validator_wrapper
    default_validator_callable,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 159, in deref_and_validate
    validator, schema_element, instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/_validators.py", line 16, in patternProperties
    v, subschema, path=k, schema_path=pattern,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 121, in descend
    for error in self.iter_errors(instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 105, in iter_errors
    for error in errors:
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 117, in validator_wrapper
    default_validator_callable,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 159, in deref_and_validate
    validator, schema_element, instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/_validators.py", line 216, in ref
    for error in validator.descend(instance, resolved):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 121, in descend
    for error in self.iter_errors(instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 105, in iter_errors
    for error in errors:
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 117, in validator_wrapper
    default_validator_callable,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 159, in deref_and_validate
    validator, schema_element, instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/_validators.py", line 304, in properties_draft4
    schema_path=property,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 121, in descend
    for error in self.iter_errors(instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 105, in iter_errors
    for error in errors:
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 117, in validator_wrapper
    default_validator_callable,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 159, in deref_and_validate
    validator, schema_element, instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/_validators.py", line 216, in ref
    for error in validator.descend(instance, resolved):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 121, in descend
    for error in self.iter_errors(instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 105, in iter_errors
    for error in errors:
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 117, in validator_wrapper
    default_validator_callable,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 159, in deref_and_validate
    validator, schema_element, instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/_validators.py", line 304, in properties_draft4
    schema_path=property,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 121, in descend
    for error in self.iter_errors(instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 105, in iter_errors
    for error in errors:
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 117, in validator_wrapper
    default_validator_callable,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 159, in deref_and_validate
    validator, schema_element, instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/_validators.py", line 216, in ref
    for error in validator.descend(instance, resolved):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 121, in descend
    for error in self.iter_errors(instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/validators.py", line 105, in iter_errors
    for error in errors:
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 117, in validator_wrapper
    default_validator_callable,
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_spec_validator/ref_validators.py", line 159, in deref_and_validate
    validator, schema_element, instance, schema):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/_validators.py", line 25, in additionalProperties
    extras = set(_utils.find_additional_properties(instance, schema))
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/jsonschema/_utils.py", line 104, in find_additional_properties
    if patterns and re.search(patterns, property):
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/re.py", line 182, in search
    return _compile(pattern, flags).search(string)
swagger_spec_validator.common.SwaggerValidationError: expected string or bytes-like object

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    parser = SwaggerParser(swagger_path='sample.yaml')
  File "/Users/johnmutua/Projects/swagger/lib/python3.6/site-packages/swagger_parser/swagger_parser.py", line 72, in __init__
    raise ValueError('{0} is not a valid swagger2.0 file: {1}'.format(swagger_path,  e))
ValueError: sample.yaml is not a valid swagger2.0 file: expected string or bytes-like object```
kigsmtua commented 7 years ago

@flavianh swagger file https://app.swaggerhub.com/apis/kigsmtua/housewives/1.0.0 default swagger file on swagger hub

kigsmtua commented 7 years ago

@flavianh running code

     from swagger_parser import SwaggerParser
     try:
        parser = SwaggerParser(swagger_path='sample.yaml')
     except Exception as e:
        print(str(e))
cyprieng commented 7 years ago

I will try to reproduce and fix your issue, thanks for reporting this

robertlagrant commented 6 years ago

I've got the same issue. I can easily push a sample repo as I'm VERY early days into a project and this is the first thing I did :-)