tortoise / tortoise/tortoise-orm

`pydantic_model_creator` should support JSONField annotation (type hints)

Open
#1,702 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pydantic
Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Is your feature request related to a problem? Please describe.

The types of JSONField are all displayed as Any in swagger:

image

I need to import openapi.json into a TypeScript project for use, and this makes the generated openapi.json very poor in usability.

Describe the solution you'd like

I found in tortoise/contrib/pydantic/creator.py has

        # Json fields
        elif field_type is JSONField:
            properties[fname] = Any

I think we can avoid any by adding an optional type json_schema when defining the field, like this:

image

Or is there any better way to achieve this?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in tortoise/contrib/pydantic/creator.py at the JSONField branch that currently assigns Any. Read how field annotations are defined and how generated schemas become OpenAPI, then determine how an optional JSONField type hint should be represented. Done means JSONField annotations produce more useful types in the generated openapi.json instead of Any.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.