swagger-api / swagger-api/swagger-codegen

[JAVA] JAX-RS Server InvalidDefinitionException: Cannot construct instance by using oneOf

Open
#10,520 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

I have the following swagger.yaml that uses the oneOf keyword and I used the latest Swagger codegen tools (3.0.22) to create a server stub for JAX-RS Jersey.


openapi: 3.0.0
info:
  version: 0.0.1
  title: Polymorphism example
  description: ''

paths:
 /animal:
    post:
      summary: Create a new animal   
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Animal'
              
      responses:
        '201':
          description: Created        

components:
  schemas:
  
    Animal:
      properties:
        name: 
          type: string
        props:  
          oneOf:
            - $ref: '#/components/schemas/Fish'
            - $ref: '#/components/schemas/Dog'
      
      
    Fish:
      type: object
      properties:
        depth:
          type: integer
          
    
    Dog:
      type: object
      properties:
        country:
          type: string      


I am trying to post the following example object:

{
   "name":"Foo",
   "props":{
      "country":"Bar"
   }
}

But this results in the following exception:


javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `io.swagger.model.OneOfAnimalProps` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
 at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 1, column: 30] (through reference chain: io.swagger.model.Animal["props"])
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl (WebComponent.java:432)
    at org.glassfish.jersey.servlet.WebComponent.service (WebComponent.java:370)
    at org.glassfish.jersey.servlet.ServletContainer.service (ServletContainer.java:389)
    at org.glassfish.jersey.servlet.ServletContainer.service (ServletContainer.java:342)
    at org.glassfish.jersey.servlet.ServletContainer.service (ServletContainer.java:229)
    at org.eclipse.jetty.servlet.ServletHolder.handle (ServletHolder.java:808)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter (ServletHandler.java:1669)
    at io.swagger.api.ApiOriginFilter.doFilter (ApiOriginFilter.java:15)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter (ServletHandler.java:1652)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle (ServletHandler.java:585)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle (ScopedHandler.java:143)
    at org.eclipse.jetty.security.SecurityHandler.handle (SecurityHandler.java:577)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle (SessionHandler.java:223)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle (ContextHandler.java:1127)
    at org.eclipse.jetty.servlet.ServletHandler.doScope (ServletHandler.java:515)
    at org.eclipse.jetty.server.session.SessionHandler.doScope (SessionHandler.java:185)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope (ContextHandler.java:1061)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle (ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle (ContextHandlerCollection.java:215)
    at org.eclipse.jetty.server.handler.HandlerCollection.handle (HandlerCollection.java:110)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle (HandlerWrapper.java:97)
    at org.eclipse.jetty.server.Server.handle (Server.java:497)
    at org.eclipse.jetty.server.HttpChannel.handle (HttpChannel.java:310)
    at org.eclipse.jetty.server.HttpConnection.onFillable (HttpConnection.java:257)
    at org.eclipse.jetty.io.AbstractConnection$2.run (AbstractConnection.java:540)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob (QueuedThreadPool.java:635)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run (QueuedThreadPool.java:555)
    at java.lang.Thread.run (Thread.java:748)
Swagger-codegen version

Swagger-codegen version: 3.0.22

Command line used for generation

CLI command: java -jar swagger-codegen-cli.jar generate -i openapi.yaml -l jaxrs-jersey -o jaxrs-jersey/

Steps to reproduce
  1. download swagger codegen 3.0.22 and save the swagger.yaml to openapi.yaml
  2. java -jar swagger-codegen-cli.jar generate -i openapi.yaml -l jaxrs-jersey -o jaxrs-jersey/
  3. cd jaxrs-jersey/
  4. mvn jetty:run
  5. post the above example object via curl: curl -X POST "http://localhost:8080/animal" -H "accept: */*" -H "Content-Type: application/json" -d "{\"name\":\"foo\",\"props\":{\"country\":\"bar\"}}"

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

Start by reproducing the failure with openapi.yaml, Swagger Codegen 3.0.22, the jaxrs-jersey generator, and the Maven Jetty run described in the issue. Inspect the generated Animal model and OneOfAnimalProps path shown in the Jackson stack trace. Done means the example POST payload is accepted by the generated JAX-RS Jersey server without InvalidDefinitionException.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.