swagger-api / swagger-api/swagger-codegen

[JAVA] Issue deserializing List of dynamically typed nodes

Open
#8,954 1 comment 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

The list of nodes with strogly typed "subtypes" cannot be deserialized properly. NULL is retuned.

Swagger-codegen version

2.3.1

Swagger declaration file content or url

// Strongly typed "sub-type"
TestTheme:
    allOf:
      - $ref: '#/definitions/Node'
      - type: object
        properties:

// Please note empty properties list
Node:
    type: object
    discriminator: type
    properties: {}


'List[Node]':
    type: array
    items:
      $ref: '#/definitions/Node'

'/tov/{tovKey}/structure':
    get:
      tags:
        - tov
      summary: Returns the structure of the given test object version.
      description: ''
      parameters:
        - name: tovKey
          in: path
          description: The identifier of the test object version
          required: true
          type: integer
          format: int64
      responses:
        '200':
          description: Ok
          schema:
            $ref: '#/definitions/List[Node]' // Generic node is used with empty properties
        '403':
          description: Forbidden
          schema: null
    path: '/tov/{tovKey}/structure'

Generated model:

/*
 * OpenAPI spec version: 2.5.3
 * 
 *
 * NOTE: This class is auto generated by the swagger code generator program.
 * https://github.com/swagger-api/swagger-codegen.git
 * Do not edit the class manually.
 */


package io.my.client.model;

import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import java.io.Serializable;

// FIXME: Json subtypes cannot be properly detected. null is returned
/**
 * Node
 */
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", visible = true )
@JsonSubTypes({
  @JsonSubTypes.Type(value = TestTheme.class, name = "TestTheme"),
  @JsonSubTypes.Type(value = TestCaseSet.class, name = "TestCaseSet"),
  @JsonSubTypes.Type(value = Root.class, name = "Root"),
})

public class Node implements Serializable {
  private static final long serialVersionUID = 1L;
   ....
}
Command line used for generation

Web Gui has been used

Steps to reproduce

Test to detect bug:

stubFor(get(urlMatching("/api/1/tov/.*/structure"))
                .willReturn(aResponse()
                        .withBodyFile(tovKeyStructureGetBodyFileName)
                ));

        Long tovKey = (long)"tovKey";

        ListNode response = api.tovTovKeyStructureGet(tovKey);

        // response.toString();  - see output below:

        for (Node node : response) {
            // FIXME: nod is null
            assertThat(node).isNotNull(); // fails
        }

Returns ListNode object with empty list items:

class ListNode {
   // The list items count is right
  [null, null, null, null, null, null, null]
}
Related issues

Which jackson version should be used

Actual version: jackson_version = 2.8.9
Should version should fix bug - ?

compile "com.fasterxml.jackson.core:jackson-core:$jackson_version"
compile "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"
compile "com.fasterxml.jackson.core:jackson-databind:$jackson_version"
compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jackson_version"

Project build warnings:

Warning:(15, 43) java: com.fasterxml.jackson.databind.util.ISO8601DateFormat in com.fasterxml.jackson.databind.util has been deprecated
Warning:(16, 43) java: com.fasterxml.jackson.databind.util.ISO8601Utils in com.fasterxml.jackson.databind.util has been deprecated
Warning:(27, 20) java: com.fasterxml.jackson.databind.util.ISO8601Utils in com.fasterxml.jackson.databind.util has been deprecated
Warning:(22, 40) java: com.fasterxml.jackson.databind.util.ISO8601DateFormat in com.fasterxml.jackson.databind.util has been deprecated
Warning:(203, 18) java: mappingException(java.lang.String) in com.fasterxml.jackson.databind.DeserializationContext has been deprecated
Suggest a fix/enhancement

Please suggest the right way to deal with

  • Are generation settings wrong?
  • Possible library versions (jackson) ?
  • Should be code to deal with nodes be fixed?
  • Should be new 10.1 version used?

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 with the generated Node and ListNode classes, their Jackson annotations, and the deserialization path exercised by the shown API call. Reproduce the issue with the supplied YAML and response fixture, then verify that the list contains non-null subtype instances and that the assertion passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.