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

OpenAPI remote example failing due to stringified json #481

Open yarinsa opened 1 year ago

yarinsa commented 1 year ago

Describe the bug Reading trough the docs it seems like the mock server supposed to auto-return the example defined in the open api. I can see that mock server successfully found the routes. But I am getting 404.

To Reproduce I tried even the provided example (using remote github openapi.json) And I get the same result

Expected behavior

**Logs** 15:33:42:39 [verbose][plugins:openapi] Loading 7 routes from OpenAPI definitions found in folder '/Users/yarinsasson/Work/Code/product/packages/api-client/mocks/openapi' 15:33:42:39 [debug][mock:routes:load] Processing loaded routes 15:33:42:39 [debug][mock:routes] Creating logger namespace for route variant add-headers:enabled 15:33:42:39 [debug][mock:routes] Creating logger namespace for route variant listVersionsv2:200-json-foo 15:33:42:39 [debug][mock:routes] Creating logger namespace for route variant listVersionsv2:300-json-foo 15:33:42:39 [warn][alerts:300-json-foo] Variant with id '300-json-foo' in route with id 'listVersionsv2' is invalid: Invalid 'options' property:/body: type must be object. /body: type must be array. /body: oneOf must match exactly one schema in oneOf 15:33:42:39 [debug][mock:routes] Creating logger namespace for route variant getVersionDetailsv2:200-json-foo 15:33:42:39 [debug][mock:routes] Creating logger namespace for route variant getVersionDetailsv2:203-json-foo 15:33:42:39 [debug][mock:routes:load] Processed 5 route variants 15:33:42:39 [debug][mock:collections:load] Processing loaded collections 15:33:42:39 [verbose][mock] Trying to select collection 'undefined' 15:33:42:39 [warn][alerts:collections] Option 'mock.collections.selected' was not defined 15:33:42:39 [warn][alerts:collections] No collections found 15:33:42:39 [debug][plugins:openapi] Collections created from OpenAPI definitions: '[{"id":"api-2","from":null,"routes":["listVersionsv2:200-json-foo","getVersionDetailsv2:200-json-foo"]},{"id":"access-requests","from":null,"routes":[]}]' 15:33:42:39 [debug][plugins:openapi] Collection created from all OpenAPI definitions: '{"id":"openapi","from":null,"routes":["listVersionsv2:200-json-foo","getVersionDetailsv2:200-json-foo"]}' 15:33:42:39 [verbose][plugins:openapi] Loading 3 collections from OpenAPI definitions found in folder '/Users/yarinsasson/Work/Code/product/packages/api-client/mocks/openapi' 15:33:42:39 [debug][mock:routes:load] Processing loaded routes 15:33:42:40 [debug][mock:routes] Creating logger namespace for route variant add-headers:enabled 15:33:42:40 [debug][mock:routes] Creating logger namespace for route variant listVersionsv2:200-json-foo 15:33:42:40 [debug][mock:routes] Creating logger namespace for route variant listVersionsv2:300-json-foo 15:33:42:40 [warn][alerts:300-json-foo] Variant with id '300-json-foo' in route with id 'listVersionsv2' is invalid: Invalid 'options' property:/body: type must be object. /body: type must be array. /body: oneOf must match exactly one schema in oneOf 15:33:42:40 [debug][mock:routes] Creating logger namespace for route variant getVersionDetailsv2:200-json-foo 15:33:42:40 [debug][mock:routes] Creating logger namespace for route variant getVersionDetailsv2:203-json-foo 15:33:42:40 [debug][mock:routes:load] Processed 5 route variants 15:33:42:40 [debug][mock:collections:load] Processing loaded collections 15:33:42:40 [verbose][mock] Trying to select collection 'undefined' 15:33:42:40 [warn][alerts:collections] Option 'mock.collections.selected' was not defined. Selecting the first collection found 15:33:42:40 [info][mock] Selected collection: 'api-2' 15:33:42:40 [debug][plugins] Initializing plugin 'adminApi' 15:33:42:40 [debug][plugins] Initializing plugin 'inquirerCli' 15:33:42:40 [verbose][plugins] Initializated 2 plugins without errors 15:33:42:40 [debug][server] Configuring server 15:33:42:41 [debug][server] Registering custom routers in server 15:33:42:41 [verbose][server] Creating HTTP server 15:33:42:41 [info][server] Server started and listening at http://localhost:3100 15:33:42:41 [debug][files] Starting files watcher 15:33:42:42 [debug][plugins] Starting plugin 'adminApi' 15:33:42:42 [verbose][plugins:adminApi:server] Creating HTTP server 15:33:42:42 [info][plugins:adminApi:server] Server started and listening at http://localhost:3110 15:33:42:42 [debug][plugins] Starting plugin 'inquirerCli' ──────────────────────────────────────────────────────────── ⚠️ ALERTS ──────────────────────────────────────────────────────────── ‧ Warning: [mock:routes:load:listVersionsv2:variants:300-json-foo:validation] Variant with id '300-json-foo' in route with id 'listVersionsv2' is invalid: Invalid 'options' property:/body: type must be object. /body: type must be array. /body: oneOf must match exactly one schema in oneOf ‧ Warning: [mock:collections:selected] Option 'mock.collections.selected' was not defined. Selecting the first collection found ──────────────────────────────────────────────────────────── ℹ️ CURRENT SETTINGS ──────────────────────────────────────────────────────────── ‧ Server listening at: http://localhost:3100 ‧ Delay: 0 ‧ Current collection: api-2 ‧ Collections: 3 ‧ Routes: 3 ‧ Route variants: 5 ‧ Log level: debug ‧ Watch enabled: true ──────────────────────────────────────────────────────────── ↕️ ACTIONS ──────────────────────────────────────────────────────────── ? Select action: Select collection ──────────────────────────────────────────────────────────── ⚠️ ALERTS ──────────────────────────────────────────────────────────── ‧ Warning: [mock:routes:load:listVersionsv2:variants:300-json-foo:validation] Variant with id '300-json-foo' in route with id 'listVersionsv2' is invalid: Invalid 'options' property:/body: type must be object. /body: type must be array. /body: oneOf must match exactly one schema in oneOf ‧ Warning: [mock:collections:selected] Option 'mock.collections.selected' was not defined. Selecting the first collection found ──────────────────────────────────────────────────────────── ℹ️ CURRENT SETTINGS ──────────────────────────────────────────────────────────── ‧ Server listening at: http://localhost:3100 ‧ Delay: 0 ‧ Current collection: api-2 ‧ Collections: 3 ‧ Routes: 3 ‧ Route variants: 5 ‧ Log level: debug ‧ Watch enabled: true ──────────────────────────────────────────────────────────── ↕️ ACTIONS ──────────────────────────────────────────────────────────── ? Please choose collection api-2 ──────────────────────────────────────────────────────────── ⚠️ ALERTS ──────────────────────────────────────────────────────────── ‧ Warning: [mock:routes:load:listVersionsv2:variants:300-json-foo:validation] Variant with id '300-json-foo' in route with id 'listVersionsv2' is invalid: Invalid 'options' property:/body: type must be object. /body: type must be array. /body: oneOf must match exactly one schema in oneOf ──────────────────────────────────────────────────────────── ℹ️ CURRENT SETTINGS ──────────────────────────────────────────────────────────── ‧ Server listening at: http://localhost:3100 ‧ Delay: 0 ‧ Current collection: api-2 ‧ Collections: 3 ‧ Routes: 3 ‧ Route variants: 5 ‧ Log level: debug ‧ Watch enabled: true ──────────────────────────────────────────────────────────── ↕️ ACTIONS ──────────────────────────────────────────────────────────── ? Select action: (Use arrow keys) ❯ Select collection Use route variant Restore route variants Change delay Restart server Change log level Switch watch (Move up and down to reveal more choices) ──────────────────────────────────────────────────────────── ⚠️ ALERTS ──────────────────────────────────────────────────────────── ‧ Warning: [mock:routes:load:listVersionsv2:variants:300-json-foo:validation] Variant with id '300-json-foo' in route with id 'listVersionsv2' is invalid: Invalid 'options' property:/body: type must be object. /body: type must be array. /body: oneOf must match exactly one schema in oneOf ──────────────────────────────────────────────────────────── ℹ️ CURRENT SETTINGS ──────────────────────────────────────────────────────────── ‧ Server listening at: http://localhost:3100 ‧ Delay: 0 ‧ Current collection: api-2 ‧ Collections: 3 ‧ Routes: 3 ‧ Route variants: 5 ‧ Log level: debug ‧ Watch enabled: true ──────────────────────────────────────────────────────────── ↕️ ACTIONS ──────────────────────────────────────────────────────────── ? Select action: (Use arrow keys) ❯ Select collection Use route variant Restore route variants Change delay Restart server Change log level Switch watch (Move up and down to reveal more choices)

Operating system, Node.js an npm versions, or browser version (please complete the following information): MacOS M3 NODE.js 18.5 yarn 3.5.0 Chrome

javierbrea commented 11 months ago

Hi @yarinsa , The remote example fails to create the response for the 300 status code because it is defined in a string, which is something that the openapi plugin does not support, but the rest of routes are created successfully.

Wrong variant causing the route / to not be created in the remote example:

"300": {
            "description": "300 response",
            "content": {
              "application/json": {
                "examples": {
                  "foo": {
                    "value": "{\n \"versions\": [\n       {\n         \"status\": \"CURRENT\",\n         \"updated\": \"2011-01-21T11:33:21Z\",\n         \"id\": \"v2.0\",\n         \"links\": [\n             {\n                 \"href\": \"http://127.0.0.1:8774/v2/\",\n                 \"rel\": \"self\"\n             }\n         ]\n     },\n     {\n         \"status\": \"EXPERIMENTAL\",\n         \"updated\": \"2013-07-23T11:33:21Z\",\n         \"id\": \"v3.0\",\n         \"links\": [\n             {\n                 \"href\": \"http://127.0.0.1:8774/v3/\",\n                 \"rel\": \"self\"\n             }\n         ]\n     }\n ]\n}\n"
                  }
                }
              }
            }
          }

Response of http://localhost:3100/openapi/v2 using the remote example:

{
  "version": {
    "status": "CURRENT",
    "updated": "2011-01-21T11:33:21Z",
    "media-types": [
      {
        "base": "application/xml",
        "type": "application/vnd.openstack.compute+xml;version=2"
      },
      {
        "base": "application/json",
        "type": "application/vnd.openstack.compute+json;version=2"
      }
    ],
    "id": "v2.0",
    "links": [
      {
        "href": "http://127.0.0.1:8774/v2/",
        "rel": "self"
      },
      {
        "href": "http://docs.openstack.org/api/openstack-compute/2/os-compute-devguide-2.pdf",
        "type": "application/pdf",
        "rel": "describedby"
      },
      {
        "href": "http://docs.openstack.org/api/openstack-compute/2/wadl/os-compute-2.wadl",
        "type": "application/vnd.sun.wadl+xml",
        "rel": "describedby"
      },
      {
        "href": "http://docs.openstack.org/api/openstack-compute/2/wadl/os-compute-2.wadl",
        "type": "application/vnd.sun.wadl+xml",
        "rel": "describedby"
      }
    ]
  }
}

So, for the moment I will change the example in the docs to a completely supported openapi example, and I will also let this issue open for supporting json stringified examples.