Skip to content
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

[BUG] [C++][Pistache] cpp-pistache-server generating API include undefined "Object.h" #2769

Open
5 of 6 tasks
haibinzero opened this issue Apr 30, 2019 · 6 comments
Open
5 of 6 tasks

Comments

@haibinzero
Copy link

haibinzero commented Apr 30, 2019

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

The generated Api.h file include "Obejct.h" which was not exist

/*
 * DefaultApi.h
 *
 *
 */

#ifndef DefaultApi_H_
#define DefaultApi_H_


#include <pistache/endpoint.h>
#include <pistache/http.h>
#include <pistache/router.h>
#include <pistache/http_headers.h>

#include "Interface.h"
#include "Object.h"
#include <string>

namespace io {
namespace swagger {
namespace server {
namespace api {

using namespace io::swagger::server::model;

class  DefaultApi {

The Obejct.h was not generated and used :

.
├── api
│   ├── DefaultApi.cpp
│   └── DefaultApi.h
├── CMakeLists.txt
├── impl
│   ├── DefaultApiImpl.cpp
│   └── DefaultApiImpl.h
├── main-api-server.cpp
├── model
│   ├── Helpers.cpp
│   ├── Helpers.h
│   ├── InlineResponse200.cpp
│   ├── InlineResponse200.h
│   ├── Interface.cpp
│   ├── Interface.h
│   ├── Link.cpp
│   └── Link.h
└── README.md

openapi-generator version

4.0.0-SNAPSHOT

OpenAPI declaration file content or url

swagger 2.0 but not openapi 3.0.0

swagger: '2.0'

info:
  title: REST API
  version: v1

basePath: /api/hardware/v1

schemes:
  - http

paths:
  /vnf/{vnfId}/vm/{vmId}/interface:
    parameters:
      - in: path
        name: vnfId
        description: VNF instance Id
        required: true
        type: string
      - in: path
        name: vmId
        description: VM instance Id
        required: true
        type: string
    post:
      summary: Create interface instance under specified VM
      description: Create interface
      consumes:
        - application/json
      produces:
        - application/json
      parameters:
        - in: body
          name: interface
          description: json containing interface configuration
          schema:
            $ref: "#/definitions/Interface"
      responses:
        '200':
          description: OK
          schema:
            properties:
              link:
                $ref: "#/definitions/Link"
          examples:
            application/json:
              {
                "link": {
                  "rel": "self",
                  "href": "/vnf/1/vm/1/interface/1"
                }
              }
        '400':
          description: Bad request
        '404':
          description: Not found
        default:
          description: Unexpected error

definitions:
  Link:
    title: HATEOAS link
    properties:
      rel:
        description: relation with the link
        type: string
        example: "self"
      href:
        description: URIs relative to base URI
        type: string
        example: "/vnf/1/vm/100/interface/1"
    required:
      - href

  Interface:
    title: Interface.
    required:
      - ipAddress
      - name
      - type
    properties:
      ipAddress:
        type: string
        example: 169.254.0.4
      name:
        type: string
        enum:
          - eth0
          - eth1
        example: eth0
      type:
        type: string
        enum:
          - physical
          - logical
        example: physical
Command line used for generation
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
-i swagger.yaml \
-g cpp-pistache-server \
-o cpp
Steps to reproduce

Generate the code then check the DefaultApi.h file.

Related issues/PRs

#2338 for C but not for C++

Suggest a fix

Remove the "Object.h" from DefaultApi.h file, maybe i don't know the reason why adding this, could someone point out?

@auto-labeler
Copy link

auto-labeler bot commented Apr 30, 2019

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@etherealjoy
Copy link
Contributor

Duplicate of #1827

@etherealjoy etherealjoy marked this as a duplicate of #1827 May 1, 2019
@stkrwork stkrwork changed the title [BUG] [C++] cpp-pistache-server generating API include undefined "Object.h" [BUG] [C++][Pistache] cpp-pistache-server generating API include undefined "Object.h" May 2, 2019
@etherealjoy
Copy link
Contributor

cc:
@muttleyxd

@mlebihan
Copy link
Contributor

mlebihan commented Apr 26, 2024

Hello,

Is this issue only a configuration file problem? I'm trying to work on this issue that also affects some others generators, but not all of them.

I did few changes to make a test showing the problem on
master...mlebihan:openapi-generator:any_type
and I generate from a custom petstore, where I've added to Pet definition a:

        veterinarianVisit:
          type: object
          description: last veterinarian visit advice

Debugging things, it looks that C generator does generates its object.h and object.c files:

image

But cpp-pistache generator doesn't generate its equivalent Object.h and Object.cpp:

image

[main] ERROR org.openapitools.codegen.ObjectAnyTypeSetTest - Object.h expected file hasn't been produced by cpp-pistache-server generator.
[main] ERROR org.openapitools.codegen.ObjectAnyTypeSetTest - Object.cpp expected file hasn't been produced by cpp-pistache-server generator.

java.lang.AssertionError: 2 files ([Object.cpp, Object.h]) are lacking in cpp-pistache-server generation
Expected :true
Actual   :false
<Click to see difference>


	at org.testng.Assert.fail(Assert.java:110)
	at org.testng.Assert.failNotEquals(Assert.java:1413)
	at org.testng.Assert.assertTrue(Assert.java:56)
	at org.openapitools.codegen.ObjectAnyTypeSetTest.assertGeneratedFiles(ObjectAnyTypeSetTest.java:72)
	at org.openapitools.codegen.ObjectAnyTypeSetTest.testSomeWithPetstoreWithAbstract(ObjectAnyTypeSetTest.java:45)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at org.testng.internal.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:135)
	at org.testng.internal.invokers.TestInvoker.invokeMethod(TestInvoker.java:673)
	at org.testng.internal.invokers.TestInvoker.invokeTestMethod(TestInvoker.java:220)
	at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:50)
	at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:945)
	at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:193)
	at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
	at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:128)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at org.testng.TestRunner.privateRun(TestRunner.java:808)
	at org.testng.TestRunner.run(TestRunner.java:603)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:429)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:423)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:383)
	at org.testng.SuiteRunner.run(SuiteRunner.java:326)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1249)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
	at org.testng.TestNG.runSuites(TestNG.java:1092)
	at org.testng.TestNG.run(TestNG.java:1060)
	at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:65)
	at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:105)

Is it only a configuration issue?
What clues can you give me to help me solving it?

@mlebihan
Copy link
Contributor

mlebihan commented May 1, 2024

Object for C++ pistache looks working correctly with:

master...mlebihan:openapi-generator:any_type change

(my test isn't accurate and working, but the generation looks to be)

mlebihan added a commit to mlebihan/openapi-generator that referenced this issue May 2, 2024
…fined "Object.h" (OpenAPITools#2769)

Should handle Object.h, AnyType.h correctly. Set.h also tested.

   - #include Object.h removed and replaced by a typeMapping.put(object, nlohmann::json) like suggested in other issues
   - object had an invalid syntax: ':' instead of '::' in types with namespace
   - extra include of #include nlohmann/json.h removed when there's already #include <nlohmann/json.hpp>
   - nlohmann::json is excluded from model namespace

Tested with custom petstore played, with suggested openapi specs coming from issues OpenAPITools#2769, OpenAPITools#10266, OpenAPITools#14234

   ```bash
   rm -rf samples/server/petstore/cpp-pistache-everything/ && ./bin/generate-samples.sh ./bin/configs/cpp-pistache-server-cpp-pistache-everything.yaml  && cd samples/server/petstore/cpp-pistache-everything/ && mkdir build && cd build && cmake .. && cmake --build . --parallel
   ```
mlebihan added a commit to mlebihan/openapi-generator that referenced this issue May 2, 2024
…fined "Object.h" (OpenAPITools#2769)

Should handle Object.h, AnyType.h correctly. Set.h also tested.

   - #include Object.h removed and replaced by a typeMapping.put(object, nlohmann::json) like suggested in other issues
   - object had an invalid syntax: ':' instead of '::' in types with namespace
   - extra include of #include nlohmann/json.h removed when there's already #include <nlohmann/json.hpp>
   - nlohmann::json is excluded from model namespace

Tested with custom petstore played, with suggested openapi specs coming from issues OpenAPITools#2769, OpenAPITools#10266, OpenAPITools#14234

   ```bash
   rm -rf samples/server/petstore/cpp-pistache-everything/ && ./bin/generate-samples.sh ./bin/configs/cpp-pistache-server-cpp-pistache-everything.yaml  && cd samples/server/petstore/cpp-pistache-everything/ && mkdir build && cd build && cmake .. && cmake --build . --parallel
   ```
wing328 pushed a commit that referenced this issue May 4, 2024
#18553)

* [BUG] [C++][Pistache] cpp-pistache-server generating API include undefined "Object.h" (#2769)

Should handle Object.h, AnyType.h correctly. Set.h also tested.

   - #include Object.h removed and replaced by a typeMapping.put(object, nlohmann::json) like suggested in other issues
   - object had an invalid syntax: ':' instead of '::' in types with namespace
   - extra include of #include nlohmann/json.h removed when there's already #include <nlohmann/json.hpp>
   - nlohmann::json is excluded from model namespace

Tested with custom petstore played, with suggested openapi specs coming from issues #2769, #10266, #14234

   ```bash
   rm -rf samples/server/petstore/cpp-pistache-everything/ && ./bin/generate-samples.sh ./bin/configs/cpp-pistache-server-cpp-pistache-everything.yaml  && cd samples/server/petstore/cpp-pistache-everything/ && mkdir build && cd build && cmake .. && cmake --build . --parallel
   ```

* - Adding to samples/server/petstore cpp-pistache-everything

* - .md and FILES missing
@mlebihan
Copy link
Contributor

mlebihan commented May 5, 2024

There's currently no tests available under pistache server.

I've implemented a small server responding to get id, after having pulled master as the PR is merged.

#include "PetApiImpl.h"

namespace org {
namespace openapitools {
namespace server {
namespace api {

using namespace org::openapitools::server::model;

/** A gifted dog */
Pet giftedDog;

/** Mammals category. */
Category mammals;


PetApiImpl::PetApiImpl(const std::shared_ptr<Pistache::Rest::Router>& rtr)
    : PetApi(rtr)
{
    // Mammals category
    mammals.setId(879875);
    mammals.setName("Mammals");

    // Tags: domestic, noisy
    Tag domestic;
    domestic.setId(15);
    domestic.setName("domestic");

    Tag noisy;
    noisy.setId(16);
    noisy.setName("noisy");

    // Photos
    std::vector<std::string> photosUrls = {"https://1.com", "https://2.com"};

    // Dog friends
    Pet_bestFriends bestFriends;
    bestFriends.setBestFriends({"Dizzie", "Dragonfly"});

    // The dog
    giftedDog.setId(1578923L);
    giftedDog.setBestFriends(bestFriends);
    giftedDog.setCategory(mammals);
    giftedDog.setGoodies({"dog bowl", "comb"});

    giftedDog.setName("Hercule");
    giftedDog.setPhotoUrls(photosUrls);
    giftedDog.setStatus("Asleep");

    std::vector<Tag> tags = { domestic, noisy };
    giftedDog.setTags(tags);

    time_t now = time(0);
    giftedDog.setVeterinarianVisit(now);

    giftedDog.getCategory();
}

void PetApiImpl::add_pet(const Pet &pet, Pistache::Http::ResponseWriter &response) {
    response.send(Pistache::Http::Code::Ok, "Do some magic\n");
}
void PetApiImpl::delete_pet(const int64_t &petId, const std::optional<Pistache::Http::Header::Raw> &apiKey, Pistache::Http::ResponseWriter &response) {
    response.send(Pistache::Http::Code::Ok, "Do some magic\n");
}
void PetApiImpl::find_pets_by_status(const std::optional<std::vector<std::string>> &status, Pistache::Http::ResponseWriter &response) {
    response.send(Pistache::Http::Code::Ok, "Do some magic\n");
}
void PetApiImpl::find_pets_by_tags(const std::optional<std::vector<std::string>> &tags, Pistache::Http::ResponseWriter &response) {
    response.send(Pistache::Http::Code::Ok, "Do some magic\n");
}
void PetApiImpl::get_pet_by_id(const int64_t &petId, Pistache::Http::ResponseWriter &response) {
    response.headers().add<Pistache::Http::Header::ContentType>(MIME(Application, Json));

    nlohmann::json jsonParser;
    nlohmann::to_json(jsonParser, giftedDog);
    std::string json = to_string(jsonParser);

    response.send(Pistache::Http::Code::Ok, json);
}
void PetApiImpl::update_pet(const Pet &pet, Pistache::Http::ResponseWriter &response) {
    response.send(Pistache::Http::Code::Ok, "Do some magic\n");
}
void PetApiImpl::update_pet_with_form(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter &response){
    response.send(Pistache::Http::Code::Ok, "Do some magic\n");
}
void PetApiImpl::upload_file(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter &response){
    response.send(Pistache::Http::Code::Ok, "Do some magic\n");
}

}
}
}
}

http://localhost:8080/v2/pet/1578923 returns this. I think it's ok.

{
  "bestFriends": {
    "bestFriends": [
      "Dizzie",
      "Dragonfly"
    ]
  },
  "category": {
    "id": 879875,
    "name": "Mammals"
  },
  "goodies": [
    "dog bowl",
    "comb"
  ],
  "id": 1578923,
  "name": "Hercule",
  "photoUrls": [
    "https://1.com",
    "https://2.com"
  ],
  "status": "Asleep",
  "tags": [
    {
      "id": 15,
      "name": "domestic"
    },
    {
      "id": 16,
      "name": "noisy"
    }
  ],
  "veterinarianVisit": 1714881239
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants