question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Running spectral CLI on OAS with external schemas produces incorrect results

See original GitHub issue

Description

When running spectral on OpenAPI 3 specification with externally defined schemas, spectral gives errors for completely valid schemas and OpenAPI Specification.

PLEASE NOTE Running spectral on schemas folder alone gives no errors 🤷‍♂

To Reproduce

  1. Checkout this repo with branch https://github.com/huksley/maas-tsp-api/tree/spec-schemas
  2. Install packages npm install
  3. Build local schemas npm run build
  4. Run this CLI command npm run lint
  5. See errors:
/home/test/maas-tsp-api/schemas/core/components/bike-station.json
 34:3  error  parser  Duplicate key: required

/home/test/maas-tsp-api/schemas/core/components/state-log.json
 49:7  error  parser  Duplicate key: additionalProperties

/home/test/maas-tsp-api/schemas/core/customer.json
 104:7  error  parser  Duplicate key: required
 105:7  error  parser  Duplicate key: additionalProperties

/home/test/maas-tsp-api/schemas/core/components/fare.json
 31:5  error  parser  Duplicate key: type

/home/test/maas-tsp-api/schemas/core/components/terms.json
 120:11  error  parser  Duplicate key: type

/home/test/maas-tsp-api/schemas/core/modes/MODE_BICYCLE.json
 23:9  error  parser  Duplicate key: type

/home/test/maas-tsp-api/schemas/core/modes/MODE_SHARED_BICYCLE.json
 11:9  error  parser  Duplicate key: type

/home/test/maas-tsp-api/specs/booking.yml
 251:15  error  oas3-schema  /paths//bookings/options/get/responses/200 should have required property '$ref'

**Example case schemas/core/components/bike-station.json breakdown **

For following schema (when referenced from OAS):

{
  "$id": "http://maasglobal.com/core/components/bike-station.json",
  "description": "MaaS bike station schema",
  "type": "object",
  "properties": {
    "id": {
      "type": "string"
    },
    "name": {
      "type": "string"
    },
    "location": {
      "$ref": "../../../schemas/core/components/units-geo.json#/definitions/location"
    },
    "slots": {
      "type": "object",
      "properties": {
        "total": {
          "type": "number",
          "minimum": 0
        },
        "empty": {
          "type": "number",
          "minimum": 0
        },
        "bikes": {
          "type": "number",
          "minimum": 0
        }
      },
      "required": ["total", "empty", "bikes"]
    }
  },
  "required": ["id", "name", "location"]
}

produces following error:

 34:3  error  parser  Duplicate key: required

Expected behavior These errors should not be reported.

Environment (remove any that are not applicable):

  • Library version: 4.2.0
  • OS: MacOS 10.14.6
  • Node: 12.11.1

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
P0lipcommented, Dec 4, 2019

@huksley The issue has been fixed and will be released in the next version of Spectral. In the meantime, you can mitigate the issue by setting a resolution in your package.json.

"resolutions": {
  "@stoplight/json": "^3.2.2"
}

Make sure to reinstall dependencies once resolution is set. If you don’t use Yarn, you may need to use npm-force-resolutions. That said, the release is around, so you can just wait a tiny bit longer 🙂

Last but not least, make sure to remove the actual duplicate property in schemas/core/error.json 😉

1reaction
huksleycommented, Dec 10, 2019

Thanks @P0lip!

I done the changes specified and Schema errors are gone!!! However it still complains about oas3-schema error, is there something wrong with the specification?

It is in this branch/PR https://github.com/maasglobal/maas-tsp-api/pull/52

> maas-tsp-api@0.0.3 lint-spec /Users/user/src/maas-tsp-api
> node utils/adopt-schemas.js && cross-env NODE_OPTIONS=--max_old_space_size=4096 spectral lint specs/booking.yml

OpenAPI 3.x detected

/Users/user/src/maas-tsp-api/specs/booking.yml
 319:11  error  oas3-schema  /paths//bookings/options/get/parameters/4 should have required property '$ref'

✖ 1 problem (1 error, 0 warnings, 0 infos, 0 hints)

I took a look at parameters, they are all have defined type via schema/$ref

If I remove the mode parameter, which declared like this (and references this):

        - name: mode
          description: 'Transfer mode'
          in: query
          required: false
          schema:
            $ref: ../schemas/core/components/travel-mode.json

it starts to complain with this:

/Users/user/src/maas-tsp-api/specs/booking.yml
 337:15  error  oas3-schema  /paths//bookings/options/get/responses/200 should have required property '$ref'

which is again looks fine to me 🤔

      responses:
        '200':
          x-summary: Array of options
          description: |
            Available transport options matching the given query parameters. If no transport options are available; an empty array is returned.
          content:
            application/json:
              schema:
                $ref: ../schemas/tsp/booking-options-list/response.json
              examples:
                Taxi:
                  summary: Taxi
                  externalValue: '../examples/taxi/booking-options-response.json'

Kind of related to #403 🤷‍♂

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spectral CLI - Stoplight
Once Spectral is installed and you have a ruleset, run Spectral via the command-line: Powered by Stoplight.
Read more >
@stoplight/spectral - npm
A flexible JSON object linter with out of the box support for OpenAPI v2 and v3. Features. Create custom rules to lint any...
Read more >
RSt - River Thames Conditions - Environment Agency - GOV.UK
#forum Moontara technovations, Relai electrique schema? ... 328i convertible review 2011, Coaster run knotts berry farm, Wrong pronunciation vine, ...
Read more >
Untitled
Glock 22 review police, Rachel platten live acoustic. #Michel Ipn vs unam 2011, Marcos para fotos graduacion! Kylie glasgow hydro 2014, Dr coudreuse...
Read more >
Untitled
#garden 8 minutes in the morning review, Anonymous kkk list facebook, Browns stats vs seahawks, Cgtb ceara, Pony blog korea, Distrutto dal dolore...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found