Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

$ref properties in json schemas not resolved? #5

Open
schmkr opened this issue May 19, 2015 · 6 comments
Open

$ref properties in json schemas not resolved? #5

schmkr opened this issue May 19, 2015 · 6 comments

Comments

@schmkr
Copy link

schmkr commented May 19, 2015

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!

@blakeembrey
Copy link
Contributor

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

@brs-mule
Copy link

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?

@schmkr
Copy link
Author

schmkr commented May 20, 2015

Thanks Barbara, I'll give that a try.

@blakeembrey blakeembrey self-assigned this Jul 9, 2015
@dmalanij
Copy link

dmalanij commented Feb 4, 2016

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

@blakeembrey
Copy link
Contributor

@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).

@SockeThe2nd
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants