openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

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

Open haibinzero opened this issue 5 years ago • 6 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] What's the version of OpenAPI Generator used?
  • [x] Have you search for related issues/PRs?
  • [x] 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?

haibinzero avatar Apr 30 '19 08:04 haibinzero

👍 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.

auto-labeler[bot] avatar Apr 30 '19 08:04 auto-labeler[bot]

Duplicate of #1827

etherealjoy avatar May 01 '19 15:05 etherealjoy

cc: @muttleyxd

etherealjoy avatar Sep 29 '19 09:09 etherealjoy

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 https://github.com/OpenAPITools/openapi-generator/compare/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 avatar Apr 26 '24 02:04 mlebihan

Object for C++ pistache looks working correctly with:

https://github.com/OpenAPITools/openapi-generator/compare/master...mlebihan:openapi-generator:any_type change

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

mlebihan avatar May 01 '24 06:05 mlebihan

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
}

mlebihan avatar May 05 '24 04:05 mlebihan