question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] Cannot specify encoding style for object and array elements

See original GitHub issue

Describe the bug OpenAPI defines a number of encoding styles for complex types like arrays and objects, and specifically reccomends using them for things like form data encoding. While the underlying OpenAPI models for parameters appear to support this, using them does not affect the resulting OpenAPI definition.

from fastapi import FastAPI, Form, Query
from pydantic import BaseModel
from typing import List

api = FastAPI()

@api.post('/list_form.json')
def test_list_form(
	id: int = Form(...),
	list: List[str] = Form(..., style = 'spaceDelimited', explode=False)
):
	pass

@api.get('/list_query.json')
def test_list_query(
	id: int = Query(...),
	list: List[str] = Query(..., style = 'spaceDelimited', explode=False)
):
	pass

class ObjectType(BaseModel):
	foo: int
	bar: int
	baz: str

@api.post('/obj_form.json')
def test_obj_form(
	id: int = Form(...),
	obj: ObjectType = Form(..., style = 'deepObject', explode=True)
):
	pass

This also prevents more complex object encodings from being used for query parameters, which some API standards may require.

from fastapi import FastAPI, Form, Query
from pydantic import BaseModel
from typing import List

api = FastAPI()

class ObjectType(BaseModel):
	foo: int
	bar: int
	baz: str


@api.post('/obj_form.json')
def test_obj_form(
	id: int = Form(...),
	obj: ObjectType = Form(..., style = 'deepObject', explode=True)
):
	pass
  File ".pyenv/lib/python3.7/site-packages/fastapi/dependencies/utils.py", line 187, in get_dependant
    ), f"Param: {param_field.name} can only be a request body, using Body(...)"
AssertionError: Param: obj can only be a request body, using Body(...)

Expected behavior All parameter types provided by FastAPI should support parameter encoding and schema encoding where applicable, as specified by the OpenAPI specification. If consistency is to be taken into account, adding keywords parameter for style, explode and allowReserved would probably be ideal. An alternative syntax for path variables and possibly query variables has been proposed before (see #268), but concerns have already been raised about the ergonomics of this solution, not to mention that it would not cover the other parameter types and having both solutions could prove to be a source of bugs and confusion for everyone.

Environment:

  • OS: Linux
  • FastAPI Version : 0.27
  • Python version: 3.7

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
sm-Fifteencommented, Oct 27, 2019

@tiangolo: I understand, it really is a very niche corner case. I was just attempting to use FastAPI to replicate existing APIs, and that came up as an unsupported feature that could have proved useful. If you’re designing your own API from scratch, though, I would agree that the usefulness of this would be pretty limited. I’ll be sure to look into custom routers and Query parameters.

Thank you.

1reaction
tiangolocommented, Oct 26, 2019

I’ll close this issue now as I think this would be more or less a corner case, with quite some extra complexity.

But if you still need to implement some sophisticate extra features like these for your use case, it’s now possible to subclass and override the APIRouter, and then you could also subclass Query, Form, etc. That way you can extend everything to suit your needs.

https://fastapi.tiangolo.com/tutorial/custom-request-and-route/

Read more comments on GitHub >

github_iconTop Results From Across the Web

Swift JSONDecode decoding arrays fails if single element ...
The problem is that when iterating over a container, the container.currentIndex isn't incremented so you can try to decode again with a ...
Read more >
Simple Object Access Protocol (SOAP) 1.1 - W3C
The SOAP encodingStyle global attribute can be used to indicate the serialization rules used in a SOAP message. This attribute MAY appear on...
Read more >
Protocol Buffer Basics: Java - Google Developers
You can invent an ad-hoc way to encode the data items into a single string – such as encoding 4 ints as "12:3:-23:67"....
Read more >
Grammar and types - JavaScript - MDN Web Docs - Mozilla
This chapter discusses JavaScript's basic grammar, variable declarations, data types and literals.
Read more >
Java String Encoding - Javatpoint
Encoding is a way to convert data from one format to another. String objects use UTF-16 encoding. The problem with UTF-16 is that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found