OpenAPI Parser Bundle Schemas

From sgcOpenAPI 2024.9.0 the parser has been improved with the following new features:

- Bundle Specification: if the specification is built with multiple schemas, the parser can bundle into a single specification file.

- Output Parser Parameters: when creating the pascal interface, the parameters used to import the specifications are written in the header of the pascal file.

- A new event has been created for the sgcOpenAPI Client, OnBeforeRequest, which can be used to customize the HTTP request before is sent to the server.

Bundle Specification

When OpenAPI or JSON Schema documents get massive or repetitive, the contents can be split across multiple documents (on the filesystem, URLs, in memory somewhere) and joined together $ref. These split up API descriptions can then be joined back together as one document, with $ref pointing to an internal location instead of an external location. This is called "bundling".

Now the sgcOpenAPI Parser supports bundling openAPI specifications. This is done automatically when importing a file.

Splitted OpenAPI Specification


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'        

Bundled Specification

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 

Output Parser Parameters

 The pascal file created from the openAPI specification now contains the parameters used to import the openAPI file. These parameters are created as comments in the top of the delphi file.

{ ***************************************************************************  
  sgcOpenAPI component                                                         
                                                                               
  written by eSeGeCe                                                           
  copyright © 2024                                                             
  Email : This email address is being protected from spambots. You need JavaScript enabled to view it.                                                     
  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 event

This event is called before the HTTP request is called. Allows to customize the Parameter names, Headers, security... Find below an example how to replace the name of some parameters.

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; 
×
Stay Informed

When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.

Google FCM HTTP v1 Delphi
sgcWebSockets 2024.8

Related Posts