question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG][JAVA][SPRING] Endpoints don't support different schema per content-type

See original GitHub issue

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What’s the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What’s the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

We want to support multiple schema for a single endpoint, depending on content-types. According to https://swagger.io/docs/specification/describing-request-body/ this is exactly what the OpenAPI spec should support:

The requestBody is more flexible in that it lets you consume different media types, such as JSON, XML, form data, plain text, and others, and use different schemas for different media types.

However the generator always combines the different content-types into a single endpoint, only using the first content-type, with the following output:

[INFO] --- openapi-generator-maven-plugin:4.3.1:generate (testEndpoint) @ pac-service ---
[INFO] OpenAPI Generator: spring (server)
[INFO] Generator 'spring' is considered stable.
[WARNING] Multiple schemas found in the OAS 'content' section, returning only the first one (application/x.foo+json)
Multiple schemas found in the OAS 'content' section, returning only the first one (application/x.foo+json)
openapi-generator version

openapi-generator-maven-plugin 4.3.1

OpenAPI declaration file content or url

https://gist.github.com/bonii-xx/1d48dd6d8e624aa3689cbb062ac2a7d2

Command line used for generation

Via openapi-generator-maven-plugin.

Steps to reproduce

Paste file above into https://editor.swagger.io/ Click on Generate Server -> spring Check code of generated FoobarApi Expect 2 endpoints with different RequestBodies, but only one is present handling both. The second request body schema for the second content type is not accepted.

Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/144 https://github.com/OpenAPITools/openapi-generator/issues/3990 https://github.com/OpenAPITools/openapi-generator/pull/3991

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:23
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
Staff-dcommented, Apr 16, 2021

I’d like to push this 🙂

0reactions
spacethercommented, Jul 15, 2022

For python-experimental I am using #10973 in this PR: https://github.com/OpenAPITools/openapi-generator/pull/8325 to allow users to send request bodies with different body content types supported. One can see it working here: https://github.com/OpenAPITools/openapi-generator/blob/master/samples/openapi3/client/petstore/python-experimental/petstore_api/api/pet_api_endpoints/add_pet.py#L78

pet_parameter = api_client.RequestBody(
    content={
        'application/json': api_client.MediaType(schema=SchemaForRequestBodyApplicationJson),
        'application/xml': api_client.MediaType(schema=SchemaForRequestBodyApplicationXml),
    },
    required=True,
)

Java and other generators could do something similar

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring boot controller content negotiation - java - Stack Overflow
When I implement two methods each with different mapping and different content type, I am able to get XML from the xml one...
Read more >
RFC 7644: System for Cross-domain Identity Management
Specific information about what attributes are defined within a schema MAY be obtained by querying a SCIM service provider's "/Schemas" endpoint for a ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found