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

Rest API does not take into account some arguments when submitting #31

Open
GuilloteauQ opened this issue Apr 14, 2023 · 2 comments
Open

Comments

@GuilloteauQ
Copy link
Collaborator

GuilloteauQ commented Apr 14, 2023

When using the Rest API i noticed that some arguments in my json were not taking into account for the submission.

here is an example of request i used:

curl --header "Content-Type: application/json" \
       --header "X_REMOTE_IDENT: user1"\
       --request POST \
       -v \
       --data '{"resource": ["/resource_id=3,walltime=00:10:00"],"command": "sleep 1m"}' \
       http://oar-server:80/oarapi-unsecure/jobs

In this case, OAR will submit a job that will sleep for 1 minute, but the resources used will be the default values (one resource_id and 1 hour walltime for my config).

The problem seems to come from the management of the types in flask.
Fields that uses the defaults values event when passed arguments in the requests are the ones having the following argument:

Arg([str])

with Arg coming from

class Arg(object):

Here are some examples:

I am not sure what is going on in this Arg class, so i cannot really propose a solution.

@GuilloteauQ GuilloteauQ changed the title Rest API do not take into account some arguments when submitting Rest API does not take into account some arguments when submitting Apr 14, 2023
@GuilloteauQ
Copy link
Collaborator Author

The following diff fixed my issue:

diff --git a/oar/rest_api/utils.py b/oar/rest_api/utils.py
index 0fc292f..feac236 100644
--- a/oar/rest_api/utils.py
+++ b/oar/rest_api/utils.py
@@ -7,7 +7,6 @@ from flask import abort, current_app, request
 from oar.lib import config
 from oar.lib.utils import integer_types, reraise, to_unicode

-
 class WSGIProxyFix(object):
     def __init__(self, app):
         self.app = app
@@ -69,7 +68,7 @@ class Arg(object):
         self.required = required
         self.dest = dest
         self.error = error
-        self.locations = locations or self.DEFAULT_LOCATIONS
+        self.locations = locations if locations else self.DEFAULT_LOCATIONS

     def raw_value(self, value):
         if value is not None:
@@ -111,7 +110,7 @@ class ArgParser(object):
         self.argmap = argmap

     def get_value(self, data, name, argobj):
-        if isinstance(argobj.type, ListArg) and not argobj.type.sep:
+        if isinstance(argobj.type, ListArg) and not argobj.type.sep and not isinstance(data, dict):
             return data.getlist(name)
         else:
             return data.get(name, self.MISSING)
@@ -119,6 +118,7 @@ class ArgParser(object):
     def parse_arg(self, argname, argobj):
         """Pull a form value from the request."""
         for location in argobj.locations:
+
             value = self.MISSING
             if location == "querystring" and request.args:
                 value = self.get_value(request.args, argname, argobj)
@@ -136,7 +136,7 @@ class ArgParser(object):

     def convert_bool(self, value):
         """Try to convert ``value`` to a Boolean."""
-        if value.lower() in ("True", "yes", "1"):
+        if value.lower() in ("true", "yes", "1"):
             return True
         if value.lower() in ("false", "no", "0"):
             return False
diff --git a/oar/rest_api/views/job.py b/oar/rest_api/views/job.py
index bb536f3..4a15eda 100644
--- a/oar/rest_api/views/job.py
+++ b/oar/rest_api/views/job.py
@@ -79,7 +79,7 @@ def index(
         "reservation": Arg(str),
         "checkpoint": Arg(int, default=0),
         "signal": Arg(int),
-        "type": Arg([str], dest="types"),
+        "type": Arg([str], dest="types", locations=("json",)),
         "directory": Arg(str),
         "project": Arg(str),
         "name": Arg(str),
---

The issue seems to be linked to the locations.
The type argument is Arg([str]).
When being parsed, this argument is first evaluated with the querystring, then form locations

DEFAULT_LOCATIONS = ("querystring", "form", "json")

The form location returns the empty list ([]), while it is actually not empty.
Forcing the location to be json fixed the issue.

@adfaure
Copy link
Contributor

adfaure commented Apr 18, 2023

Can you create a pull request ? Or push it to oar if the tests pass ?
Thanks for investigating !

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

2 participants