0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

typespecファイルからopen api3を生成する

Last updated at Posted at 2024-10-21

gen openapi3 from typespec

0. Before do

% npm init -y
% mkdir openapi3
% tree . -L 2 -I node_modules
.
├── README.md
├── compose.yaml
├── dockerfile
├── openapi3
│   └── main.tsp
├── package-lock.json
└── package.json
compose.yaml
services:
  http_schema_container:
    container_name: http_schema_container
    build:
      context: .
      dockerfile: dockerfile
    tty: true
    restart: always
    volumes:
      - .:/usr/src/app
FROM node:23-alpine3.19

WORKDIR /usr/src/app
openapi3/main.tsp
import "@typespec/http";

using TypeSpec.Http;

model Pet {
  name: string;
  age: int32;
}

model Store {
  name: string;
  address: Address;
}

model Address {
  street: string;
  city: string;
}

@route("/pets")
interface Pets {
  list(@query filter: string): Pet[];
  create(@body pet: Pet): Pet;
  read(@path id: string): Pet;
}

@route("/stores")
interface Stores {
  list(@query filter: string): Store[];
  read(@path id: string): Store;
}

1. 依存関係の設定とconfig fileの作成

1-1. 依存関係の設定

tspの設定とopenapi3 emitterを利用できる状況にします。

% npm install -D @typespec/openapi3
% npm install @typespec/compiler
package.json
"scripts": {
    "cli:tsp": "tsp",
    "compile:openapi3": "tsp compile ./openapi3 --emit=@typespec/openapi3"
},
% npm run compile:openapi
> app@0.1.0 compile:openapi3
> tsp compile ./openapi3 --emit=@typespec/openapi3

TypeSpec compiler v0.61.2

Compilation completed successfully.

% tree . -L 2 -I node_modules
.
├── README.md
├── compose.yaml
├── dockerfile
├── openapi3
│   └── main.tsp
├── package-lock.json
├── package.json
└── tsp-output
    └── @typespec
        └── openapi3
            └── openapi.yaml
tsp-output/@typespec/openapi3/openapi.yaml
openapi: 3.0.0
info:
  title: (title)
  version: 0.0.0
tags: []
paths:
  /pets:
    get:
      operationId: Pets_list
      parameters:
        - name: filter
          in: query
          required: true
          schema:
            type: string
          explode: false
      responses:
        '200':
          description: The request has succeeded.
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Pet'
    post:
      operationId: Pets_create
      parameters: []
      responses:
        '200':
          description: The request has succeeded.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Pet'
  /pets/{id}:
    get:
      operationId: Pets_read
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
      responses:
        '200':
          description: The request has succeeded.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
  /stores:
    get:
      operationId: Stores_list
      parameters:
        - name: filter
          in: query
          required: true
          schema:
            type: string
          explode: false
      responses:
        '200':
          description: The request has succeeded.
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Store'
  /stores/{id}:
    get:
      operationId: Stores_read
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
      responses:
        '200':
          description: The request has succeeded.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Store'
components:
  schemas:
    Address:
      type: object
      required:
        - street
        - city
      properties:
        street:
          type: string
        city:
          type: string
    Pet:
      type: object
      required:
        - name
        - age
      properties:
        name:
          type: string
        age:
          type: integer
          format: int32
    Store:
      type: object
      required:
        - name
        - address
      properties:
        name:
          type: string
        address:
          $ref: '#/components/schemas/Address'

1-2. config fileの設定

% touch openapi3/tspconfig.yaml
openapi3/tspconfig.yaml
emit:
  - "@typespec/openapi3"
package.json
"scripts": {
    "compile:openapi3": "tsp compile ./openapi3"
  },
% npm run compile:openapi3

OpenAPI3 Emitter Usage
Configuration

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?