cyclosproject / ng-openapi-gen

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

Bug: Required fields in subclass are optional in generated interface #34

Closed alastair-todd closed 5 years ago

alastair-todd commented 5 years ago

Take this schema snippet:

Entity:
      title: Entity
      type: object
      description: An abstract object with base properties
      required:
        - id
        - displayName
      properties:
        id:
          $ref: "#/components/schemas/UUID"
        displayName:
          type: string
          minLength: 3
          maxLength: 100

ReferencableEntity:
      title: ReferencableEntity
      description: An abstract object with base properties and a property that is an immutable unique textual reference that can be used in code with impunity
      allOf:
        - $ref: "#/components/schemas/Entity"
        - type: "object"
        - required:
          - uniqueRef
        - properties:
            uniqueRef:
              type: string
              pattern: ^([a-z][a-z0-9]*)(-[a-z0-9]+)*$
              minLength: 3
              maxLength: 100
              description: lower kebab-case string similar to the displayName
              example: roles-read

generated interface:

/* tslint:disable */
import { Entity } from './entity';

/**
 * An abstract object with base properties and a property that is an immutable unique textual reference that can be used in code with impunity
 */
export interface ReferencableEntity extends Entity {

  /**
   * lower kebab-case string similar to the displayName
   */
  uniqueRef?: string;
}

Now it is possible to add required at a number of levels to which I profess no knowledge of any differential, but swagger ui is more than happy with the spec:

inheritance-required
luisfpg commented 5 years ago

Actually, the definition of ReferencableEntity is invalid. You have required and properties as elements in the allOf array. This example works as expected:

openapi: '3.0.0'
info:
  title: 'Test'
paths:
  /foo:
    get:
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#components/schemas/ReferencableEntity'

components:
  schemas:
    UUID:
      type: string
    Entity:
      title: Entity
      type: object
      description: An abstract object with base properties
      required:
        - id
        - displayName
      properties:
        id:
          $ref: "#/components/schemas/UUID"
        displayName:
          type: string
          minLength: 3
          maxLength: 100
    ReferencableEntity:
      title: ReferencableEntity
      description: An abstract object with base properties and a property that is an immutable unique textual reference that can be used in code with impunity
      allOf:
        - $ref: "#/components/schemas/Entity"
        - type: "object"
          required:
            - uniqueRef
          properties:
            uniqueRef:
              type: string
              pattern: ^([a-z][a-z0-9]*)(-[a-z0-9]+)*$
              minLength: 3
              maxLength: 100
              description: lower kebab-case string similar to the displayName
              example: roles-read