cyclosproject / ng-openapi-gen

An OpenAPI 3.0 codegen for Angular
MIT License
397 stars 132 forks source link

Nullable object under $ref is not generated correctly #104

Closed agakam closed 3 years ago

agakam commented 4 years ago

I have defined API as follows:

openapi: 3.0.0
info:
  version: 1.0.0
  title: Abc
  description: Abc
paths:
  /abc:
    get:
      description: Abc
      responses:
        "200":
          description: Abc response
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Abc"
components:
  schemas:
    Abc:
      type: object
      required:
        - nullableObject
        - inlinedNullableObject
      properties:
        nullableObject:
          $ref: "#/components/schemas/NullableObject"
        inlinedNullableObject:
          type: object
          nullable: true
          required:
            - someProperty
          properties:
            someProperty:
              type: string
    NullableObject:
      type: object
      nullable: true
      required:
        - someProperty
      properties:
        someProperty:
          type: string

when I generate API using ng-openapi-gen nullable object defined under $ref is no longer nullable. This issue not exists with inlined nullable object.

Generated files:

/* tslint:disable */
import { NullableObject } from './nullable-object';
export interface Abc {
  inlinedNullableObject?: null | { 'someProperty': string };
  nullableObject: NullableObject;
}

and

/* tslint:disable */
export interface NullableObject {
  someProperty: string;
}

as you can see nullableObject is defined as NullableObject interface, so it can't be nulled.

agakam commented 4 years ago

Hi!

@luisfpg thanks for the quick fix! Your changes fixed above example, but it doesn't work in more complex case:

    Abc:
      type: object
      required:
        - nullableObject
        - inlinedNullableObject
        - withNullableProperty
      properties:
        nullableObject:
          $ref: "#/components/schemas/NullableObject"
        withNullableProperty:
          type: object
          required:
            - someProperty
          properties:
            someProperty:
              $ref: "#/components/schemas/NullableObject"
        inlinedNullableObject:
          type: object
          nullable: true
          required:
            - someProperty
          properties:
            someProperty:
              type: string

I've added property with nullable property and this is the outcome:

/* tslint:disable */
import { NullableObject } from './nullable-object';
export interface Abc {
  inlinedNullableObject: null | { 'someProperty': string };
  nullableObject: NullableObject | null;
  withNullableProperty: { 'someProperty': NullableObject };
}

Can you reopen this issue?

I've got an idea how to handle those nullable objects. It could be created as type:

interface NullableObjectInterface {
    someProperty: string
}

type NullableObject = NullableObjectInterface | null;

What do you think about that?

BR, Aga

luisfpg commented 4 years ago

So, in the example, the only error is actually withNullableProperty, right? The expected output would be:

/* tslint:disable */
import { NullableObject } from './nullable-object';
export interface Abc {
  inlinedNullableObject: null | { 'someProperty': string };
  nullableObject: NullableObject | null;
  withNullableProperty: { 'someProperty': NullableObject | null };
}
agakam commented 4 years ago

Yes, exactly. The only error is withNullableProperty and the expected output should look like this.

luisfpg commented 3 years ago

Sorry for the delay. Can you, please, @agakam, still test the master branch to make sure it works in all cases you've tested?