Is there a way to generate the OpenAPI/Swagger definition from Postgres with object references?

1.6k views Asked by At

I have a rest api using supabase/postgres(t) which generates it's own OpenAPI/Swagger definition. The problem is that this only contains the id fields but not actual references to the other tables.

When generating a client with openapi-generater, the classes therefore only contain the foreign keys but not actual object references.

Is there a way to generate the openapi definition with references so I can generate my classes with references?

{
  "swagger": "2.0",
  "info": {
    "description": "standard public schema",
    "title": "PostgREST API",
    "version": "9.0.0.20220211 (pre-release) (dcf7ade)"
  },
  "host": "localhost:3000",
  "basePath": "/",
  "schemes": [
    "http"
  ],
  "consumes": [
    "application/json",
    "application/vnd.pgrst.object+json",
    "text/csv"
  ],
  "produces": [
    "application/json",
    "application/vnd.pgrst.object+json",
    "text/csv"
  ],
  "paths": {
    "/": {
      "get": {
        "tags": [
          "Introspection"
        ],
        "summary": "OpenAPI description (this document)",
        "produces": [
          "application/openapi+json",
          "application/json"
        ],
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    },
    "/hallo": {
      "get": {
        "tags": [
          "hallo"
        ],
        "parameters": [
          {
            "$ref": "#/parameters/rowFilter.hallo.id"
          },
          {
            "$ref": "#/parameters/rowFilter.hallo.created_at"
          },
          {
            "$ref": "#/parameters/rowFilter.hallo.name"
          },
          {
            "$ref": "#/parameters/select"
          },
          {
            "$ref": "#/parameters/order"
          },
          {
            "$ref": "#/parameters/range"
          },
          {
            "$ref": "#/parameters/rangeUnit"
          },
          {
            "$ref": "#/parameters/offset"
          },
          {
            "$ref": "#/parameters/limit"
          },
          {
            "$ref": "#/parameters/preferCount"
          }
        ],
        "responses": {
          "206": {
            "description": "Partial Content"
          },
          "200": {
            "description": "OK",
            "schema": {
              "items": {
                "$ref": "#/definitions/hallo"
              },
              "type": "array"
            }
          }
        }
      },
      "post": {
        "tags": [
          "hallo"
        ],
        "parameters": [
          {
            "$ref": "#/parameters/body.hallo"
          },
          {
            "$ref": "#/parameters/select"
          },
          {
            "$ref": "#/parameters/preferReturn"
          }
        ],
        "responses": {
          "201": {
            "description": "Created"
          }
        }
      },
      "delete": {
        "tags": [
          "hallo"
        ],
        "parameters": [
          {
            "$ref": "#/parameters/rowFilter.hallo.id"
          },
          {
            "$ref": "#/parameters/rowFilter.hallo.created_at"
          },
          {
            "$ref": "#/parameters/rowFilter.hallo.name"
          },
          {
            "$ref": "#/parameters/preferReturn"
          }
        ],
        "responses": {
          "204": {
            "description": "No Content"
          }
        }
      },
      "patch": {
        "tags": [
          "hallo"
        ],
        "parameters": [
          {
            "$ref": "#/parameters/rowFilter.hallo.id"
          },
          {
            "$ref": "#/parameters/rowFilter.hallo.created_at"
          },
          {
            "$ref": "#/parameters/rowFilter.hallo.name"
          },
          {
            "$ref": "#/parameters/body.hallo"
          },
          {
            "$ref": "#/parameters/preferReturn"
          }
        ],
        "responses": {
          "204": {
            "description": "No Content"
          }
        }
      }
    },
    "/lol": {
      "get": {
        "tags": [
          "lol"
        ],
        "parameters": [
          {
            "$ref": "#/parameters/rowFilter.lol.id"
          },
          {
            "$ref": "#/parameters/rowFilter.lol.inserted_at"
          },
          {
            "$ref": "#/parameters/rowFilter.lol.updated_at"
          },
          {
            "$ref": "#/parameters/rowFilter.lol.data"
          },
          {
            "$ref": "#/parameters/rowFilter.lol.name"
          },
          {
            "$ref": "#/parameters/rowFilter.lol.owner_id"
          },
          {
            "$ref": "#/parameters/select"
          },
          {
            "$ref": "#/parameters/order"
          },
          {
            "$ref": "#/parameters/range"
          },
          {
            "$ref": "#/parameters/rangeUnit"
          },
          {
            "$ref": "#/parameters/offset"
          },
          {
            "$ref": "#/parameters/limit"
          },
          {
            "$ref": "#/parameters/preferCount"
          }
        ],
        "responses": {
          "206": {
            "description": "Partial Content"
          },
          "200": {
            "description": "OK",
            "schema": {
              "items": {
                "$ref": "#/definitions/lol"
              },
              "type": "array"
            }
          }
        }
      },
      "post": {
        "tags": [
          "lol"
        ],
        "parameters": [
          {
            "$ref": "#/parameters/body.lol"
          },
          {
            "$ref": "#/parameters/select"
          },
          {
            "$ref": "#/parameters/preferReturn"
          }
        ],
        "responses": {
          "201": {
            "description": "Created"
          }
        }
      },
      "delete": {
        "tags": [
          "lol"
        ],
        "parameters": [
          {
            "$ref": "#/parameters/rowFilter.lol.id"
          },
          {
            "$ref": "#/parameters/rowFilter.lol.inserted_at"
          },
          {
            "$ref": "#/parameters/rowFilter.lol.updated_at"
          },
          {
            "$ref": "#/parameters/rowFilter.lol.data"
          },
          {
            "$ref": "#/parameters/rowFilter.lol.name"
          },
          {
            "$ref": "#/parameters/rowFilter.lol.owner_id"
          },
          {
            "$ref": "#/parameters/preferReturn"
          }
        ],
        "responses": {
          "204": {
            "description": "No Content"
          }
        }
      },
      "patch": {
        "tags": [
          "lol"
        ],
        "parameters": [
          {
            "$ref": "#/parameters/rowFilter.lol.id"
          },
          {
            "$ref": "#/parameters/rowFilter.lol.inserted_at"
          },
          {
            "$ref": "#/parameters/rowFilter.lol.updated_at"
          },
          {
            "$ref": "#/parameters/rowFilter.lol.data"
          },
          {
            "$ref": "#/parameters/rowFilter.lol.name"
          },
          {
            "$ref": "#/parameters/rowFilter.lol.owner_id"
          },
          {
            "$ref": "#/parameters/body.lol"
          },
          {
            "$ref": "#/parameters/preferReturn"
          }
        ],
        "responses": {
          "204": {
            "description": "No Content"
          }
        }
      }
    }
  },
  "definitions": {
    "hallo": {
      "required": [
        "id"
      ],
      "properties": {
        "id": {
          "type": "integer",
          "format": "bigint",
          "description": "Note:\nThis is a Primary Key.<pk/>"
        },
        "created_at": {
          "type": "string",
          "format": "timestamp with time zone",
          "default": "now()"
        },
        "name": {
          "type": "string",
          "format": "text"
        }
      },
      "type": "object"
    },
    "lol": {
      "required": [
        "id",
        "inserted_at",
        "updated_at"
      ],
      "properties": {
        "id": {
          "type": "integer",
          "format": "bigint",
          "description": "Note:\nThis is a Primary Key.<pk/>"
        },
        "inserted_at": {
          "type": "string",
          "format": "timestamp with time zone",
          "default": "timezone('utc'::text, now())"
        },
        "updated_at": {
          "type": "string",
          "format": "timestamp with time zone",
          "default": "timezone('utc'::text, now())"
        },
        "data": {
          "type": "string",
          "format": "jsonb"
        },
        "name": {
          "type": "string",
          "format": "text"
        },
        "owner_id": {
          "type": "integer",
          "format": "bigint",
          "description": "Note:\nThis is a Foreign Key to `hallo.id`.<fk table='hallo' column='id'/>"
        }
      },
      "type": "object"
    }
  },
  "parameters": {
    "preferParams": {
      "name": "Prefer",
      "description": "Preference",
      "required": false,
      "type": "string",
      "in": "header",
      "enum": [
        "params=single-object"
      ]
    },
    "preferReturn": {
      "name": "Prefer",
      "description": "Preference",
      "required": false,
      "type": "string",
      "in": "header",
      "enum": [
        "return=representation",
        "return=minimal",
        "return=none"
      ]
    },
    "preferCount": {
      "name": "Prefer",
      "description": "Preference",
      "required": false,
      "type": "string",
      "in": "header",
      "enum": [
        "count=none"
      ]
    },
    "select": {
      "name": "select",
      "description": "Filtering Columns",
      "required": false,
      "type": "string",
      "in": "query"
    },
    "on_conflict": {
      "name": "on_conflict",
      "description": "On Conflict",
      "required": false,
      "type": "string",
      "in": "query"
    },
    "order": {
      "name": "order",
      "description": "Ordering",
      "required": false,
      "type": "string",
      "in": "query"
    },
    "range": {
      "name": "Range",
      "description": "Limiting and Pagination",
      "required": false,
      "type": "string",
      "in": "header"
    },
    "rangeUnit": {
      "name": "Range-Unit",
      "description": "Limiting and Pagination",
      "required": false,
      "type": "string",
      "default": "items",
      "in": "header"
    },
    "offset": {
      "name": "offset",
      "description": "Limiting and Pagination",
      "required": false,
      "type": "string",
      "in": "query"
    },
    "limit": {
      "name": "limit",
      "description": "Limiting and Pagination",
      "required": false,
      "type": "string",
      "in": "query"
    },
    "body.hallo": {
      "name": "hallo",
      "description": "hallo",
      "required": false,
      "in": "body",
      "schema": {
        "$ref": "#/definitions/hallo"
      }
    },
    "rowFilter.hallo.id": {
      "name": "id",
      "required": false,
      "type": "string",
      "format": "bigint",
      "in": "query"
    },
    "rowFilter.hallo.created_at": {
      "name": "created_at",
      "required": false,
      "type": "string",
      "format": "timestamp with time zone",
      "in": "query"
    },
    "rowFilter.hallo.name": {
      "name": "name",
      "required": false,
      "type": "string",
      "format": "text",
      "in": "query"
    },
    "body.lol": {
      "name": "lol",
      "description": "lol",
      "required": false,
      "in": "body",
      "schema": {
        "$ref": "#/definitions/lol"
      }
    },
    "rowFilter.lol.id": {
      "name": "id",
      "required": false,
      "type": "string",
      "format": "bigint",
      "in": "query"
    },
    "rowFilter.lol.inserted_at": {
      "name": "inserted_at",
      "required": false,
      "type": "string",
      "format": "timestamp with time zone",
      "in": "query"
    },
    "rowFilter.lol.updated_at": {
      "name": "updated_at",
      "required": false,
      "type": "string",
      "format": "timestamp with time zone",
      "in": "query"
    },
    "rowFilter.lol.data": {
      "name": "data",
      "required": false,
      "type": "string",
      "format": "jsonb",
      "in": "query"
    },
    "rowFilter.lol.name": {
      "name": "name",
      "required": false,
      "type": "string",
      "format": "text",
      "in": "query"
    },
    "rowFilter.lol.owner_id": {
      "name": "owner_id",
      "required": false,
      "type": "string",
      "format": "bigint",
      "in": "query"
    }
  },
  "externalDocs": {
    "url": "https://postgrest.org/en/latest/api.html",
    "description": "PostgREST Documentation"
  }
}
0

There are 0 answers