feat(auth): expose auth placeholders as empty collection variables on import (closes #894)#942
Open
Mrsandeep27 wants to merge 1 commit into
Open
Conversation
…mport
When an OpenAPI spec declares a security scheme, the generated Postman
collection's auth block references templates like `{{basicAuthUsername}}`,
`{{basicAuthPassword}}`, `{{bearerToken}}`, etc. Today those variables are
not added to `collection.variable`, so the first time a user sends a
request they hit an "unresolved variable" warning and have to discover
where credentials are expected.
This mirrors the existing `baseUrl` treatment: after `collection.auth` is
set, scan the auth helper for `{{identifier}}` placeholders and push an
empty Variable for each unique one into `collection.variable`. That way
users see all auth credentials alongside `baseUrl` in the Variables tab
and can fill them in one place.
- schemaUtils.getAuthCollectionVariables(authHelper): new helper that
recursively scans the auth helper payload for `{{...}}` placeholders
and returns empty collection-level `Variable` objects for each unique
name. No-op for `noauth`.
- schemapack.js: after setting `generatedStore.collection.auth`, push the
returned variables alongside `baseUrl`.
- Extended the existing basic-auth test to assert `basicAuthUsername` and
`basicAuthPassword` now appear as empty entries in
`collection.variable`.
Works uniformly for all auth schemes that use variable placeholders
(basic, bearer, digest, apiKey, oauth2, …) without needing per-scheme
mappings.
Closes postmanlabs#894
Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
Closes #894.
When the imported OpenAPI spec declares a security scheme, `getAuthHelper` produces an auth block whose values template Postman variables:
```js
{ type: 'basic', basic: [
{ key: 'username', value: '{{basicAuthUsername}}' },
{ key: 'password', value: '{{basicAuthPassword}}' }
]}
```
…but those variables were never pushed into `collection.variable`. On first send the user hit an "unresolved variable" warning and had to hunt for where to put credentials — while `baseUrl` (which is treated the same way) already appears in the Variables tab waiting to be filled.
Change
Mirror the existing `baseUrl` flow. After `generatedStore.collection.auth = authHelper` is set in `schemapack.js`, scan the helper for `{{identifier}}` placeholders and add an empty collection-level `Variable` for each unique one.
```js
if (authHelper) {
schemaUtils.getAuthCollectionVariables(authHelper).forEach((element) => {
generatedStore.collection.variables.add(element);
});
}
```
New helper `schemaUtils.getAuthCollectionVariables(authHelper)` recursively walks the helper payload (skipping the `type` field), uses `/{{\s*([A-Za-z0-9_]+)\s*}}/g` to extract placeholder names, de-duplicates with a `Set`, and returns `Variable` objects with empty values.
Design notes:
Test plan
Extended `test/unit/convertV2.test.js` → 'Should convert a collection and set basic auth placeholder as variable' to additionally assert:
```js
const variableKeys = conversionResult.output[0].data.variable.map((v) => v.key);
expect(variableKeys).to.include('basicAuthUsername');
expect(variableKeys).to.include('basicAuthPassword');
// both should be empty strings
```
Out of scope (for follow-up)
🤖 Generated with Claude Code