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

Issues with type array when type is (object | array) | "null" #612

Open
mattoni opened this issue Apr 7, 2024 · 3 comments
Open

Issues with type array when type is (object | array) | "null" #612

mattoni opened this issue Apr 7, 2024 · 3 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@mattoni
Copy link

mattoni commented Apr 7, 2024

I've noticed an issue with the latest update to support type arrays per 3.1.0 spec (thanks a ton for adding that!)

However, I've noticed if the type is

type:
      - array
      - "null"

or

type:
      - object
      - "null"

The null is not included in the final type. For example, this type from my spec:

title: LoadBalancerLatestTelemetryController
type: object
required:
  - time
  - controller
properties:
  time:
    $ref: ../../../../DateTime.yml
  controller:
    $ref: ../../../../Identifier.yml
  instances:
    type:
      - array
      - "null"
    items:
      $ref: ./LoadBalancerLatestTelemetryInstance.yml

generated the following code:

export type LoadBalancerLatestTelemetryController = {
    time: DateTime;
    controller: Identifier;
    instances?: LoadBalancerLatestTelemetryInstance[];
};

or, in the case of object, this:

title: PromoCode
type: object
description: A billing promo code.
required:
  - code
  - credit
  - expires
  - state
properties:
  code:
    type: string
    description: The promo "code".
  credit:
    type:
      - object
      - "null"
    description: The amount of credit the promo code offers.
    additionalProperties:
      type: object
      properties:
        amount:
          "$ref": "./BillingAmount.yml"
        expires:
          "$ref": "../DateTime.yml"
  state:
    title: PromoCodeState
    allOf:
      - required:
          - current
        properties:
          current:
            description: The current state of the promo code.
            type: string
            enum:
              - live
              - deleted
      - "$ref": "../State.yml"

generates this:

export type PromoCode = {
    /** The promo "code". */
    code: string;
    /** The amount of credit the promo code offers. */
    credit: {
        [key: string]: {
            amount?: BillingAmount;
            expires?: DateTime;
        };
    };
    state: {
        /** The current state of the promo code. */
        current: "live" | "deleted";
    } & State;
};

Every other type seems to work, and using anyOf seems to work as well:

property:
  anyOf:
    - $ref: xyz.yml
    - type: "null"

So it just seems to be these two cases that I've found so far. Any help is appreciated!

@Xiphe
Copy link
Collaborator

Xiphe commented Apr 22, 2024

Hi @mattoni, thank for reporting this 👍

To be clear, the expected outcome for your first example would be

export type LoadBalancerLatestTelemetryController = {
    time: DateTime;
    controller: Identifier;
    instances?: LoadBalancerLatestTelemetryInstance[];
} | null;

right?

@mattoni
Copy link
Author

mattoni commented Apr 22, 2024

No, it'd be

export type LoadBalancerLatestTelemetryController = {
    time: DateTime;
    controller: Identifier;
    instances?: LoadBalancerLatestTelemetryInstance[] | null;
} 

where the instances field is also nullable.

@Xiphe
Copy link
Collaborator

Xiphe commented Apr 23, 2024

my bad, yeah - that makes sense!

@Xiphe Xiphe added bug Something isn't working help wanted Extra attention is needed labels Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants