swagger-api / swagger-api/swagger-codegen

[CPP] [RESTCPP] Bug resolving object/class names when referencing from the same file

Open
#8,087 2 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 defining an object which has a property of a type which was defined in the same file, the generator cannot solve the object/class name of that referenced type, resulting in '#/ObjectName' in the generated code instead of 'ObjectName'.

If, for example, an object 'Line' contains a property of type 'Point' ('Point' being defined in the same yaml file as 'Line'), the generated class 'Line' will have an attribute of type std::shared_ptr<#/Point>, and the include command '#include "#/Point.h"'.

This seems to be a problem when no path to the referenced type is given, i.e. the referencing is direct:
$ref: '#/Point'
If instead the path to the same file (e.g. geometries.yaml) is given
$ref: './geometries.yaml#/Point'
or an initial 'definitions' layer is added to the file
$ref: '#/definitions/Point'
the code is generated correctly.

Swagger-codegen version

swagger-generator-2.4.0

Swagger declaration file content or url

Failing (file geometries.yaml):

  Point:
    type: object
    properties:
      value:
        type: integer
  Line:
    type: object
    properties:
      point:
        $ref: './geometries.yaml#/Point'
#include "#/Point.h";
class  Line : public ModelBase{
...
std::shared_ptr<#/Point> m_Point;
};

Working - 1 (file geometries.yaml)

  Point:
    type: object
    properties:
      value:
        type: integer
  Line:
    type: object
    properties:
      point:
        $ref: './geometries.yaml#/Point'

Working - 2 (file geometries.yaml)

definitions:
  Point:
    type: object
    properties:
      value:
        type: integer
  Line:
    type: object
    properties:
      point:
        $ref: '#/definitions/Point'
Command line used for generation

java -jar swagger-codegen/modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate
-i ${YAML_SERVICE_FILE_INCLUDING_A_REFERENCE_TO_LINE}
-l cpprest
-c ${CONFIG_FILE}
-o ${OUTPUT_FOLDER}

Steps to reproduce

Simply create a service that includes a type 'Line' and generate the cpprest client

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 issue with the geometries.yaml examples using the swagger-codegen CLI command and the cpprest generator. Trace how a direct same-file $ref such as #/Point is resolved and rendered, then verify that generated includes and member types use Point rather than #/Point.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.