How to build multimodule gradle project with each module having its own Open API spec and the parent has all spec merge together on build

Viewed 168

I am designing one application, the application should merge OpenAPI-3 specification files into a single file. Consider the following open api v3 schema files color.yaml and book.yaml color.yaml

openapi: 3.0.1
info:
  title: OpenAPI definition
  version: v0
servers:
- url: http://localhost:8080
  description: Generated server url
paths:
  /api/color/{name}:
    get:
      tags:
      - color-controller
      operationId: getColor
      parameters:
      - name: name
        in: path
        required: true
        schema:
          type: string
      responses:
        "200":
          description: OK
          content:
            '*/*':
              schema:
                $ref: '#/components/schemas/Color'
components:
  schemas:
    Color:
      type: object
      properties:
        name:
          type: string
        red:
          type: integer
          format: int32
        green:
          type: integer
          format: int32
        blue:
          type: integer
          format: int32

book.yaml

openapi: 3.0.1
info:
  title: OpenAPI definition
  version: v0
servers:
- url: http://localhost:8080
  description: Generated server url
paths:
  /api/book/{name}:
    get:
      tags:
      - book-controller
      operationId: getBook
      parameters:
      - name: name
        in: path
        required: true
        schema:
          type: string
      responses:
        "200":
          description: OK
          content:
            '*/*':
              schema:
                $ref: '#/components/schemas/Book'
components:
  schemas:
    Book:
      type: object
      properties:
        name:
          type: string
        iban:
          type: string

</code>

The parent module should merge these files into a single master yaml spec file

**merged.yaml**



 openapi: 3.0.1
    info:
      title: My title
      version: 1.0.0-SNAPSHOT
    servers:
    - url: http://localhost:8080
      description: Generated server url
    paths:
      /api/book/{name}:
        get:
          tags:
          - book-controller
          operationId: getBook
          parameters:
          - name: name
            in: path
            required: true
            style: simple
            explode: false
            schema:
              type: string
          responses:
            "200":
              description: OK
              content:
                '*/*':
                  schema:
                    $ref: '#/components/schemas/Book'
      /api/color/{name}:
        get:
          tags:
          - color-controller
          operationId: getColor
          parameters:
          - name: name
            in: path
            required: true
            style: simple
            explode: false
            schema:
              type: string
          responses:
            "200":
              description: OK
              content:
                '*/*':
                  schema:
                    $ref: '#/components/schemas/Color'
    components:
      schemas:
        Book:
          type: object
          properties:
            name:
              type: string
            iban:
              type: string
        Color:
          type: object
          properties:
            name:
              type: string
            red:
              type: integer
              format: int32
            green:
              type: integer
              format: int32
            blue:
              type: integer
              format: int32

Also when I do changes to specific sub-module spec file it should reflect in parent spec file and should show in swagger-ui for testing.

1 Answers

Regarding the merging part of your question:

You can use APIMatic's API spec merge feature to first merge your specs and then transform the merged output into OpenAPI's format. Here are the steps:

  1. Structure your directory as follows:
dir\
  spec1\  
    color.yaml
  spec2\
    book.yaml
  APIMATIC-META.json
  1. A minimalistic APIMATIC-META.json can look like this to enable merging:
{
    "MergeConfiguration": {
        "MergedApiName": "My title",
        "MergeApis": true,
        "MergeSettings": {
          "SkipCodeGenValidation": true
       }
    }
}
  1. ZIP the directory, upload it and transform it via their website to OpenAPI v3 to get your merged output. Here is a link that provides step by step guide on uploading and performing a transformation: https://docs.apimatic.io/manage-apis/api-merging/#transforming-the-zipped-file

If you are looking to automate the process, APIMatic has an API too: https://www.apimatic.io/docs/api#/http/api-endpoints/transformation/transform-via-file

Related