cercide / fastapi-xml

adds xml support to fastapi
MIT License
12 stars 2 forks source link

CDATA element in dataclass? #11

Open rsoika opened 3 months ago

rsoika commented 3 months ago

Is it possible to define a dataclass that represents a string wrapped in CDATA

like:

<PromptData>
    <prompt><![CDATA[
.....
]]></prompt>
</PromptData> 
cercide commented 3 months ago

I am not sure about that. Have you figured out, how to do it with xsdata alone?

By myself, i did not find any reference within the xsdata documentation. Please keep me updated if you figure this out.

rsoika commented 3 months ago

Hi @cercide , no I did not - xsdata did not mention CDATA. I found this discussion but I do not now how to adapt this (I am new to python)

cercide commented 3 months ago

Upon further research, unfortunately, there's no easy solution. Let's start with the basics.

What is CDATA? In this post, Helen succinctly explains: "CDATA is not part of the XML logical structure; it's simply a way to indicate text containing special characters to prevent such text from being interpreted as XML markup."

How does fastapi-xml work? This library is straightforward:

Request -> Decoder -> [your code] -> Encoder -> Response

Or, in library terms:

FastAPI -> xsdata -> [your code] -> xsdata -> FastAPI

How to use CDATA with fastapi-xml? You don't. xsdata does not support CDATA. OpenAPI does not support CDATA. Therefore, fastapi-xml does not support CDATA.

How do I implement CDATA support? Consider using a Custom Route along with lxml.