onshape-public / onshape-clients

All Onshape clients for interacting with the Onshape API.
MIT License
34 stars 29 forks source link

part_studios_api.eval_feature_script 400s with JSON error when using queries #82

Open capfish opened 2 years ago

capfish commented 2 years ago

Whenever I try to add queries to an eval_feature_script API call I get a 400 with a message that says "Error processing json"

from onshape_client import OnshapeElement
from onshape_client import Client
from onshape_client.oas import BTFeatureScriptEvalCall2377

onshape_client = Client.get_client()
element = OnshapeElement('https://cad.onshape.com/documents/b8397145a0c13529d3a18476/w/14ca07c660d598d66ebc7883/e/60dd6866d7205557ecadcb5e')
script = 'function(context is Context, queries is map) { return "hi"; }'
queries = {'some_prop': ['some_value']}
script_call = BTFeatureScriptEvalCall2377(script=script, queries=queries)
onshape_client.part_studios_api.eval_feature_script(element.did, element.wvm, element.wvmid, element.eid, bt_feature_script_eval_call_2377=script_call, _preload_content=False)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\cappie\.virtualenvs\wonderbread-gnvLMd7J\lib\site-packages\onshape_client\oas\api\part_studios_api.py", line 3009, in __call__
    return self.callable(self, *args, **kwargs)
  File "C:\Users\cappie\.virtualenvs\wonderbread-gnvLMd7J\lib\site-packages\onshape_client\oas\api\part_studios_api.py", line 691, in __eval_feature_script
    return self.call_with_http_info(**kwargs)
  File "C:\Users\cappie\.virtualenvs\wonderbread-gnvLMd7J\lib\site-packages\onshape_client\oas\api\part_studios_api.py", line 3066, in call_with_http_info
    return self.api_client.call_api(
  File "C:\Users\cappie\.virtualenvs\wonderbread-gnvLMd7J\lib\site-packages\onshape_client\oas\api_client.py", line 367, in call_api
    return self.__call_api(
  File "C:\Users\cappie\.virtualenvs\wonderbread-gnvLMd7J\lib\site-packages\onshape_client\oas\api_client.py", line 184, in __call_api
    response_data = self.request(
  File "C:\Users\cappie\.virtualenvs\wonderbread-gnvLMd7J\lib\site-packages\onshape_client\oas\api_client.py", line 447, in request
    return self.rest_client.POST(
  File "C:\Users\cappie\.virtualenvs\wonderbread-gnvLMd7J\lib\site-packages\onshape_client\oas\rest.py", line 400, in POST
    return self.request(
  File "C:\Users\cappie\.virtualenvs\wonderbread-gnvLMd7J\lib\site-packages\onshape_client\oas\rest.py", line 312, in request
    raise ApiException(http_resp=r)
onshape_client.oas.exceptions.ApiException: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Date': 'Fri, 22 Oct 2021 00:25:30 GMT', 'Content-Type': 'application/json;charset=utf-8', 'Content-Length': '95', 'Connection': 'keep-alive', 'On-Request-Id': 'a383979b63881b38b9416dca', 'On-Version': '1.137.27924.4a0cfb40dd99', 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains', 'X-Content-Type-Options': 'nosniff', 'X-XSS-Protection': '1; mode=block'})
HTTP response body: b'{\n  "message" : "Error processing json",\n  "moreInfoUrl" : "",\n  "status" : 400,\n  "code" : 0\n}'

Calling it without queries works just fine.

import json
from pprint import pprint
from onshape_client import OnshapeElement
from onshape_client import Client
from onshape_client.oas import BTFeatureScriptEvalCall2377

onshape_client = Client.get_client()
element = OnshapeElement('https://cad.onshape.com/documents/b8397145a0c13529d3a18476/w/14ca07c660d598d66ebc7883/e/60dd6866d7205557ecadcb5e')
script = 'function(context is Context, queries is map) { return "hi"; }'
script_call = BTFeatureScriptEvalCall2377(script=script)

pprint(json.loads(onshape_client.part_studios_api.eval_feature_script(element.did, element.wvm, element.wvmid, element.eid, bt_feature_script_eval_call_2377=script_call, _preload_content=False).data.decode('UTF-8')))

# Returns the following which is expected
{'console': '',
 'libraryVersion': 1576,
 'microversionSkew': False,
 'notices': [],
 'rejectMicroversionSkew': False,
 'result': {'message': {'typeTag': '', 'value': 'hi'},
            'type': 1422,
            'typeName': 'BTFSValueString'},
 'serializationVersion': '1.1.22',
 'sourceMicroversion': 'a078a9aacc9f85768e006c30'}

I tried initializing the class with other attributes like library_version and that worked as expected. I tried turning off the OpenAPI type checking and passed a dictionary directly which shows the same behavior of working correctly until I include a queries key. I added a print statement on this line to inspect the JSON https://github.com/onshape-public/onshape-clients/blob/20843a00c628e516e7219e17a23ec4ef2bf9f16f/python/onshape_client/oas/rest.py#L188

And it printed valid JSON {"script": "function(context is Context, queries is map) { return \"hi\"; }", "queries": {"some_prop": ["some_value"]}}

Am I doing something wrong? Or is there a bug in the server side JSON parsing?

Thanks!