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

Generate routes and variants from OpenApi document #384

Closed
javierbrea opened this issue Jul 28, 2022 · 0 comments · Fixed by #417
Closed

Generate routes and variants from OpenApi document #384

javierbrea opened this issue Jul 28, 2022 · 0 comments · Fixed by #417
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@javierbrea
Copy link
Member

It is desirable to support generating routes and variants automatically from an OpenApi document. The routes ids could be assigned using the method and path (${method}-${path}). Variants could be created using the OpenApi responses and their examples (${statusCode}-${exampleId}). For defining other ids, the x-mocks-server-id property could be used in the OpenApi document, and maybe it would be interesting also to provide a hook allowing to transform the proposed ids into another custom format.

For creating the routes and variants, it could be done using different approaches:

Javascript library

A JavaScript library exporting methods for generating the routes. Collections should be added manually. For example, in a routes folder this library would be used as in:

const { createRoutes } = require("@mocks-server/plugin-open-api");

module.exports = [
  createRoutes({
    baseUrl: "/api"
    document:  {} // Open API document
  })
];

This would allow to load OpenAPI documents from files, or to perform a request to load it from a remote server, etc. (Caution: Asyncs are not supported when loading files for the moment. Maybe the core should accept also exporting async functions apart from plain arrays when loading routes in order to allow users to generate routes from async processes)

Plugin

A plugin receiving some options could be at charge of automatically creating the routes from files in the mocks folder, or from urls. Its configuration could look like:

{
   plugins: {
    openApi: {
      createRoutes: [ // Supports using n OpenApi documents
         {
            baseUrl: "/api",
            file: "openApi/openapi.json" // relative to files.path, or absolute
         },
         {
            baseUrl: "/api",
            url: "https://api.my-project.com/openapi.json" // relative to files.path, or absolute
         },
         {
            baseUrl: "/api",
            document: { } // Define OpenApi document here directly
         }
      ]
    }
  }
}

Both approaches are compatible and could be available at the same time, because the plugin could export the method for creating the routes while it uses it internally.

@javierbrea javierbrea added the enhancement New feature or request label Jul 28, 2022
@javierbrea javierbrea added this to To do in mocks-server via automation Jul 28, 2022
@javierbrea javierbrea moved this from To do to In progress in mocks-server Aug 17, 2022
@javierbrea javierbrea self-assigned this Aug 17, 2022
javierbrea added a commit that referenced this issue Aug 19, 2022
@javierbrea javierbrea added this to the v3.11.0 milestone Aug 22, 2022
javierbrea added a commit that referenced this issue Aug 24, 2022
This was referenced Aug 25, 2022
mocks-server automation moved this from In progress to Done Aug 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
mocks-server
  
Done
Development

Successfully merging a pull request may close this issue.

1 participant