Skip to content

[C++]Enable forward declaration for C++ #6654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 21, 2020

Conversation

etherealjoy
Copy link
Contributor

Fixes #6377
@muttleyxd @MartinDelille @stkrwork @ravinikam
@wing328

PR checklist

  • Read the contribution guidelines.
  • If contributing template-only or documentation-only changes which will change sample output, build the project beforehand.
  • Run the shell script ./bin/generate-samples.shto update all Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/config/java*. For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master
  • Copy the technical committee to review the pull request if your PR is targeting a particular programming language.

Fixes this spec below

openapi: 3.0.0
info:
  version: 0.0.1
  title: Test
paths:
  /endpoint:
    get:
      operationId: getOptions
      description: "-"
      parameters:
      - name: OptionA
        in: header
        schema:
          $ref: '#/components/schemas/OptionA'
      responses:
        200:
          description: Success
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/OptionA'
components:
  schemas:
    OptionA:
      type: object
      description: 'OptionA'
      properties:
        suboptions:
          type: array
          description: 'Suboptions of OptionA'
          items:
            $ref: '#/components/schemas/OptionB'
    OptionB:
      type: object
      description: 'OptionB'
      properties:
        suboptions1:
          type: array
          description: 'Suboptions 1 of OptionB'
          items:
            $ref: '#/components/schemas/OptionA'
        suboptions2:
          type: array
          description: 'Suboptions 2 of OptionB'
          items:
            $ref: '#/components/schemas/OptionB'

This spec below cannot be fixed due to C++ limitations.

openapi: 3.0.0
info:
  version: 0.0.1
  title: Test
paths:
  /endpoint:
    get:
      operationId: getOptions
      description: "-"
      parameters:
      - name: OptionA
        in: header
        schema:
          $ref: '#/components/schemas/OptionA'
      responses:
        200:
          description: Success
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/OptionA'
components:
  schemas:
    OptionA:
      type: object
      description: 'OptionA'
      properties:
        suboptions:
          $ref: '#/components/schemas/OptionB'
    OptionB:
      type: object
      description: 'OptionB'
      properties:
        suboptions1:
          $ref: '#/components/schemas/OptionA'

Generated code below has forward declaration and include as well.

#ifndef OAIOptionB_H
#define OAIOptionB_H

#include <QJsonObject>

#include "OAIOptionA.h"

#include "OAIEnum.h"
#include "OAIObject.h"

namespace OpenAPI {
class OAIOptionA;

class OAIOptionB : public OAIObject {
public:
    OAIOptionB();
    OAIOptionB(QString json);
    ~OAIOptionB() override;

    QString asJson() const override;
    QJsonObject asJsonObject() const override;
    void fromJsonObject(QJsonObject json) override;
    void fromJson(QString jsonString) override;

    OAIOptionA getSuboptions1() const;
    void setSuboptions1(const OAIOptionA &suboptions1);
    bool is_suboptions1_Set() const;
    bool is_suboptions1_Valid() const;

    virtual bool isSet() const override;
    virtual bool isValid() const override;

private:
    void initializeModel();

    OAIOptionA suboptions1;
    bool m_suboptions1_isSet;
    bool m_suboptions1_isValid;
};

} // namespace OpenAPI

Error for the one above

OAIOptionB.h:53: error: field ‘suboptions1’ has incomplete type ‘OpenAPI::OAIOptionA’
   53 |     OAIOptionA suboptions1;
      |                ^~~~~~~~~~~

@etherealjoy
Copy link
Contributor Author

@wing328
When you have time could you please take a look at the logic for parsing the model which could have class members that reference back to the parent class?

@etherealjoy
Copy link
Contributor Author

Other generators (like cpp-restsdk) however, which uses std::shared_ptr to instantiate class members will not be impacted, and can fully support forward declaration for circular references

@etherealjoy etherealjoy force-pushed the cpp_enable_forward_decl branch from 2467aa0 to 8232756 Compare June 15, 2020 09:33
@etherealjoy etherealjoy merged commit ec4e44a into OpenAPITools:master Jun 21, 2020
@etherealjoy etherealjoy added this to the 5.0.0 milestone Jun 21, 2020
@etherealjoy etherealjoy deleted the cpp_enable_forward_decl branch July 20, 2020 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG][C++][QT5] Code not compileable when circular scheme dependencies exist
2 participants