Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Merge requests
  • !3417

[C++][Pistache] Simplified model template

  • Review changes

  • Download
  • Email patches
  • Plain diff
Merged Administrator requested to merge github/fork/muttleyxd/pistache-simplified-model into master Jul 22, 2019
  • Overview 0
  • Commits 5
  • Pipelines 0
  • Changes 7

Created by: muttleyxd

PR checklist

  • Read the contribution guidelines.
  • Ran the shell script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh, ./bin/openapi3/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in .\bin\windows\. If contributing template-only or documentation-only changes which will change sample output, be sure to build the project first.
  • Filed the PR against the correct branch: master, 4.1.x, 5.0.x. Default: master.
  • Copied the technical committee to review the pull request if your PR is targeting a particular programming language.

@ravinikam @stkrwork @etherealjoy @MartinDelille

Description of the PR

Well, this PR targets two issues:

  • There's a bug in current generator, if you declare a structure containing an array, then it will be declared as std::vector, which is good. But also there won't be a setter, which for me is fine, since you can get it through getter. But it doesn't set isSet variable, which means to_json function doesn't do anything useful (well it puts some members into json, but not all).
class UnsettableVector {
public:
    std::vector<int32_t>& getSomeints();
    // Missing setter
    bool someintsIsSet() const;
    void unsetSomeints();
}

YAML:

openapi: 3.0.0
info:
  description: Some description
  version: 0.0.1
  title: Some title

tags:
  - name: hello

paths:
  "/there":
    get:
      operationId: helloThereGet
      tags:
        - hello
      summary: Do something
      responses:
        200:
          description: Successful operation
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/UnsettableVector"
servers:
  - url: http://localhost:8080
components:
  schemas:
    UnsettableVector:
      type: object
      properties:
        someints:
          type: array
          items:
            type: integer
        somestring:
          type: string

HelloApiImpl.cpp:

#include "UnsettableVector.h"

void HelloApiImpl::hello_there_get(Pistache::Http::ResponseWriter &response) {
    UnsettableVector vec;
    vec.setSomestring("Hello there");
    vec.getSomeints().push_back(1);
    vec.getSomeints().push_back(2);
    vec.getSomeints().push_back(3);

    nlohmann::json json;
    to_json(json, vec);
    
    response.send(Pistache::Http::Code::Ok, json.dump(4));
}

curl http://127.0.0.1:8080/there

{
    "somestring": "Hello there"
}                              

someints are missing

  • Second issue may not be an issue, even it may be better design, so that's a thing to discuss. I don't come from Java, so this model looks like unneeded OOP, when this model could've been a simpler struct. I don't know what to do with optional fields, since I wasn't able to influence current generator to generate different code (so it could omit some parameters), so currently I don't see if it does anything. That's why I went with simpler struct like design, which may not be right if there may be optional fields.

Anyways, this new design allows more flexible syntax, especially that we won't need to create separate json/class objects to create one object from another type.

HelloApiImpl.cpp

#include "UnsettableVector.h"

void HelloApiImpl::hello_there_get(Pistache::Http::ResponseWriter &response) {
    response.send(Pistache::Http::Code::Ok, UnsettableVector{{1, 2, 3}, "Hello there"}.to_json().dump(4));
}

curl http://127.0.0.1:8080/there

{
    "someints": [
        1,
        2,
        3
    ],
    "somestring": "Hello there"
} 
Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: github/fork/muttleyxd/pistache-simplified-model