Skip to main content

How do I add a new OpenAPI Schema?

Objective: This article explains how to add a new OpenAPI Schema and export it to the client.

Steps:

  1. Add a new schema object to the relevant directory:
    • modules/<module>/contracts/api/schema/requests/
    • modules/<module>/contracts/api/schema/responses/
  2. Add the schema in the schemas object in the OpenAPI Document definition.
    • modules/<module>/contracts/api/document.ts

Example:

// modules/<module>/contracts/api/schema/requests/...
// or
// modules/<module>/contracts/api/schema/responses/...

export const MySchema: OpenAPIV3.SchemaObject = {
type: 'object',
required: ['name', 'price'], // Forces properties to be not-undefinable.
nullable: false,
properties: {
name: { type: 'string' },
price: { type: 'number' }
}
}
// modules/<module>/contracts/api/document.ts

import { OpenAPIV3 } from 'openapi-types'
import * as paths from './paths/index.js'
import * as schema from './schema/index.js'
import { CommonOpenApi } from '@hectare/platform.components.common'

export const compose = (): OpenAPIV3.Document => {
const document: OpenAPIV3.Document = {
...
components: {
..
schemas: {
...
// This adds the schema to the FE client:
MySchema: schema.responses.MySchema
}
}
}
return document
}

Additional Resources: