Forum Discussion

dseybo's avatar
dseybo
New Contributor
4 years ago

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

Hi Smartbear Community,

 

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/

 

Any advise how to fix this issue is much appreciated!

4 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    When using oneOf, the subschemas must have unique required properties so that the tools can match the payload to the correct schema. Try specifying the required properties for Fish and Dog and see if it helps:

        Fish:
          type: object
          required: [depth]   # <-----------
          properties:
            depth:
              type: integer
    
        Dog:
          type: object
          required: [country]  # <-----------
          properties:
            country:
              type: string  
    • dseybo's avatar
      dseybo
      New Contributor

      HKosova  thank you for the suggestion, I have applied the changes to the schemas as follows:

       

          Fish:
            type: object
            required:
                [depth]
            properties:
              depth:
                type: integer
          
          Dog:
            type: object
            required:
                [country]
            properties:
              country:
                type: string   

      But the errors results the same when calling the endpoint of the newly generated JAX-RS server

      • dirosden's avatar
        dirosden
        New Member

        dseybo - Did you get this to work? Having the same issue when parsing the request.