mulesoft-labs / osprey-mock-service

Generate an API mock service from a RAML definition using Osprey
Other
111 stars 35 forks source link

`$ref` properties in json schemas not resolved? #5

Open schmkr opened 9 years ago

schmkr commented 9 years ago

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": "foo@bar.com"
        },
        "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 commented 9 years ago

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.

brs-mule commented 9 years ago

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 commented 9 years ago

Thanks Barbara, I'll give that a try.

dmalanij commented 8 years ago

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 commented 8 years ago

@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 commented 4 years ago

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