cebe / yii2-openapi

REST API application generator for Yii2, openapi 3.0 YAML -> Yii2
MIT License
130 stars 23 forks source link

Add ability to add "ON DELETE" and "ON UPDATE" foreign key constraints in yii2-open-api spec definitions #131

Closed SOHELAHMED7 closed 1 year ago

SOHELAHMED7 commented 1 year ago

Add ability to add "ON DELETE" and "ON UPDATE" foreign key constraints in yii2-open-api spec definitions.

Consider a user can create one or more tasks.

Now if we want to provide "ON DELETE" and "ON UPDATE" foreign key constraints in OpenAPI spec then : (initial thought about the syntax which is yet to be implemented)

# Task:
title: Task
x-table: tasks
type: object

# ----------------------- FK constraint -------------------------
x-fk-on-constraint:
  creator: # creator_id
    x-on-update: CASCADE
    x-on-delete: SET NULL
# ----------------------- FK constraint -------------------------

description: Database schema of a Task.

required:
  - id
  - creator # creator_id

properties:

  creator: # creator_id
    $ref: '../openapi.yaml#/components/schemas/User'

  id:
    type: integer
    example: 12531

@cebe feel free to provide your thoughts about how we should define foreign key constraints.

cebe commented 1 year ago

I'd prefer definition on the property itself, but it is not directly possible because $ref does not allow adding additional properties.

There is an allOf construct which essentially means that the schema must match all sub-schemas.

One could do this like:

# Task:
title: Task
x-table: tasks
type: object

description: Database schema of a Task.

required:
  - id
  - creator # creator_id

properties:

  creator: # creator_id
    allOf:
      - $ref: '../openapi.yaml#/components/schemas/User'
      - x-fk-on-update: CASCADE
      - x-fk-on-delete: SET NULL

  id:
    type: integer
    example: 12531
cebe commented 1 year ago

It would probably be useful to be able to understand the allOf syntax in general so when it occurs, the sub-arrays should be merged.