From e4a1ced8c26f165bd1454be0409db2555e4e26e9 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Sat, 29 Oct 2016 22:34:29 +0800 Subject: [PATCH] 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/_user.json | 3 -- doc/api/schemas/current_user.json | 12 ++++++++ spec/api/v2/users_api_spec.rb | 18 ++++++++++++ 7 files changed, 70 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/_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/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