openapi-generators / openapi-generators/openapi-python-client
Generation for multiple content types for a single endpoint fails when content types expect identical models
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 2k
- Forks
- 293
- Merge medio
- 34 min
- PR fusionados (30 d)
- 1
Descripción
Describe the bug
Support for multiple content types for a single endpoint was added in #453 / #822. However, the implemented fix generates non-functional code if the content types expect the same model. For instance, take this Api Spec snippet:
paths./api/dns/add/...
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/DnsCreate'
application/x-www-form-urlencoded:
schema:
$ref: '#/components/schemas/DnsCreate'
multipart/form-data:
schema:
$ref: '#/components/schemas/DnsCreate'
required: true
...
components.schemas...:
DnsCreate:
type: object
properties:
name:
type: string
dns_type:
oneOf:
- $ref: '#/components/schemas/DnsTypeEnum'
- $ref: '#/components/schemas/BlankEnum'
content:
type: string
ttl:
type: integer
maximum: 2147483647
minimum: -2147483648
nullable: true
required:
- content
- dns_type
- name
All 3 content types expect the same content type: DnsCreate. However, this generates this Python code:
def _get_kwargs(
*,
body: Union[
DnsCreate,
DnsCreate,
DnsCreate,
],
) -> dict[str, Any]:
headers: dict[str, Any] = {}
_kwargs: dict[str, Any] = {
"method": "post",
"url": "/api/dns/add/",
}
if isinstance(body, DnsCreate):
_kwargs["json"] = body.to_dict()
headers["Content-Type"] = "application/json"
if isinstance(body, DnsCreate):
_kwargs["data"] = body.to_dict()
headers["Content-Type"] = "application/x-www-form-urlencoded"
if isinstance(body, DnsCreate):
_kwargs["files"] = body.to_multipart()
headers["Content-Type"] = "multipart/form-data"
_kwargs["headers"] = headers
return _kwargs
This results in all requests made to this endpoint being treated as multipart form data, and it is impossible to make a JSON or form-urlencoded request.
Desktop (please complete the following information):
- Python Version: 3.12.3
- openapi-python-client version: 0.25.0
Additional context
While it may seem odd to support 3 different content types for the same endpoint, this specification has the advantage of making Django Rest Framework's interactive API support form uploads (instead of requiring you to manually edit JSON). While I'm sure this is a more niche problem, I think we can all agree that this is problematic code generation. While I haven't contributed to this repo, perhaps supporting an additional content type parameter is the solution (rather than relying on isinstance calls).
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Empieza reproduciendo la generación con el fragmento de OpenAPI del issue e inspecciona la función de endpoint generada _get_kwargs. Sigue cómo se asocian modelos idénticos con cada tipo de contenido de la solicitud; se considera terminado cuando las solicitudes JSON, form-urlencoded y multipart siguen siendo seleccionables en lugar de ser sobrescritas por la rama final.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- openapi, python
- Área
- api, tooling
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 45/100