'FastAPI Textarea OpenAPI for Form Data

I am using Form Data format for APIs. The thing is how I am going to make OpenAPI input being larger by using textarea?

image



Solution 1:[1]

i've hacked.

my way is when specific field name, change input(type=text) to textarea. first,
you serve self-hosting javascript and css for docs. https://fastapi.tiangolo.com/advanced/extending-openapi/#self-hosting-javascript-and-css-for-docs.

second,
you change javascript source code like this.

                    var is_textarea = false;
                    // when fieldname is info_body, change to textarea
                    if(i === "info_body") {
                        is_textarea = true;
                    }
                    return l && "file" === l ? D.a.createElement(d, {
                        type: "file",
                        className: o.length ? "invalid" : "",
                        title: o.length ? o : "",
                        onChange: this.onChange,
                        disabled: h
                    }) :( !is_textarea ? D.a.createElement(kr.a, {
                        type: c && "password" === c ? "password" : "text",
                        className: o.length ? "invalid" : "",
                        title: o.length ? o : "",
                        value: n,
                        minLength: 0,
                        debounceTimeout: 350,
                        placeholder: i,
                        onChange: this.onChange,
                        disabled: h
                    }) : (D.a.createElement("textarea",{
                        className: o.length ? "invalid" : "",
                        title: o.length ? o : "",
                        value: n,
                        minLength: 0,
                        debounceTimeout: 350,
                        placeholder: i,
                        onChange: this.onChange,
                        disabled: h
                    }))) // here!

you can hack css for more pretty but not need it for me. enjoy.

enter image description here

Solution 2:[2]

Unfortunately, it's not doable as of right now.

FastAPI is using Swagger-UI for the API preview. This particular issue was reported a while back. See issues #1578 and #1795. These issues were closed without addressing.

Solution 3:[3]

I had the same problem. But when I have added type="input" (which is quite an antipattern) FastAPI starts to work.

<form method="post" id="complain_form">
<div class="form-group">
    <label for="complain">Customer complains:</label>
    <textarea type="input" class="form-control" name="complain" id="complain" rows="3"></textarea>
</div>
<button type="submit" class="btn btn-success">Submit</button>
</form>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 modeverv
Solution 2
Solution 3 Peter Trcka