Skip to content

Commit

Permalink
Add basic discriminator spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Ladas committed May 16, 2019
1 parent c844e37 commit 10e9070
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 0 deletions.
109 changes: 109 additions & 0 deletions spec/data/petstore-with-discriminator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Petstore
description: A sample API that uses a petstore as an example to demonstrate features in the OpenAPI 3.0 specification
termsOfService: http://swagger.io/terms/
contact:
name: Swagger API Team
email: [email protected]
url: http://swagger.io
license:
name: Apache 2.0
url: https://www.apache.org/licenses/LICENSE-2.0.html
servers:
- url: http://petstore.swagger.io/api
paths:
/save_the_pets:
post:
description: Creates a new pet in the store. Duplicates are allowed
operationId: addPet
requestBody:
description: Pet to add to the store
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/PetBaskets'
responses:
'200':
description: pet response
content:
application/json:
schema:
type: object
components:
schemas:
PetBaskets:
type: object
properties:
baskets:
type: array
items:
anyOf:
- "$ref": "#/components/schemas/SquirrelBasket"
- "$ref": "#/components/schemas/CatBasket"
discriminator:
propertyName: name
mapping:
cats: "#/components/schemas/CatBasket"
squirrels: "#/components/schemas/SquirrelBasket"
SquirrelBasket:
type: object
required:
- name
properties:
name:
type: string
content:
type: array
items:
"$ref": "#/components/schemas/Squirrel"
Squirrel:
type: object
required:
- name
- nut_stock
properties:
name:
type: string
born_at:
format: date-time
nullable: true
type: string
description:
nullable: true
type: string
nut_stock:
nullable: true
type: integer
CatBasket:
type: object
required:
- name
properties:
name:
type: string
content:
type: array
items:
"$ref": "#/components/schemas/Cat"
Cat:
type: object
required:
- name
- milk_stock
properties:
name:
type: string
born_at:
format: date-time
nullable: true
type: string
description:
nullable: true
type: string
milk_stock:
nullable: true
type: integer

55 changes: 55 additions & 0 deletions spec/openapi_parser/schemas/discriminator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require_relative '../../spec_helper'

RSpec.describe OpenAPIParser::Schemas::RequestBody do
let(:root) { OpenAPIParser.parse(petstore_with_discriminator_schema, {}) }

describe 'discriminator' do
let(:content_type) { 'application/json' }
let(:http_method) { :post }
let(:request_path) { '/save_the_pets' }
let(:request_operation) { root.request_operation(http_method, request_path) }
let(:params) { {} }

it 'picks correct object based on mapping and succeeds' do
body = {
"baskets" => [
{
"name" => "cats",
"content" => [
{
"name" => "Mr. Cat",
"born_at" => "2019-05-16T11 =>37 =>02.160Z",
"description" => "Cat gentleman",
"milk_stock" => 10
}
]
},
]
}

request_operation.validate_request_body(content_type, body)
end

it 'picks correct object based on mapping and fails' do
body = {
"baskets" => [
{
"name" => "cats",
"content" => [
{
"name" => "Mr. Cat",
"born_at" => "2019-05-16T11 =>37 =>02.160Z",
"description" => "Cat gentleman",
"nut_stock" => 10 # passing squirrel attribute here, but discriminator still picks cats and fails
}
]
},
]
}
expect { request_operation.validate_request_body(content_type, body) }.to raise_error do |e|
expect(e.kind_of?(OpenAPIParser::NotExistRequiredKey)).to eq true
expect(e.message.start_with?("required parameters milk_stock not exist")).to eq true
end
end
end
end
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def petstore_schema
YAML.load_file('./spec/data/petstore-expanded.yaml')
end

def petstore_with_discriminator_schema
YAML.load_file('./spec/data/petstore-with-discriminator.yaml')
end

def build_validate_test_schema(new_properties)
b = YAML.load_file('./spec/data/validate_test.yaml')
obj = b['paths']['/validate_test']['post']['requestBody']['content']['application/json']['schema']['properties']
Expand Down

0 comments on commit 10e9070

Please sign in to comment.