osprey-mock-service icon indicating copy to clipboard operation
osprey-mock-service copied to clipboard

`$ref` properties in json schemas not resolved?

Open schmkr opened this issue 9 years ago • 6 comments

Hi,

We're using this project for mocking api calls and we've set up json schemas with references to definitions in another file, like so:

schemas/authentication/login.json

{
  "$schema": "http://json-schema.org/draft-04/schema",
  "type": "object",
  "properties": {
      "email": {
          "$ref" : "../definitions.json#/definitions/emailaddress"
      },
      "password": {
          "$ref" : "../definitions.json#/definitions/password"
      }
  },
  "required": ["email","password"]
}

schemas/definitions.json

{
    "$schema": "http://json-schema.org/draft-04/schema",
    "id": "definitions",
    "title" : "Definitions",
    "description": "Additional typ defintions for our json schemas",
    "definitions" : {
        "emailaddress" : {
            "id" : "#email",
            "title": "Email",
            "description": "This is a definition for an email type",
            "type": "string",
            "pattern": "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$",
            "example": "[email protected]"
        },
        "password" : {
            "id" : "#password",
            "title" : "Password",
            "type": "string",
            "example": "aBcdeF12!"
        }
    }
}

When we run the osprey-mock-service these refered definitions seem not to do anything with the validation, whereas when we inline the type and pattern from the definitions.json in the login.json the validation does happen.

Is there a way to make the $ref property work for the schemas?

Thanks!

schmkr avatar May 19 '15 13:05 schmkr

Unfortunately this has been a long running issue with the JavaScript parser (https://github.com/raml-org/raml-js-parser/issues/115) and there's currently no solution available. This is something will definitely be fixed in future versions of the RAML specification itself.

blakeembrey avatar May 19 '15 14:05 blakeembrey

Hi Alwin.

I have found that the most reliable way to make use of the $ref element (which, as Blake says, is not directly supported in RAML) is to use a single schema for all elements and to add a unique "id" element to each schema element. This appears to be the only guarantee currently of unambiguous addressing.

Is this a possibility for you?

brs-mule avatar May 19 '15 14:05 brs-mule

Thanks Barbara, I'll give that a try.

schmkr avatar May 20 '15 06:05 schmkr

I'm having a similar problem, when using a reference to the schema with the name. Assume you have the schemas included as follows

- player: !include schemas/player-schema.json
- team: !include schemas/team-schema.json

Then in player you have defined a property

    "currentTeam": {
      "$ref": "team",
      "description": "Team the player is playing right now"
    },

When running osprey-mock-service I get: Unable to compile JSON schema for post /player: can't resolve reference team from id # The problem is that we work with quite large RAML definitions and we definitely need to keep the schemas in different files. Any idea how this can be adressed?

EDIT 2015-02-08: just figured out this is only happening when the schema is in the body for the request. Therefore affects mainly POST and PUT, when using a schema with a reference in the response, seems to be working OK

dmalanij avatar Feb 04 '16 19:02 dmalanij

@dmalanij The $ref implementation is specific to JSON schema. It doesn't resolve to RAML schemas, and doing so would be a change in the current specification (though, probably worth it honestly). Unfortunately it's impossible to really use $ref property because of the limitations in the RAML 0.8 specification such as how to resolve JSON references (information which is currently lost from the parser output).

blakeembrey avatar Feb 09 '16 03:02 blakeembrey

Is there any update on this? This is currently the only issue I have with the package.

SockeThe2nd avatar Jan 08 '20 14:01 SockeThe2nd