OpenAPITools / OpenAPITools/openapi-generator

Question: [Java] How to customise Base-class if oneOf is used as request body

Open
#18,842 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

My API spec uses oneOf when specifying the schema for a request body similar to the following:

  /request-endpoint:
    post:
      requestBody:
        content:
          application/json
            schema:
              oneOf:
                - $ref: '#/components/schemas/RequestA'
                - $ref: '#/components/schemas/RequestB'

Of course there are two schema definitions for these reqeust types.

The generator generates a base class for these types, and they are derived from it. I can customize the name of the base class by using the inlineSchemaNameMappings option (thow it is a bit tricky to find out the correct source to use):

<inlineSchemaNameMappings>
                                _request_endpoint_post_request=RequestBase
</inlineSchemaNameMappings>

The problem is that in order to correctly parse the request-body, the RequestBase interface needs some special annotations:

@Valid
@JsonTypeInfo(use = DEDUCTION)
@JsonSubTypes({ @Type(RequestA.class), @Type(RequestB.class) })

So the question is: is openapi-generator able to generate these annotations? (I only know the approach with "discriminator", but this is not usable in my case.)

It would also be a sufficient to not generate the RequestBase class using something like "schemaMapping" (which would work for RequestA and/or RequestB), but I didn't get it to work for RequestBase.

Thanks for any help.

openapi-generator version

openapi-maven-plugin 7.6.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the supplied oneOf request-body spec with openapi-maven-plugin 7.6.0 and inspect the generated RequestBase, RequestA, and RequestB types. Check how inlineSchemaNameMappings and schemaMapping affect the generated base type. Done means either the requested Jackson annotations can be generated without a discriminator, or the base type can be excluded or replaced as requested.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
backend-api-design, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.