mocks-server / main

Node.js mock server running live, interactive mocks in place of real APIs
https://www.mocks-server.org
Apache License 2.0
288 stars 16 forks source link

Custom ids function for OpenAPI definitions #427

Open javierbrea opened 2 years ago

javierbrea commented 2 years ago

Currently, it is possible to assign a custom id to routes and variants created from an OpenAPI document using "x-..." properties. But modifying the OpenAPI may not be an option sometimes. So, it is desirable to add a mechanism allowing to create custom ids using the OpenAPI properties. For example:

const openApiDefinition = {
    basePath: "/testing-api",
    ids: {
        variant: (openAPIVariantData) =>  {
             return `custom-id-${openAPIVariantData.exampleId}-${openAPIVariantData.mediaType}-${openAPIVariantData.code}`
        },
        route: (openAPIRouteData) =>  {
             return `custom-id-${openAPIRouteData.method}-${openAPIRouteData.path}`
        }
    },
    document: {
      $ref: "../documents/openapi.json"
    }
}
javierbrea commented 2 years ago

A similar mechanism could be also provided to customize data from examples, or even to generate data from schemas (related to #424 )