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

Property rules skipped for $ref'd properties #2091

Open
jeepshop opened this issue Mar 15, 2022 · 5 comments
Open

Property rules skipped for $ref'd properties #2091

jeepshop opened this issue Mar 15, 2022 · 5 comments
Labels
json-refs everything JSON Refs resolving related t/bug Something isn't working triaged

Comments

@jeepshop
Copy link

Describe the bug
Some of my casing rules don't apply to property names if the property is a $ref: This is occurring within Stoplight Studio but it seems to be a spectral issue.

To Reproduce

  1. Given this OpenAPI/AsyncAPI document model '...'
{
  "title": "Batman",
  "type": "object",
  "properties": {
    "Id": {
      "type": "integer"
    },
    "aircraftTail": {
      "$ref": "./AircraftTail.json"
    },
    "aircraft": {
      "type": "string"
    },
    "LAC": {
      "type": "integer"
    }
  }
}
  1. With this ruleset
    "model-properties-pascal-case": {
      "description": "Model properties MUST be written in PascalCase",
      "message": "{{path}} should be PascalCase",
      "severity": "warn",
      "given": "$.properties[*]~",
      "then":{
        "function":"pattern",
        "functionOptions": {
          "match": "^[A-Z][a-zA-Z0-9]*$"
        }
      }
    }

Expected behavior
Both aircraftTail and aircraft should be tagged with a warning for not being PascalCased, however only aircraft is tagged. Any property that is a $ref, the property name isn't rule checked.

Screenshots
image
image

Environment (remove any that are not applicable):

  • Stoplight Studio 2.8.0-statble.7260
  • Windows 10

Additional context
This problem is reproducible in individual models as well as within the main document rules.

@P0lip
Copy link
Contributor

P0lip commented Mar 15, 2022

I suppose the rule itself is not skipped, but the error itself is attached to a different file and therefore it's not exposed in the Diagnostics pane in Studio.
It's less of an issue in the context of CLI since the error should still be logged, but in the case of Studio it's more apparent since we only show errors that are specific to a particular file.

@P0lip P0lip added the t/bug Something isn't working label Mar 15, 2022
@michaelcilibrasi
Copy link

@jeepshop jeepshop

Hi JeepShot, in the Expected behavior screenshots, which editor are you using that both marks the lines and pops up the rule ID?

@jeepshop
Copy link
Author

Hi JeepShot, in the Expected behavior screenshots, which editor are you using that both marks the lines and pops up the rule ID?

Stoplight Studio. Specifically in the Code view.

@P0lip P0lip added the json-refs everything JSON Refs resolving related label Mar 24, 2022
@canvural
Copy link

canvural commented Oct 5, 2023

I also hit an issue similar to this. But looks like now error reporting for $ref is working, but error is attached to to $ref itself instead of the property name like aircraftTail Which causes confusion and it is really hard to find where is the real error.

@mskonovalov
Copy link

hmm, I'm still not sure if it is a bug or not but...
to what @canvural mentioned: reporting works fine just the rule itself is not working as everyone expecting
E.g. here is the example:

{
  "$id": "/schemas/test",
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "description": "Test schema",
  "info": {
    "name": "TestSchema",
    "title": "Test Schema",
    "version": "2-0-0",
    "format": "jsonschema"
  },
  "properties": {
    "aaa": {
      "title": "aaa",
      "description": "The type",
      "type": "string"
    },
    "bbb": {
      "$ref": "./ref.schema.json"
    }
  },
  "required": ["type", "ref"]
}
{
  "$id": "/schemas/ref",
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "description": "Description",
  "title": "Ref",
  "info": {
    "name": "Ref",
    "title": "Title",
    "format": "jsonschema"
  },
  "properties": {
    "ccc": {
      "title": "Ccc",
      "description": "The version of the event",
      "exampleValues": ["1-0-1"],
      "type": "string",
      "metadata": {
        "personalData": false
      }
    }
  },
  "required": ["version"]
}

So if we try to figure out how the refs are getting expanded I guess it will be something like that (omitted all the data that is not needed)

{
  "properties": {
    "aaa": { "title": "aaa" },
    "bbb": {
      "title": "bbb",
      "properties": {
        "ccc": { "title": "ccc" }
      }
    }
  }
}

so when you try to apply JsonPath like in the Op's example $.properties[*].title
it will actually return the following

["aaa", "bbb"]

Why not "ccc"? Because this JsonPath will match only top level properties, not the nested inside "bbb". (you can check it in online JsonPath editor)

To fix this issue you need to use recursive operator .. So in that case you jsonPath will become $..properties[*].title and this will match ["aaa", "bbb", "ccc"]

Hope that helps to somebody who bump their head at this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
json-refs everything JSON Refs resolving related t/bug Something isn't working triaged
Projects
None yet
Development

No branches or pull requests

6 participants