Skip to content

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

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

Open
5 of 6 tasks
AIexG opened this issue Oct 23, 2019 · 5 comments
Open
5 of 6 tasks

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

AIexG opened this issue Oct 23, 2019 · 5 comments
Labels
Client: C++ Inline Schema Handling Schema contains a complex schema in items/additionalProperties/allOf/oneOf/anyOf Issue: Bug

Comments

@AIexG
Copy link
Contributor

AIexG commented Oct 23, 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

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: [email protected]
  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

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!

@auto-labeler
Copy link

auto-labeler bot commented Oct 23, 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.

@saiejp
Copy link

saiejp commented Oct 24, 2019

I am seeing a similar issue to this, however, in my case the Java code that is being generated is invalid. Essentially I get an invalid class generated. I have bypassed this by setting skipFormModel=true because it's a model. Do you need an example of this ?

@jwilmoth
Copy link

I can confirm the same issue as @saiejp with the 4.1.3 version of openapi-generator-maven-plugin.

Here's a spec that reproduces the issue:

openapi: 3.0.2
info:
  title: Heterogeneous Collection Sample API
  version: 1.0.0

paths:
  /pets:
    post:
      summary: Add a list of pets.
      requestBody:
        $ref: '#/components/schemas/Pets'
      responses:
        '200':
          description: OK

components:
  schemas:
    Pets:
      type: object
      properties:
        heterogenousPets:
          type: array
          items:
            anyOf:
              - $ref: '#/components/schemas/Cat'
              - $ref: '#/components/schemas/Dog'
              - $ref: '#/components/schemas/Lizard'
            discriminator:
              propertyName: petType
    Pet:
      type: object
      required:
        - petType
      properties:
        petType:
          type: string
      discriminator:
        propertyName: petType

    Cat:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Cat`
          properties:
            name:
              type: string
    Dog:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Dog`
          properties:
            bark:
              type: string
    Lizard:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Lizard`
          properties:
            lovesRocks:
              type: boolean

This results in a Pets.java class with the following property definition:

private List<AnyOfCatDogLizard> heterogenousPets = null;

Only there is no AnyOfCatDogLizard.java file generated.

@etinshome
Copy link

I have the very same problem with Pistache generator.

@AIexG
Copy link
Contributor Author

AIexG commented Dec 10, 2019

For now I've switched from using oneOf inside array items to creating a simple schema referencing the schemas previously in oneOf. The request stays the same, just a new model gets created. Creates a favorable result.

openapi: 3.0.1
info:
  title: Variant Selection
  description: Custom Model with variant selection
  contact:
    email: [email protected]
  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:
            $ref: '#/components/schemas/foobar'

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

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

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

@AIexG AIexG changed the title [cpp-restsdk] items of oneOf and anyOf result in faulty code [cpp-restbed] items of oneOf and anyOf result in faulty code Feb 4, 2020
@spacether spacether added the Inline Schema Handling Schema contains a complex schema in items/additionalProperties/allOf/oneOf/anyOf label Mar 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Client: C++ Inline Schema Handling Schema contains a complex schema in items/additionalProperties/allOf/oneOf/anyOf Issue: Bug
Projects
None yet
Development

No branches or pull requests

6 participants