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

Missing nillable value in generated record field when object property has oneOf with $ref and nullable fields #7424

Open
Nuvindu opened this issue Nov 29, 2024 · 2 comments

Comments

@Nuvindu
Copy link
Contributor

Nuvindu commented Nov 29, 2024

Description

When the OpenAPI spec has the following, the generated type should properly reflect the oneOf schema with $ref and nullable by mapping it to the specific referenced type with a nullable modifier (repository? in this case).

"pull-request-simple": {
  "title": "Pull Request Simple",
  "description": "Pull Request Simple",
  "type": "object",
  "properties": {
    "head": {
      "type": "object",
      "properties": {
        "repo": {
          "oneOf": [
            {
              "$ref": "#/components/schemas/repository"
            },
            {
              "nullable": true
            }
          ]
        }
      }
    }
  }
}

In this case, the generated type for the property repo should be,

public type pullrequestsimple_base record {
    repository? repo;
};

However, the tool generates the following type instead,

public type pullrequestsimple_base record {
    anydata repo;
};

Steps to Reproduce

  1. Extract the content from the following json content
    https://github.com/ballerina-platform/module-ballerinax-github/blob/master/docs/spec/openapi.json

  2. Generate the files using the following command

    bal openapi -i openapi.json --mode client --client-methods resource

Version

2201.10.0

Environment Details (with versions)

No response

@TharmiganK
Copy link
Contributor

Schema to reproduce the issue:

openapi: 3.0.1
info:
  title: Api
  version: 0.0.0
servers:
  - url: "{server}:{port}/api"
    variables:
      server:
        default: http://localhost
      port:
        default: "8080"
paths:
  /path:
    get:
      operationId: getPath
      responses:
        "200":
          description: Ok
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/RecordB"
components:
  schemas:
    RecordA:
      required:
        - name
      type: object
      properties:
        name:
          type: string
    RecordB:
      required:
        - repo
      type: object
      properties:
        repo:
          oneOf:
            - $ref: "#/components/schemas/RecordA"
            - nullable: true

@TharmiganK
Copy link
Contributor

As a workaround you can use allOf in here:

"pull-request-simple": {
  "title": "Pull Request Simple",
  "description": "Pull Request Simple",
  "type": "object",
  "properties": {
    "head": {
      "type": "object",
      "properties": {
        "repo": {
          "allOf": [
            {
              "$ref": "#/components/schemas/repository"
            }
          ],
          "nullable": true
        }
      }
    }
  }
}

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

3 participants