swagger-api / swagger-api/swagger-codegen

[CPPRest] [C++] Object without properties results in segmentation fault in generated code

Open
#7,557 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

When generating code using a swagger.yaml file that includes an object without properties in the definitions like so:

attributes:
  type: object

the generated model file contains this fromJson function:

void Attributes::fromJson(web::json::value& val)
{
    if(val.has_field(utility::conversions::to_string_t("attribute1")))
    {
        if(!val[utility::conversions::to_string_t("attribute1")].is_null())
        {
            std::shared_ptr<Object> newItem(nullptr);
            newItem->fromJson(val[utility::conversions::to_string_t("attribute1")]);
            setAttribute1( newItem );
        }
    }
}

As you can see a shared_ptr is used that points to nullptr therefore every time I run my program I get a segmentation fault when newItem->fromJson() is called because it's trying to access the function from the nullptr which can't work.

When I tried adding some dummy properties to the object the generation seemed to work correctly. However, my service return the contents of a JSON file which I can't know the structure of so I can't add any correct properties to workaround this problem. Am I doing something wrong? Is there another way to describe the response? When using the same YAML the generation of an Java server and client works without problems.

Swagger-codegen version

I've encountered this problem when trying Codegen version 2.2.1, 2.2.3, 2.3.0 and 2.3.1.

Swagger declaration file content or url

This YAML results in the same problem. In my personal swagger file the attributes object has more than this one property but that doesn't seem to be an issue.

swagger: '2.0'
info:
  title: Test API
  description: test api
  version: "1.0.0"
schemes:
  - https
  - http
tags:
  - name: test
produces:
  - application/json
paths:
  /test:
    get:
      tags:
        - test
      summary: Test path
      operationId: testId
      produces:
        - application/json
      responses:
        '200':
          description: Attributes of the system
          schema:
            $ref: '#/definitions/attributes'
        '400':
          description: Error

definitions:
  attributes:
    type: object
    properties:
      attribute1:
        type: object
Command line used for generation

I'm using

java -jar ~/Downloads/swagger-codegen-cli.jar generate -i ./test.yaml -l cpprest

to generate the code.

Steps to reproduce
  1. Put the above mentioned YAML into a new test.yaml file.
  2. Download the current codegen jar (version 2.3.1)
  3. Use the above mentioned command to generate the client.
  4. Navigate to Attributes.cpp and find the fromJson function to see a nullptr being set and referenced.
Related issues/PRs

I haven't found a related issue yet I think.

Suggest a fix/enhancement

I think the error is in this line

std::shared_ptr<Object> newItem(nullptr);

as that's where the nullptr gets set. Maybe switching to something like new Object() instead of nullptr would help but I'm not proficient enough in C++ to be completely certain.

Thanks for your help!

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

Reproduce the problem with the provided test.yaml and generation command, then inspect the generated Attributes.cpp fromJson function. Trace the cpprest generator template that emits this code and verify the generated client handles an object without properties without dereferencing a null pointer; confirm the reproduction no longer segfaults.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
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.