Skip to content

Commit d80bf88

Browse files
committed
Don't error if new keys are added to student response
It's common when developing with APIs to add in new keys to the response if this happens we don't want this code to error.
1 parent 0e9ceec commit d80bf88

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

lib/profile_api_client.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,9 @@ def build_student(attrs)
255255
symbolized_attrs[:email] ||= nil
256256
symbolized_attrs[:ssoProviders] ||= []
257257

258-
Student.new(**symbolized_attrs)
258+
allowed_keys = ProfileApiClient::Student.members
259+
260+
Student.new(**symbolized_attrs.slice(*allowed_keys))
259261
end
260262

261263
def unauthorized!(response)

spec/lib/profile_api_client_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,15 @@ def update_school_student
553553
expect(school_student_response).to eq(expected)
554554
end
555555

556+
it 'returns student even if response contains unexpected keys' do
557+
data = { id: student_id, schoolId: school.id, name: 'name', username: 'username', email: 'test@example.com', ssoProviders: [], createdAt: '', updatedAt: '', discardedAt: '' }
558+
response = data.merge({ unexpectedKey: 'unexpectedValue' })
559+
expected = ProfileApiClient::Student.new(**data)
560+
stub_request(:get, student_url)
561+
.to_return(status: 200, body: response.to_json, headers: { 'content-type' => 'application/json' })
562+
expect(school_student_response).to eq(expected)
563+
end
564+
556565
private
557566

558567
def school_student

0 commit comments

Comments
 (0)