Search code examples
pythontextareaopenapifastapiform-data

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

  • 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