From 5e0e44b436e49ac7fb18aa3725661f50f7bc10aa Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Sat, 29 Oct 2016 22:58:31 +0800 Subject: [PATCH] supercharge the schemas (#874) * fix map schema - woot it works * update other schemas to include embeddable attrs * update current user schema/examples --- app/controllers/api/v2/users_controller.rb | 2 +- app/serializers/api/v2/user_serializer.rb | 4 +- doc/api/examples/current_user.json | 3 +- doc/api/schemas/_current_user.json | 34 +++++++++++ doc/api/schemas/_map.json | 71 ++++++++++++++++++++++ doc/api/schemas/_mapping.json | 20 ++++++ doc/api/schemas/_synapse.json | 29 +++++++++ doc/api/schemas/_topic.json | 20 ++++++ doc/api/schemas/_user.json | 3 - doc/api/schemas/current_user.json | 12 ++++ spec/api/v2/maps_api_spec.rb | 5 ++ spec/api/v2/users_api_spec.rb | 18 ++++++ 12 files changed, 215 insertions(+), 6 deletions(-) create mode 100644 doc/api/schemas/_current_user.json create mode 100644 doc/api/schemas/current_user.json diff --git a/app/controllers/api/v2/users_controller.rb b/app/controllers/api/v2/users_controller.rb index b4b83e3f..3f60c410 100644 --- a/app/controllers/api/v2/users_controller.rb +++ b/app/controllers/api/v2/users_controller.rb @@ -17,7 +17,7 @@ module Api # only ask serializer to return is_admin field if we're on the # current_user action def default_scope - super.merge(show_is_admin: action_name == 'current') + super.merge(show_full_user: action_name == 'current') end end end diff --git a/app/serializers/api/v2/user_serializer.rb b/app/serializers/api/v2/user_serializer.rb index c3b0c3fe..7f18f02b 100644 --- a/app/serializers/api/v2/user_serializer.rb +++ b/app/serializers/api/v2/user_serializer.rb @@ -8,7 +8,9 @@ module Api :generation attribute :is_admin, - if: -> { scope[:show_is_admin] && scope[:current_user] == object } + if: -> { scope[:show_full_user] && scope[:current_user] == object } + attribute :email, + if: -> { scope[:show_full_user] && scope[:current_user] == object } def avatar object.image.url(:sixtyfour) diff --git a/doc/api/examples/current_user.json b/doc/api/examples/current_user.json index 83476006..78018884 100644 --- a/doc/api/examples/current_user.json +++ b/doc/api/examples/current_user.json @@ -4,6 +4,7 @@ "name": "user", "avatar": "https://s3.amazonaws.com/metamaps-assets/site/user.png", "generation": 0, - "is_admin": false + "is_admin": false, + "email": "user@example.com" } } diff --git a/doc/api/schemas/_current_user.json b/doc/api/schemas/_current_user.json new file mode 100644 index 00000000..94464ba8 --- /dev/null +++ b/doc/api/schemas/_current_user.json @@ -0,0 +1,34 @@ +{ + "name": "User", + "type": "object", + "properties": { + "id": { + "$ref": "_id.json" + }, + "name": { + "type": "string" + }, + "avatar": { + "format": "uri", + "type": "string" + }, + "generation": { + "type": "integer", + "minimum": 0 + }, + "is_admin": { + "type": "boolean" + }, + "email": { + "type": "string" + } + }, + "required": [ + "id", + "name", + "avatar", + "generation", + "is_admin", + "email" + ] +} diff --git a/doc/api/schemas/_map.json b/doc/api/schemas/_map.json index 76586914..d8d9c138 100644 --- a/doc/api/schemas/_map.json +++ b/doc/api/schemas/_map.json @@ -30,35 +30,68 @@ "user_id": { "$ref": "_id.json" }, + "user": { + "$ref": "_user.json" + }, "topic_ids": { "type": "array", "items": { "$ref": "_id.json" } }, + "topics": { + "type": "array", + "items": { + "$ref": "_topic.json" + } + }, "synapse_ids": { "type": "array", "items": { "$ref": "_id.json" } }, + "synapses": { + "type": "array", + "items": { + "$ref": "_synapse.json" + } + }, "mapping_ids": { "type": "array", "items": { "$ref": "_id.json" } }, + "mappings": { + "type": "array", + "items": { + "$ref": "_mapping.json" + } + }, "contributor_ids": { "type": "array", "items": { "$ref": "_id.json" } }, + "contributors": { + "type": "array", + "items": { + "$ref": "_user.json" + } + }, "collaborator_ids": { "type": "array", "items": { "$ref": "_id.json" } + }, + "collaborators": { + "type": "array", + "items": { + "$ref": "_user.json" + } } }, "required": [ @@ -70,5 +103,43 @@ "starred", "created_at", "updated_at" + ], + "allOf": [ + { + "oneOf": [ + { "required": [ "user_id" ] }, + { "required": [ "user" ] } + ] + }, + { + "oneOf": [ + { "required": [ "topic_ids" ] }, + { "required": [ "topics" ] } + ] + }, + { + "oneOf": [ + { "required": [ "synapse_ids" ] }, + { "required": [ "synapses" ] } + ] + }, + { + "oneOf": [ + { "required": [ "mapping_ids" ] }, + { "required": [ "mappings" ] } + ] + }, + { + "oneOf": [ + { "required": [ "contributor_ids" ] }, + { "required": [ "contributors" ] } + ] + }, + { + "oneOf": [ + { "required": [ "collaborator_ids" ] }, + { "required": [ "collaborators" ] } + ] + } ] } diff --git a/doc/api/schemas/_mapping.json b/doc/api/schemas/_mapping.json index 5a3b06a6..8789c5ec 100644 --- a/doc/api/schemas/_mapping.json +++ b/doc/api/schemas/_mapping.json @@ -27,8 +27,14 @@ "map_id": { "$ref": "_id.json" }, + "map": { + "$ref": "_map.json" + }, "user_id": { "$ref": "_id.json" + }, + "user": { + "$ref": "_user.json" } }, "required": [ @@ -37,5 +43,19 @@ "mappable_type", "created_at", "updated_at" + ], + "allOf": [ + { + "oneOf": [ + { "required": [ "map_id" ] }, + { "required": [ "map" ] } + ] + }, + { + "oneOf": [ + { "required": [ "user_id" ] }, + { "required": [ "user" ] } + ] + } ] } diff --git a/doc/api/schemas/_synapse.json b/doc/api/schemas/_synapse.json index dea238e2..6c323225 100644 --- a/doc/api/schemas/_synapse.json +++ b/doc/api/schemas/_synapse.json @@ -24,11 +24,20 @@ "topic1_id": { "$ref": "_id.json" }, + "topic1": { + "$ref": "_topic.json" + }, "topic2_id": { "$ref": "_id.json" }, + "topic2": { + "$ref": "_topic.json" + }, "user_id": { "$ref": "_id.json" + }, + "user": { + "$ref": "_user.json" } }, "required": [ @@ -38,5 +47,25 @@ "permission", "created_at", "updated_at" + ], + "allOf": [ + { + "oneOf": [ + { "required": [ "topic1_id" ] }, + { "required": [ "topic1" ] } + ] + }, + { + "oneOf": [ + { "required": [ "topic2_id" ] }, + { "required": [ "topic2" ] } + ] + }, + { + "oneOf": [ + { "required": [ "user_id" ] }, + { "required": [ "user" ] } + ] + } ] } diff --git a/doc/api/schemas/_topic.json b/doc/api/schemas/_topic.json index e9ccf67b..8f47fcc4 100644 --- a/doc/api/schemas/_topic.json +++ b/doc/api/schemas/_topic.json @@ -27,8 +27,14 @@ "user_id": { "$ref": "_id.json" }, + "user": { + "$ref": "_user.json" + }, "metacode_id": { "$ref": "_id.json" + }, + "metacode": { + "$ref": "_metacode.json" } }, "required": [ @@ -39,5 +45,19 @@ "permission", "created_at", "updated_at" + ], + "allOf": [ + { + "oneOf": [ + { "required": [ "user_id" ] }, + { "required": [ "user" ] } + ] + }, + { + "oneOf": [ + { "required": [ "metacode_id" ] }, + { "required": [ "metacode" ] } + ] + } ] } diff --git a/doc/api/schemas/_user.json b/doc/api/schemas/_user.json index ee2ef14f..f66d5508 100644 --- a/doc/api/schemas/_user.json +++ b/doc/api/schemas/_user.json @@ -15,9 +15,6 @@ "generation": { "type": "integer", "minimum": 0 - }, - "is_admin": { - "type": "boolean" } }, "required": [ diff --git a/doc/api/schemas/current_user.json b/doc/api/schemas/current_user.json new file mode 100644 index 00000000..e8260bcb --- /dev/null +++ b/doc/api/schemas/current_user.json @@ -0,0 +1,12 @@ +{ + "name": "Current User Envelope", + "type": "object", + "properties": { + "data": { + "$ref": "_current_user.json" + } + }, + "required": [ + "data" + ] +} diff --git a/spec/api/v2/maps_api_spec.rb b/spec/api/v2/maps_api_spec.rb index 5f5e9b83..a7edeef2 100644 --- a/spec/api/v2/maps_api_spec.rb +++ b/spec/api/v2/maps_api_spec.rb @@ -78,6 +78,7 @@ RSpec.describe 'maps API', type: :request do context 'RAML example' do let(:resource) { get_json_example(:map) } let(:collection) { get_json_example(:maps) } + let(:starred) { get_json_example(:map_starred) } it 'resource matches schema' do expect(resource).to match_json_schema(:map) @@ -86,5 +87,9 @@ RSpec.describe 'maps API', type: :request do it 'collection matches schema' do expect(collection).to match_json_schema(:maps) end + + it 'starred resource matches schema' do + expect(starred).to match_json_schema(:map) + end end end diff --git a/spec/api/v2/users_api_spec.rb b/spec/api/v2/users_api_spec.rb index 70e22c2f..279224f3 100644 --- a/spec/api/v2/users_api_spec.rb +++ b/spec/api/v2/users_api_spec.rb @@ -30,4 +30,22 @@ RSpec.describe 'users API', type: :request do expect(response).to match_json_schema(:user) expect(JSON.parse(response.body)['data']['id']).to eq user.id end + + context 'RAML example' do + let(:resource) { get_json_example(:user) } + let(:collection) { get_json_example(:users) } + let(:current) { get_json_example(:current_user) } + + it 'resource matches schema' do + expect(resource).to match_json_schema(:user) + end + + it 'collection matches schema' do + expect(collection).to match_json_schema(:users) + end + + it 'current_user resource matches schema' do + expect(current).to match_json_schema(:current_user) + end + end end