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

Test for embedded definitions (using single Swagger/OpenAPI file) #80

Open
Ajaxy opened this issue May 3, 2018 · 4 comments
Open

Test for embedded definitions (using single Swagger/OpenAPI file) #80

Ajaxy opened this issue May 3, 2018 · 4 comments

Comments

@Ajaxy
Copy link

Ajaxy commented May 3, 2018

I have a JSON file with embedded schemas in the definitions object. Is it possible to test for those schemas straight with match_json_schema()?

Neither match_json_schema('openapi#/definitions/SomeSchema') or match_json_schema('openapi.json#/definitions/SomeSchema') are seem to work, so now I have to manually create separate files for each schema of my definitions before I run tests.

@seanpdoyle
Copy link
Collaborator

@Ajaxy could you share your schema definitions and the JSON you're trying to validate against them?

@Ajaxy
Copy link
Author

Ajaxy commented May 7, 2018

It's just a regular swagger.json file:

{
  "swagger": "2.0",
  "info": {
    "title": "My API 1.0",
    "description": "...",
    "version": "0.0.1"
  },
  "host": "...",
  "basePath": "/v1",
  "schemes": [
    "https"
  ],
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "paths": {
    /* ... */
  },
  "tags": [
    /* ... */
  ],
  "definitions": {
    "Clinic": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        },
        "address": {
          "type": "string"
        },
        "latitude": {
          "type": "number"
        },
        "longitude": {
          "type": "number"
        }
      },
      "required": [
        "name",
        "address",
        "latitude",
        "longitude"
      ]
    },
    /* ... */
  }
}

So I'm to trying to check it like this:

expect(result[:clinics][0]).to match_json_schema('swagger.json#/Clinic')

@barnaclebarnes
Copy link

I would love to be able to use my OpenAPI schema as the base file for validations. @seanpdoyle Any update on if this is possible? For example for the attached file I want to match the response from the /api/frontend_v2/collections endpoint to the following schema in the document:

paths/"/api/frontend_v2/collections"/get/responses/200/content/"application/json"/schema

write_api.json.txt

@Ajaxy
Copy link
Author

Ajaxy commented Aug 16, 2018

@barnaclebarnes I was able to do this with this code:

require 'json'
require 'json_matchers/rspec'

JsonMatchers.schema_root = 'spec/schemas'

file = File.read 'spec/schemas/swagger.json'
swagger = JSON.parse(file, symbolize_names: true)

# Fix for json_matchers single-file restriction
swagger[:definitions].keys.each do |key|
  File.open("spec/schemas/#{key}.json", 'w') do |f|
    f.write(JSON.pretty_generate({
      '$ref': "swagger.json#/definitions/#{key}"
    }))
  end
end

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

No branches or pull requests

3 participants