Skip to content

Commit

Permalink
feat: use null instead of empty-string for empty state (#451)
Browse files Browse the repository at this point in the history
allows consumers to more easy distinguish between an empty oas file and an empty user input
  • Loading branch information
Ilias Tsangaris authored Jun 10, 2021
1 parent 48514f7 commit b5ae66b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
28 changes: 14 additions & 14 deletions __tests__/lib/get-auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ const oas = new Oas(multipleSecurities);
test('should fetch all auths from the OAS files', () => {
expect(oas.getAuth({ oauthScheme: 'oauth', apiKeyScheme: 'apikey' })).toStrictEqual({
apiKeyScheme: 'apikey',
apiKeySignature: '',
apiKeySignature: null,
basicAuth: {
pass: '',
user: '',
pass: null,
user: null,
},
httpBearer: '',
oauthDiff: '',
httpBearer: null,
oauthDiff: null,
oauthScheme: 'oauth',
unknownAuthType: '',
unknownAuthType: null,
});
});

Expand Down Expand Up @@ -107,16 +107,16 @@ describe('#getByScheme', () => {
});
});

it('should return emptystring for anything else', () => {
expect(getByScheme(topLevelUser, { type: 'unknown' })).toBe('');
expect(getByScheme({}, { type: 'http', scheme: 'basic' })).toStrictEqual({ user: '', pass: '' });
expect(getByScheme({}, { type: 'http', scheme: 'bearer' })).toBe('');
expect(getByScheme({}, { type: 'http', scheme: 'unknown' })).toBe('');
expect(getByScheme(keysUser, { type: 'unknown' })).toBe('');
expect(getByScheme(keysUser, { type: 'unknown' }, 'app-2')).toBe('');
it('should return null for anything else', () => {
expect(getByScheme(topLevelUser, { type: 'unknown' })).toBeNull();
expect(getByScheme({}, { type: 'http', scheme: 'basic' })).toStrictEqual({ user: null, pass: null });
expect(getByScheme({}, { type: 'http', scheme: 'bearer' })).toBeNull();
expect(getByScheme({}, { type: 'http', scheme: 'unknown' })).toBeNull();
expect(getByScheme(keysUser, { type: 'unknown' })).toBeNull();
expect(getByScheme(keysUser, { type: 'unknown' }, 'app-2')).toBeNull();
});

it('should allow scheme to be undefined', () => {
expect(getByScheme(topLevelUser)).toBe('');
expect(getByScheme(topLevelUser)).toBeNull();
});
});
10 changes: 5 additions & 5 deletions src/lib/get-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ function getKey(user, scheme) {
switch (scheme.type) {
case 'oauth2':
case 'apiKey':
return user[scheme._key] || user.apiKey || scheme['x-default'] || '';
return user[scheme._key] || user.apiKey || scheme['x-default'] || null;

case 'http':
if (scheme.scheme === 'basic') {
return user[scheme._key] || { user: user.user || '', pass: user.pass || '' };
return user[scheme._key] || { user: user.user || null, pass: user.pass || null };
}

if (scheme.scheme === 'bearer') {
return user[scheme._key] || user.apiKey || '';
return user[scheme._key] || user.apiKey || null;
}
return '';
return null;

default:
return '';
return null;
}
}

Expand Down

0 comments on commit b5ae66b

Please sign in to comment.