OpenAPI パーサーによるスキーマのバンドル

· 機能

sgcOpenAPI 2024.9.0 より、パーサーに以下の新機能が追加されました:

- バンドル仕様:仕様が複数のスキーマで構成されている場合、パーサーが 1 つの仕様ファイルにバンドルできます。

- 出力パーサーパラメーター:Pascal インターフェイスを生成する際、仕様のインポートに使用したパラメーターが Pascal ファイルのヘッダーにコメントとして書き出されます。

- sgcOpenAPI クライアントに新しいイベント OnBeforeRequest が追加されました。サーバーへ送信する前に HTTP リクエストをカスタマイズするために使用できます。

バンドル仕様

OpenAPI や JSON スキーマドキュメントが大規模になったり繰り返しが多くなったりすると、複数のドキュメント(ファイルシステム・URL・メモリ上など)に分割して $ref で結合できます。分割された API 記述は、$ref が外部ロケーションではなく内部ロケーションを指すように 1 つのドキュメントにまとめ直すことができます。これを「バンドリング」と呼びます。

sgcOpenAPI パーサーは OpenAPI 仕様のバンドリングをサポートするようになりました。ファイルをインポートする際に自動的に行われます。

分割された OpenAPI 仕様


openapi: 3.0.0
info:
  title: My API
  version: 1.0.0
paths:
  /things:
    get:
      responses: 
        '200':
          description: 'OK'
          content:
            application/json:
              schema:
                properties:
                  data:
                    type: array
                    items:
                      $ref: './schemas/thing.yaml'
  /things/{id}:
    get:
      parameters: 
        - name: id 
          in: path
          required: true
          schema: 
            type: string
            format: uuid
      responses: 
        '200':
          description: 'OK'
          content:
            application/json:
              schema:
                $ref: './schemas/thing.yaml'        

バンドルされた仕様

openapi: 3.0.0
info:
  title: My API
  version: 1.0.0
paths:
  /things:
    get:
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                properties:
                  data:
                    type: array
                    items:
                      $ref: '#/paths/~1things~1%7Bid%7D/get/responses/200/content/application~1json/schema'
  '/things/{id}':
    get:
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
            format: uuid
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  id:
                    type: string
                    format: uuid
                  name:
                    type: string
                  type:
                    type: string
                    enum:
                      - type1
                      - type2 

出力パーサーパラメーター

OpenAPI 仕様から生成された Pascal ファイルに、OpenAPI ファイルのインポートに使用したパラメーターが含まれるようになりました。これらのパラメーターは Delphi ファイルの先頭にコメントとして記述されます。

{ ***************************************************************************  
  sgcOpenAPI component                                                         
  written by eSeGeCe                                                           
  copyright © 2024                                                             
  Email : info@esegece.com
  Web : http://www.esegece.com                                                 
  Source: D:\Downloads\ICAR-ADE-1\url-schemes\registrationURLScheme.json
  Parsed At: 2024-10-02 10:12:18
  Options: [Generate Classes] [Enable Classes] [Documentation]
  Authentication: Token
  Method Name: OperationId
  Base URL: https://spec.openapi.com
  *************************************************************************** } 

OnBeforeRequest イベント

このイベントは HTTP リクエストが送信される前に呼び出されます。パラメーター名・ヘッダー・セキュリティなどをカスタマイズできます。以下はパラメーター名を置き換えるサンプルです。

procedure OnBeforeRequestEvent(Sender: TObject; const aRequest: TsgcOpenAPIRequest);
var
  i: Integer;
  oParameter: TsgcOpenAPIParameter;
begin
  for i := 0 to aRequest.Parameters.Count - 1 do
  begin
    oParameter := aRequest.Parameters[i];
    if oParameter._Name = 'meta-modified-from' then
      oParameter._name := 'eventDateTime-from';
    if oParameter._Name = 'meta-modified-to' then
      oParameter._name := 'eventDateTime-to';
  end;
end;