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
  • Issues
  • #4239
Closed
Open
Issue created Oct 23, 2019 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

[cpp-restbed] items of oneOf and anyOf result in faulty code

Created by: AIexG

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

Generating a cpp-restbed-server stub with an OpenAPI Specification containing an array of multiple types/objects through oneOf or anyOf will generate faulty code, resulting in referencing non-existant headers and objects.

I've tried the PHP generator as well, resulting in the same issue.

openapi-generator version

Tried with 4.1.3 and 4.2.0-SNAPSHOT

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: Variant Selection
  description: Custom Model with variant selection
  contact:
    email: mail@example.com
  version: 1.0.0

paths:
  /myPath:
    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/mySchema'
        required: true
      responses:
        '405':
          description: 'Validation error'
          content: {}

components:
  schemas:      
    mySchema:
      type: object
      properties:
        variantSelection:
          type: array
          items:
            oneOf:
            - $ref: '#/components/schemas/foo'
            - $ref: '#/components/schemas/bar'

    foo:
      type: object
      properties:
        name:
          type: string

    bar:
      allOf:
        - $ref: '#/components/schemas/foo'
        - type: object
          properties:
            value: 
              type: string

Most important snippet:

items:
  oneOf:
    - $ref: '#/components/schemas/foo'
    - $ref: '#/components/schemas/bar'

Generates model/MySchema.h (shortened), which references a class "OneOffoobar" that does not exist:

// [...]

/* File "OneOffoobar.h" does not exist */
#include "OneOffoobar.h"
#include <vector>
#include <memory>

// [...]

class  MySchema
{
public:
    MySchema();
    virtual ~MySchema();
    
    std::string toJsonString();
    void fromJsonString(std::string const& jsonString);

    // [...]

    /* Object "OneOffoobar" obviously doesn't exist either */
    std::vector<std::shared_ptr<OneOffoobar>> getVariantSelection() const;
    void setVariantSelection(std::vector<std::shared_ptr<OneOffoobar>> value);

protected:
    std::vector<std::shared_ptr<OneOffoobar>> m_VariantSelection;
};

// [...]
Command line used for generation
java -jar <pathto>\openapi-generator-cli.jar generate -i <pathto>\myOAS.yaml -g cpp-restbed-server -o <pathto>\myOASServer
Steps to reproduce

Create a OAS3 with an array of different obects inside a oneOf/anyOf Generate a server stub with cpp-restbed.

Related issues/PRs

#15 #500 #2845 (closed)

Suggest a fix

Heterogeneous arrays should be supported in the code generator, many languages just don't support them out of the box, the YAML above might even work in a language that does actually does support them, haven't tried that out. Maybe it would be possible for the generator to detect object inheritance automatically, that way at least arrays of an object and their subclasses could be generated correctly.

I'm currently trying out the following workaround, where the array consists of objects (foobar) containing one or the other object (foo or bar). That way the code gets generated without issues!

components:
  schemas:      
    mySchema:
      type: object
      properties:
        variantSelection:
          type: array
          items:
            $ref: '#/components/schemas/foobar'

    foobar:
      type: object
      oneOf:
        - $ref: '#/components/schemas/foo'
        - $ref: '#/components/schemas/bar'

    foo:
      type: object
      properties:
        name:
          type: string

    bar:
      allOf:
        - $ref: '#/components/schemas/foo'
        - type: object
          properties:
            value: 
              type: string
Comments

I'm relatively new to Rest, OpenAPI and C++, so bear with me! :) If there is any other, smarter, way to approach this problem, feel free to enlighten me!

Assignee
Assign to
Time tracking