swagger-api / swagger-api/swagger-codegen

[QT5][QT5CPP] Bug in methods writing model data into JSON

Open
#10,332 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

The generated model classes contain methods to read data from and write data back to JSON. I'm expecting that I'd receive the same JSON object when:

auto model = new SWGMyModel();
auto jsonOutput = model->fromJson()->asJson();

Instead on the output most of the fields are not defined anymore.

The reason for that is that the m_*_isSet member variables do not get updated when loading data from a JSON string / object

Swagger-codegen version

Version 2.4.14

Swagger declaration file content or url
swagger: "2.0"
info:
  version: "1.0.0"
  title: "Bug in qt5cpp"
schemes:
- "http"
paths:

  /echo:
    post:
      parameters:
      - name: "body"
        in: "body"
        schema:
          $ref: "#/definitions/Example"
      responses:
        "200":
          description: "successful operation"

definitions:
  Example:
    type: "object"
    properties:
      int64_type:
        type: "integer"
        format: "int64"
      int32_type:
        type: "integer"
        format: "int32"
      boolean_type:
        type: "boolean"
      float_type:
        type: "number"
        format: "float"
      double_type:
        type: "number"
        format: "double"
      string_type:
        type: "string"
      enum_type:
        type: "string"
        enum:
        - "a"
        - "b"
      object_type:
        type: "object"
        properties:
          double_type:
            type: "number"
            format: "double"
          string_type:
            type: "string"
      number_array_type:
        type: "array"
        items:
          type: "string"
      string_array_type:
        type: "array"
        items:
          type: "string"
Command line used for generation

java -jar swagger-codegen-cli.jar generate -l qt5cpp -i swagger.json

Steps to reproduce
  1. generate code using provided interface description
  2. use these files to create a Qt project using the generated client
  3. run qmake && make
  4. check the output and compare to the JSON used as input
Related issues/PRs
Suggest a fix/enhancement

I don't know the purpose of m_*_isSet member variables. Either they are used to track if the user modified the model (like the use in the isSet() function suggests). Then the check for these members when writing to JSON can be removed.
Or the members are to check if the value is set at all (not undefined) then we could set the member to true if the corresponding field in the JSON document is present and add lines like the following in the fromJsonObject() method:

    m_propertyName_isSet = pJson.contains("propertyName");

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 qt5cpp generator templates that produce model fromJsonObject() and asJson(), then compare how m_*_isSet is handled during deserialization and serialization. Reproduce the issue with the supplied Swagger declaration and generation command; it is done when fields present in the input JSON are preserved in the generated model's output.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.