Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated Swagger spec enhancements #984

Open
2 tasks done
mastersilv3r opened this issue Feb 29, 2024 · 0 comments
Open
2 tasks done

Generated Swagger spec enhancements #984

mastersilv3r opened this issue Feb 29, 2024 · 0 comments
Assignees
Labels
current current sprint feature new feature

Comments

@mastersilv3r
Copy link
Contributor

mastersilv3r commented Feb 29, 2024

The Problem

Currently the swagger spec generated from events does not create tags & operationid keys. Because of no tags the Swagger UI doesn't bring organised APIs. Because of no operationId, it is difficult to generate client for a swagger spec.

The Solution

Generate Swagger UI with tags and operationId

How to solve

  • For tags: either developer gives tags array in schema of event or calculate it from the id of the file. If none of this is present, then use ${method}_${endpoint} remove / from endpoint

  • For operationId: developer can give operationId, or id in the event shema. If none of this is present, then use the summary of the event to generate the id

In yamlLoader.ts add tags in every event

const eventFileId = file
      .replace(new RegExp(`.*?\/${basePath}\/`), '')
      .replace(/\//g, '.')
      .replace(/\.(yaml|yml)/i, '')
      .replace(/\.index$/, '');
    
  if (basePath === 'events') {
      for (let eventKey of Object.keys(module)) {
        module[eventKey].tags = module[eventKey].tags || [eventFileId];
      }
    }

In core/router/Swagger.ts

//For every event calculate operationId and tags
    apiEndPoint = apiEndPoint.replace(/:([^\/]+)/g, '{$1}'); //In Godspeed we take :path_param. OAS3 takes {path_param}


// Add in every event schema

tags: [
        eventSchema.tags
      ],
           operationId: eventSchema.operationId || eventSchema.id || eventSchema.summary?.replace(' ', '_') || `${method}_${apiEndPoint}`.replace(/\//g, '_'),

Test Cases

Generated Swagger should have endpoints like this..

"paths": {
        "/lending_service_db/loanapplication/{id}": {
            "get": {
                "summary": "Fetch LoanApplication",
                "description": "Fetch LoanApplication from database",
                "tags": [
                    
                        "lending_service_db.loanapplication"
                    
                ],
                "operationId": "Fetch_LoanApplication",
                "parameters": [
                    {
                        "name": "id",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "string"
                        }
                    }
                ],
                "responses": {
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object"
                            }
                        }
                    }
                },
                "security": [
                    {
                        "bearerAuth": []
                    }
                ]
            },
    ....

UI will look like this

image

@mastersilv3r mastersilv3r added feature new feature current current sprint labels Feb 29, 2024
@mastersilv3r mastersilv3r self-assigned this Feb 29, 2024
@mastersilv3r mastersilv3r changed the title Published Swagger spec enhancements Generated Swagger spec enhancements Feb 29, 2024
@kushal023 kushal023 assigned xCodeMuse and unassigned mastersilv3r Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
current current sprint feature new feature
Projects
None yet
Development

No branches or pull requests

2 participants