Skip to content

XML example return value is rendered as escaped text, wrapped within <notagname> #2199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
cristalp opened this issue Feb 13, 2025 · 7 comments

Comments

@cristalp
Copy link
Contributor

I have an example of an XML document as it would be returned:

  public static final String XML = """
      <foo>
        <bar>baz</bar>
      </foo>""";

This is used within the REST endpoint:

  @APIResponse(responseCode = "200", description = "Bla bla", content = {
      @Content(mediaType = MediaType.APPLICATION_JSON, example = "JSON example"),
      @Content(mediaType = MediaType.APPLICATION_XML, example = XML),
      @Content(mediaType = MediaType.TEXT_PLAIN, example = "Text example") })

In the openapi.yml this is rendered as

            application/xml:
              example: |-
                <foo>
                  <bar>baz</bar>
                </foo>

But in SwaggerUI, I see a strange escaped text:

Image

See also fastapi/fastapi#8440

@MikeEdgar
Copy link
Member

How about something like this?

@XmlRootElement(name = "foo")
static class FooSchema { }

@APIResponse(responseCode = "200", description = "Bla bla", content = {
      @Content(mediaType = MediaType.APPLICATION_JSON, example = "JSON example"),
      @Content(mediaType = MediaType.APPLICATION_XML, example = XML,
               schema = @Schema(implementation = FooSchema.class)),
      @Content(mediaType = MediaType.TEXT_PLAIN, example = "Text example") })

@cristalp
Copy link
Contributor Author

cristalp commented Feb 14, 2025

Thanks, that's a workaround which works. However, it's kind of obscure :-) , so it would be nice if this was generated correctly right out of the box. By this I mean that it's clear from the MediaType that it's XML, so the plugin could generate what's necessary to get it right.

@MikeEdgar
Copy link
Member

Right, I agree. It seems to be more an issue with Swagger UI in my opinion. I'd expect it to perhaps be aware of the representation for the XML media type without requiring the schema. I wouldn't have guessed the work-around without the fastapi issue you linked.

@MikeEdgar
Copy link
Member

By this I mean that it's clear from the MediaType that it's XML, so the plugin could generate what's necessary to get it right.

Unfortunately, just from the @Content without the schema, there isn't enough information to do it. Generating an empty schema always in such cases would not be good either.

@cristalp
Copy link
Contributor Author

Too bad... so there's no solution except defining schemas by hand?

@MikeEdgar
Copy link
Member

Do you already have a class that represents the XML that includes @XmlRootElement? The @Schema annotation doesn't include a way to specify XML information, but it has been requested in the spec [1]. No one has had a chance to work on that yet, but contributions are always welcome, of course.

[1] microprofile/microprofile-open-api#530

@cristalp
Copy link
Contributor Author

cristalp commented Feb 17, 2025

Thanks for the information. Unfortunately, at the moment I'm not able to take out some time of my schedule. But maybe one day I'll be able to convince my boss :-)

Anyway, since the other feature request already exists, please close this one - and thanks for the workaround!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants