0

I am retrofitting an existing hand-crafted REST API (in Python Flask) to leverage OpenAPI 3.0 and I am a bit at a loss about what to do here. My endpoint will take an arbitrary list of filters:

[{"prop":"is_automatic", "operator":"equal", "value":"true"},
   :
 {"prop":"brand", "operator":"equal", "value":"Sumsang"}]

and use them to query a database. As far as my current implementation goes, the client does this:

  var myFormData = new FormData();      

  this.filters.forEach((filter) => {

      allFilters.push({
            prop: filter.prop,
            operator: filter.operator,
            value: filter.value
            })
  })
  myFormData.append("filters",JSON.stringify(allFilters));

and the server accepts and decodes that data with:

filtersJson = request.form.get('filters')
  :
filters = json.loads(filtersJson)

I understand this is not particularly elegant, but it gets the job done. Now my problem. I am trying to do things the OpenAPI 3.0 way and I can't seem to find a good way. Whatever I try, I keep bumping my head against deepObject and "the behavior for nested objects and arrays is undefined". Btw: This SO answer appears to be the closest to what I am asking, but it does not quite dissipate the doubt that I may be missing something.

Am I missing something? Is there an elegant OpenAPI way to implement what already works for me today? Is "packaging all my filters into a string that happens to be JSON" frowned upon by people with a lot of experience in writing Swagger/OpenAPI APIs?

4
  • From the code, it seems that the filters are actually sent in the request body (i.e. something similar to curl <url> -d 'filters=[{"prop":"is_operator","operator":"equal","value":"true"}]') rather than the query string. Can you confirm if this is the case? Commented Sep 1, 2022 at 14:27
  • Yes, they were sent in the body, but I could as well have sent them as query string, as my hand-made endpoint supports both methods. Apologies if this aspect is not clear. Anyway, the question is still the same: is there a better way to implement this that does not require encoding/decoding a "JSON string"? Thanks Commented Sep 1, 2022 at 20:02
  • axios({ method: 'post', url: "/search", data: myFormData, headers: {'Content-Type': 'multipart/form-data' } }) .then(res => { Commented Sep 1, 2022 at 20:12
  • So, I guess the question is: how do I use Openapi 3.0 to model this parameter in the requestBody: filters=[{"prop":"is_operator","operator":"equal","value":"true"}]? I spent a good chunk of my Sunday on this and frustration is all that I encountered. Really close to making it a string and screw it. Commented Sep 4, 2022 at 19:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.