Skip to content

Conversation

@ekorenblum-simtlix
Copy link
Contributor

@ekorenblum-simtlix ekorenblum-simtlix commented Apr 30, 2024

The OpenAPI generator module will now accept @example tags in the JSDoc, which will translate into OpenAPI examples.
They may be used in the route definition to declare a response example, or in a schema definition to declare a schema usage example

Usage:

Schema definition

Given the following JSDoc schema definition

/**
 * A variable with example
 * 
 * @example foo
 */

The resulting YAML declaration will be

summary: A variable with example
tags:
  example:
    test: foo

Endpoint definition

Given the following endpoint definition

import * as t from 'io-ts';
import * as h from '@api-ts/io-ts-http';

/**
 * A simple route
 *
 * @operationId api.v1.test
 * @tag Test Routes
 * @example {
 *   "test": "bar"
 * }
 */
export const route = h.httpRoute({
  path: '/foo',
  method: 'GET',
  request: h.httpRequest({}),
  response: {
    200: {
      test: t.string
    }
  },
});

The resulting YAML declaration will be

openapi: 3.0.3
info:
  title: Test
  version: 1.0.0
paths:
  "/foo":
    get:
      summary: A simple route
      operationId: api.v1.test
      tags:
      - Test Routes
      parameters: []
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  test:
                    type: string
                required:
                - test
              example:
                test: bar
components:
  schemas: {}

@ekorenblum-simtlix ekorenblum-simtlix requested a review from a team as a code owner April 30, 2024 16:31
Copy link
Contributor

@bitgopatmcl bitgopatmcl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please re-word the commit message to use feat(openapi-generator) rather than feature(openapi-generator) since the latter won't be picked up by semantic-release.

summary?: string;
description?: string;
tags?: Record<string, string>;
tags?: Record<string, string> & { example?: unknown };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically this is unsound since example is a possible key of the record type so the final type should be the intersection of string & unknown which is just string

image

However, the compiler does treat this in the intended manner of "example could be anything, all other properties are strings". If you want the type to be more accurate you could write Record<Exclude<string, 'example'>, string> & { example?: unknown } but it doesn't appear to make a difference in practice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I changed it in the declaration as requested

*
* @example {
* "test": "foo"
* }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test for tags that come after a multi-line example?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added!

@ekorenblum-simtlix ekorenblum-simtlix changed the title feature(openapi-generator): take and parse @example tags on JSDoc feat(openapi-generator): take and parse @example tags on JSDoc May 2, 2024
@ekorenblum-simtlix ekorenblum-simtlix force-pushed the DX-338-@example-tag-in-generator branch from d74ee90 to 921e204 Compare May 2, 2024 16:43
@ekorenblum-simtlix ekorenblum-simtlix force-pushed the DX-338-@example-tag-in-generator branch from 921e204 to 9da14c6 Compare May 2, 2024 16:44
@bitgopatmcl bitgopatmcl merged commit 9af30ad into master May 2, 2024
@bitgopatmcl bitgopatmcl deleted the DX-338-@example-tag-in-generator branch May 2, 2024 20:14
@github-actions
Copy link

github-actions bot commented May 2, 2024

🎉 This PR is included in version @api-ts/openapi-generator@3.6.0 🎉

The release is available on npm package (@latest dist-tag)

Your semantic-release bot 📦🚀

@github-actions
Copy link

github-actions bot commented Jun 4, 2024

🎉 This PR is included in version @api-ts/express-wrapper@1.0.26 🎉

The release is available on npm package (@latest dist-tag)

Your semantic-release bot 📦🚀

@github-actions
Copy link

github-actions bot commented Jun 4, 2024

🎉 This PR is included in version @api-ts/superagent-wrapper@1.2.1 🎉

The release is available on npm package (@latest dist-tag)

Your semantic-release bot 📦🚀

@github-actions
Copy link

github-actions bot commented Jun 4, 2024

🎉 This PR is included in version @api-ts/typed-express-router@1.1.6 🎉

The release is available on npm package (@latest dist-tag)

Your semantic-release bot 📦🚀

@github-actions
Copy link

github-actions bot commented Aug 5, 2024

🎉 This PR is included in version @api-ts/io-ts-http@3.1.0 🎉

The release is available on npm package (@latest dist-tag)

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants