Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #4879
Closed
Open
Issue created Dec 28, 2019 by Administrator@rootContributor

[BUG] [Javascript] Deserialization problem on model responses

Created by: selankon

Description

When a response is defined on the .yml as an object, there are a problem with the serialization of the response: the object is empty.

This happen because the response object should be parsed as JSON before enter to the constructFromObject function, used to serialize.

openapi-generator version
➜ openapi-generator version
4.2.2
OpenAPI declaration file content or url
openapi: 3.0.0
info:
    title: RetroShare OpenApi wrapper
    version: 0.0.1
    description: RetroShare OpenApi wrapper generated using Doxygen documentation
    license:
        name: AGPLv3
servers:
    - url: http://127.0.0.1:9092/

paths:
    /jsonApiServer/version:
        post:
            operationId: JsonApiServerVersion
            summary: Write version information to given paramethers  
            deprecated: false

            
            responses:
                '200':
                    description: >
                            return: 
                            [out] major(integer)storage 
                            [out] minor(integer)storage 
                            [out] mini(integer)storage 
                            [out] extra(string)storage 
                            [out] human(string)storage 
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/RespJsonApiServerVersion'

components:      
    schemas:        
        RespJsonApiServerVersion:
            type: object
            properties:
                major:
                    type: integer
                minor:
                    type: integer
                mini:
                    type: integer
                extra:
                    type: string
                human:
                    type: string
Command line used for generation
➜ openapi-generator generate -i definition.yml -g javascript -o openapi-javascript-retroshare-api-wrapper
Steps to reproduce

First of all generate the wrapper using the command above. Then cd into it and npm install the modules.

You will need to have the retroshare-service working on your computer to get the backend answer. Here instructions for debian based systems. Then install retroshare-service. The response should be something like:

{
    "major": 0,
    "minor": 6,
    "mini": 9999,
    "extra": "-retroshare-service-OBS-deb",
    "human": "0.6.9999-retroshare-service-OBS-deb"
}

Then you can write a test to test it, copy the following code on test/ folder:

(function(root, factory) {
    if (typeof define === 'function' && define.amd) {
      // AMD.
      define(['expect.js', process.cwd()+'/src/index'], factory);
    } else if (typeof module === 'object' && module.exports) {
      // CommonJS-like environments that support module.exports, like Node.
      factory(require('expect.js'), require(process.cwd()+'/src/index'));
    } else {
      // Browser globals (root is window)
      factory(root.expect, root.RetroShareOpenApiWrapper);
    }
  }(this, function(expect, RetroShareOpenApiWrapper) {
    'use strict';
  
    var apiInstance;
  
    beforeEach(function() {
      apiInstance = new RetroShareOpenApiWrapper.DefaultApi();
    });
  
    var getProperty = function(object, getter, property) {
      // Use getter method if present; otherwise, get the property directly.
      if (typeof object[getter] === 'function')
        return object[getter]();
      else
        return object[property];
    }
  
    var setProperty = function(object, setter, property, value) {
      // Use setter method if present; otherwise, set the property directly.
      if (typeof object[setter] === 'function')
        object[setter](value);
      else
        object[property] = value;
    }
  
    describe('/jsonApiServer/version', function() {
      it('should show the jsonApiServer version, returns RespJsonApiServerVersion', function() {
        apiInstance.jsonApiServerVersion((error, data, response) => {
          if (error) {
            console.error(error);
          } else {
            console.log('API called successfully. Returned data: ', data);
            
          }
        });
        
      });
  
    });
  
  }));

And then execute the test, from the generated root folder:

./node_modules/mocha/bin/mocha --require @babel/register --recursive test/versiontest.spec.js 

The response is:

API called successfully. Returned data:  RespJsonApiServerVersion {}

As you will see the RespJsonApiServerVersion object is empty.

Suggest a fix

Debugging the generated code, if you go to ApiClient.convertToType on src/ApiClient.js you will see that the type is // for model type like User and enum class (L510), that call the type.constructFromObject(data) function.

Inside this funcion on src/model/RespJsonApiServerVersion.js if you debug data you will find the correct json object. The problem is that for serialize this object the data must be parsed as JSON using JSON.parse(data) to get the lines if (data.hasOwnProperty('major')) working.

So if we add the following line on the constructFromObject function it solves the problem on my case.

data = JSON.parse(data)
Assignee
Assign to
Time tracking