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?

Luca P.
  • 1,021
  • 8
  • 20
  • From the code, it seems that the filters are actually sent in the request body (i.e. something similar to `curl -d 'filters=[{"prop":"is_operator","operator":"equal","value":"true"}]'`) rather than the query string. Can you confirm if this is the case? – Helen Sep 01 '22 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 – Luca P. Sep 01 '22 at 20:02
  • `axios({ ` `method: 'post',` `url: "/search",` `data: myFormData,` `headers: {'Content-Type': 'multipart/form-data' }` `})` `.then(res => {` – Luca P. Sep 01 '22 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. – Luca P. Sep 04 '22 at 19:26

0 Answers0