Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: '_io.TextIOWrapper' object has no attribute 'get' #384

Closed
Gujjar95 opened this issue Dec 24, 2017 · 7 comments
Closed

AttributeError: '_io.TextIOWrapper' object has no attribute 'get' #384

Gujjar95 opened this issue Dec 24, 2017 · 7 comments

Comments

@Gujjar95
Copy link

Gujjar95 commented Dec 24, 2017

json schema error AttributeError: '_io.TextIOWrapper' object has no attribute 'get'
My files

{
	"sender" : 6
	"receiver" : 4,
	"transaction_type" : "same",
	"transaction_amount" : "10",
	"transaction_status" : "pending",
	"transaction_receiver_address":"",
	"transaction_sender_address" : "",
	"transaction_currency_involved" : "Usd"
} ```
Below is my json schema.

``` {
 "definitions": {},
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "http://example.com/example.json",
  "type": "object",
  "properties": {
    "sender": {
      "id": "/properties/sender",
      "type": "integer",
      "title": "The Sender Schema.",
      "description": "An explanation about the purpose of this instance.",
      "default": 0
    },
    "receiver": {
      "id": "/properties/receiver",
      "type": "integer",
      "title": "The Receiver Schema.",
      "description": "An explanation about the purpose of this instance.",
      "default": 0
    },
    "transaction_type": {
      "id": "/properties/transaction_type",
      "type": "string",
      "title": "The Transaction_type Schema.",
      "description": "An explanation about the purpose of this instance.",
      "default": ""
    },
    "transaction_amount": {
      "id": "/properties/transaction_amount",
      "type": "string",
      "title": "The Transaction_amount Schema.",
      "description": "An explanation about the purpose of this instance.",
      "default": ""
    },
    "transaction_status": {
      "id": "/properties/transaction_status",
      "type": "string",
      "title": "The Transaction_status Schema.",
      "description": "An explanation about the purpose of this instance.",
      "default": ""
    },
    "transaction_receiver_address": {
      "id": "/properties/transaction_receiver_address",
      "type": "string",
      "title": "The Transaction_receiver_address Schema.",
      "description": "An explanation about the purpose of this instance.",
      "default": ""
    },
    "transaction_sender_address": {
      "id": "/properties/transaction_sender_address",
      "type": "string",
      "title": "The Transaction_sender_address Schema.",
      "description": "An explanation about the purpose of this instance.",
      "default": ""
    },
    "transaction_currency_involved": {
      "id": "/properties/transaction_currency_involved",
      "type": "string",
      "title": "The Transaction_currency_involved Schema.",
      "description": "An explanation about the purpose of this instance.",
      "default": ""
    }
  }
} ```
@Julian
Copy link
Member

Julian commented Dec 24, 2017

Hi. Please include full reproducible code + traceback.

@Gujjar95
Copy link
Author

Below is my Code, Traceback and schema and instances i have posted above

def post(self, request, id):
    	received_json_data = json.loads(request.body.decode("utf-8"))
    	validate(instance = received_json_data, schema = schema)
    	sender = User.objects.filter(pk=int(received_json_data['sender'])).first()
    	receiver = User.objects.filter(pk=int(received_json_data['receiver'])).first()
    	transaction_type = received_json_data['transaction_type']
    	transaction_status = received_json_data['transaction_status']
    	transaction_amount = int(received_json_data['transaction_amount'])
    	transaction_receiver_address = received_json_data['transaction_receiver_address']
    	transaction_sender_address = received_json_data['transaction_sender_address']
    	transaction_currency_involved = received_json_data['transaction_currency_involved']
    	if sender is not None:
    		temp_sender = WalletBalance.objects.get(wallet=Wallet.objects.filter(user=sender),wallet_currency=transaction_currency_involved)
    		if temp_sender.currency_address != transaction_sender_address:
    			return HttpResponse("Sender address does not matches")
    		if transaction_amount > temp_sender.balance:
    			return HttpResponse("Insufficient balance")
    	if receiver is not None:
    		temp_receiver = WalletBalance.objects.get(wallet=Wallet.objects.filter(user=receiver),wallet_currency=transaction_currency_involved).currency_address
    		if temp_receiver != transaction_receiver_address:
    			return HttpResponse("Receiver address does not matches")
    	transaction_new = Transaction(sender=sender, receiver=receiver, transaction_type=transaction_type,transaction_status=transaction_status, transaction_amount=transaction_amount,transaction_sender_address=transaction_sender_address,transaction_receiver_address=transaction_receiver_address,transaction_currency_involved=transaction_currency_involved)
    	transaction_new.save()
    	return HttpResponse("OK")

Trace back

Traceback (most recent call last):
  File "/home/gujjar/start1/venv/lib/python3.5/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/home/gujjar/start1/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/gujjar/start1/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/gujjar/start1/venv/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/gujjar/start1/venv/lib/python3.5/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/gujjar/tempry/check/apis/views.py", line 166, in post
    validate(instance = received_json_data, schema = schema)
  File "/home/gujjar/start1/venv/lib/python3.5/site-packages/jsonschema/validators.py", line 539, in validate
    cls = validator_for(schema)
  File "/home/gujjar/start1/venv/lib/python3.5/site-packages/jsonschema/validators.py", line 484, in validator_for
    return meta_schemas.get(schema.get(u"$schema", u""), default)
AttributeError: '_io.TextIOWrapper' object has no attribute 'get'

@Julian
Copy link
Member

Julian commented Dec 24, 2017

@Gujjar95 thanks, but I can't reproduce with that.

By reproducible -- I need code I can run that is self contained.

Your issue is going to be somewhere you passing in a TextWrapper, but in order to see where, I need the above.

@Gujjar95
Copy link
Author

Gujjar95 commented Dec 24, 2017

@Julian
Sorry for the above, I am new to the community and learning all terminology.
Above method is an API call for post in which body contain JSON.Without validating function everything is working fine.So pb is arising after adding the validate.in the validate I am calling instance and schema which I posted in the first comment.
If you need full code then it is too many files to post here.

@Julian
Copy link
Member

Julian commented Dec 24, 2017

No worries, but without a fully reproducible set of code I can't really help -- you need to work on stripping away all the parts of your code that are unrelated and provide a minimal, self contained example.

I can tell from your traceback that you must be doing something like passing in open(someschemafile) rather than the actual contents of the file, but when filing issues you need to do a bit of work upfront to reduce down to something that you can share here if you can't find your issue yourself.

Hopefully the above is enough to point you in the right direction.

@Gujjar95
Copy link
Author

yup you are right got it now ,

__location__ = os.path.realpath(
    os.path.join(os.getcwd(), os.path.dirname('views.py')))
schema = open(os.path.join(__location__,'apis/json_schema.json'))

@catb0t
Copy link

catb0t commented Dec 24, 2017

https://stackoverflow.com/help/mcve

Julian added a commit that referenced this issue Jun 9, 2020
817b724 add perl implementation and test suite to the user list
ca14e01 Merge branch 'pull/382'
3dabf55 move non-format tests out of the format directory, while keeping all ECMA regex tests together
4121aa5 move format tests to their own directory
4bae8aa Add more idn-hostname tests to draft-2019-09 and draft-07
6d91158 [325] Add some more hostname tests
e593057 Merge pull request #389 from ssilverman/readme-drafts
fb3766d README: Improve issue/PR links
79bef22 README: Update language around drafts
ade47e4 README: Add Snow to the list of supporting Java validators
fc0c14e README: Update simple JSON example
1167669 README: Update structure, consistency, spacing, and wrapping
9514122 Merge pull request #388 from json-schema-org/ether/maxProperties=0
7646490 test that maxProperties = 0 means the object is empty
c3f4319 Merge pull request #384 from ChALkeR/chalker/unique
7766f48 Improve uniqueItems validation
7555d41 Add unnormalized $id tests
11f70eb [300] Add tests for valid use of empty fragments in "$id"
b106ff0 [299] Add tests for invalid use of fragments in "$id"
4a74d45 Fix "tilde" spelling
3eca41b Merge pull request #379 from json-schema-org/ether/remove-wrapped-refs
d61bae8 remove wrapped refs
536ec07 [359] Add unevaluatedProperties/unevaluatedItems cousin tests
ac63eb7 Small README update that introduces the concept of directories
697944e Merge pull request #374 from karenetheridge/ether/allOf-anyOf-oneOf
33f8549 test all the *Of keywords together
44b99ed Merge pull request #373 from karenetheridge/ether/items-and-contains
4a2b52f some tests of items + contains
7f00cc8 add test that was present for other drafts but not 2019-09
a3f9e2e Merge pull request #371 from karenetheridge/ether/if-then-else-boolean
aeeaecc some tests with if/then/else and boolean schemas
b8a083c Merge pull request #372 from nomnoms12/unique-false-and-zero
85728f1 Add tests for uniqueness [1] and [true]
fd01a60 Add tests for uniqueness [1] and [true]
0a8823c Merge pull request #370 from karenetheridge/ether/nul-char
fa6f4dd add tests for the NUL character in strings
8bf2f7f Merge pull request #369 from ssilverman/data-desc
2ba7a76 Add data description
4f66078 Merge pull request #367 from karenetheridge/ether/additionalItems
283da7c some more tests for additionalItems
7ba95f3 add tests for keywords maxContains, minContains
2f2e7cf Merge pull request #365 from karenetheridge/ether/move-ecma-regex
8388f27 move ECMA regex tests under format/

git-subtree-dir: json
git-subtree-split: 817b724b7a64d7c18a8232aa32b5f1cc1d6dd153
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants