swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

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

Open sfocke opened this issue 8 years ago • 2 comments

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

sfocke avatar Apr 27 '18 14:04 sfocke

failing and working 1 are identical

stkrwork avatar May 07 '18 02:05 stkrwork

Sorry, the referencing of the failing one is $ref: '#/Point': Point: type: object properties: value: type: integer Line: type: object properties: point: $ref: '#/Point'

sfocke avatar May 07 '18 05:05 sfocke