Portman
repository·main·Indexed 20 days ago
https://github.com/apideck-libraries/portmanA CLI tool that converts OpenAPI 3.0 and 3.1 specifications into Postman collections. It automates the generation of contract, variation, and integration tests for CI/CD pipelines. Key features include OpenAPI document filtering, Newman test execution, global collection customizations (scripts, variables, and security overwrites), and mass replacement utilities.
What's inside Portman
- Portman is a tool designed to port OpenAPI specifications to Postman collections. It leverages the properties defined in your OpenAPI document to automatically power your Postman collection, injecting contract and variation tests with minimal configuration. It allows for customization of Postman requests and variables through a wide range of options.
Use Template Expressions to dynamically build Postman variable names
mainPortman supports template expressions to dynamically construct Postman variable names based on OpenAPI operation metadata. These expressions are wrapped in
<and>and allow you to create unique variable names for different operations (e.g., using tags or operation IDs) within a single configuration directive.Supported template expressions include:
<operationId>: TheoperationIdof the OpenAPI operation (e.g.,leadsAll).<method>: The HTTP method (e.g.,GET).<path>: The full path (e.g.,/crm/leads).<pathRef>: The Portman path reference (e.g.,GET::/crm/leads).<opsRef>: TheoperationIdwith a fallback topathRefifoperationIdis missing.<tag>: The first tag name (e.g.,Leads).<tag1>,<tag2>,<tagn>: The nth tag name.<pathPart1>,<pathPart2>,<pathPartn>: The nth part of the path (e.g.,crm,leads).
# Example OpenAPI operation path: /crm/leads: get: tags: - Leads - CRM operationId: leadsAllUnderstand the Portman configuration structure
mainA Portman configuration file is a JSON object that defines how an OpenAPI specification is transformed into a Postman collection. The top-level structure includes:
version: The JSON Portman configuration version.tests: Definitions for generated contract, variation, integration, and content tests.contractTests: Options for autogenerated contract tests.contentTests: Additional Postman tests to check response content.variationTests: Options for defining variation tests.integrationTests: Options for defining integration tests.extendTests: Custom additions of manually created Postman tests.
assignVariables: Settings for Postman collection variables to facilitate automation.overwrites: Custom modifications of OpenAPI/Postman request data.operationPreRequestScripts: Injected Postman Pre-request Scripts.globals: Customizations applied to the entire Postman collection.
Target specific OpenAPI operations in Portman
mainTo apply tests, variables, or overwrites to specific endpoints, use targeting options. Portman supports three ways to identify an operation:
openApiOperationId(String): The exactoperationIdfrom the OpenAPI spec (e.g.,"leadsAll").openApiOperationIds(Array): A list ofoperationIdstrings.openApiOperation(String): A combination of the HTTP method and path using a::separator (e.g.,"GET::/crm/leads").
Wildcard Matching for
openApiOperation:- Method wildcard:
"*::/crm/leads"targets all methods for that path. - Path wildcard:
"GET::/crm/*"targets the GET method for any path under/crm/. - Full wildcard:
"*::/crm/*"targets all methods and all paths under/crm/.
Exclusions: Use
excludeForOperations(Array) to skip specific operations. This is particularly useful when using wildcards to exclude certain endpoints from a broad match. Example:["leadsAdd", "GET::/crm/leads/{id}"].{ "openApiOperation": "*::/crm/*", "excludeForOperations": ["leadsAdd"] }How Fuzzing works in Portman
mainFuzzing (or fuzz testing) in Portman is an automated technique to trigger validation and error responses in your API by providing unexpected or invalid data. Portman generates new Postman requests based on OpenAPI request properties, where each variation is designed to test contract constraints.
Key Behaviors:
- Fuzzing is based on OpenAPI request properties.
- Postman Dynamic variables are rendered before fuzzing occurs.
- Regular Postman variables are skipped from fuzzing.
- For request bodies, fuzzing is only applicable to
application/jsonmedia types.
Available Fuzzing Variations: For
requestBody,requestQueryParams, andrequestHeaders, you can enable:requiredFields(Boolean): Removes properties marked asrequiredin OpenAPI.minimumNumberFields(Boolean): Changes numeric values to be lower than the definedminimum.maximumNumberFields(Boolean): Changes numeric values to be higher than the definedmaximum.minLengthFields(Boolean): Changes string lengths to be lower than the definedminLength.maxLengthFields(Boolean): Changes string lengths to be higher than the definedmaxLength.
Target OpenAPI operations in contentTests
mainWhen defining
contentTestsin your Portman settings, you can target specific operations using two different methods:operationId: Target a specific operation using its unique ID defined in your OpenAPI specification (e.g.,leadsOne).openApiOperation: Target an operation by combining its HTTP method and path using the formatMETHOD::/path(e.g.,GET::/crm/leads).
"contentTests": [ { "openApiOperation": "GET::/crm/leads", "responseBodyTests": [ { "key": "data[0].company_name", "value": "Spacex" } ] } ]Assign Postman variables from API responses
mainPortman allows you to automatically assign values from API responses (body or headers) or request bodies to Postman collection variables. This is useful for creating automated flows where a value from one request (like a newly created entity's ID) is needed for subsequent requests (like Read, Update, or Delete operations).
Variables are mapped to Postman requests based on the OpenAPI
operationIdor a combination of the HTTP method and path."assignVariables": [ { "openApiOperationId": "leadsAdd", "collectionVariables": [ { "responseBodyProp": "data.id", "name": "leadsAdd.id" } ] } ]Use references ($ref) in Portman configuration
mainPortman supports using JSON/YAML references (
$ref) within your configuration files. This allows you to reuse configuration segments across multiple setups or split large configurations into smaller, more manageable files.References can point to either:
- Local files: A path to a JSON or YAML file on your filesystem.
- Remote files: A URL to a JSON or YAML file hosted online.
A
$refstring must include both the file path and the specific definition path (using the#fragment syntax).version: 1 tests: contractTests: $ref: './examples/config-references/definitions/test-contracts.yaml#/contractTests' contentTests: $ref: './examples/config-references/definitions/test-content.yaml#/contentTests' globals: $ref: 'https://raw.githubusercontent.com/apideck-libraries/portman/main/examples/config-references/definitions/globals.yaml#/globals'Configure Portman Globals
mainThe
globalssection in a Portman configuration file allows you to apply customizations to the entire generated Postman collection. This includes injecting scripts, setting variables, performing mass replacements, and overwriting security schemas. The configuration follows this structure:{ "version": 1.0, "globals": { ... } }{ "version": 1.0, "globals": { "stripResponseExamples": true, "collectionPreRequestScripts": [ "pm.collectionVariables.set('status', pm.iterationData.get('status') || 'open')" ], "collectionTestScripts": [ "pm.collectionVariables.set('applicationId', pm.iterationData.get('applicationId') || '1111');" ], "collectionVariables": { "name": "portman", "version": "1" }, "keyValueReplacements": { "x-apideck-app-id": "{{applicationId}}" }, "valueReplacements": { "<Bearer Token>": "{{bearerToken}}" }, "rawReplacements": [ { "searchFor": "Unify", "replaceWith": "Unify ApiDeck" } ], "portmanReplacements": [ { "searchFor": "]::", "replaceWith": "]-:" } ] } }How to use Portman overwrites to manipulate Postman collections
mainPortman allows you to manipulate the generated Postman collection to facilitate automation. You can overwrite or modify request bodies, headers, query parameters, and path variables. This is useful for:
- Generating flows that require unique values (e.g., using Postman dynamic variables).
- Referencing created entities in subsequent flows (e.g., setting an ID as a query parameter).
- Replacing example values from the OpenAPI spec with actual values that exist in your API.
Overwrites are mapped to specific OpenAPI operations using either
openApiOperationIdoropenApiOperation.portman --cliOptionsFile ./examples/testsuite-overwrites/portman-cli-options.jsonConfigure variationTests in Portman settings
mainThe
variationTestsproperty in your Portman JSON configuration allows you to inject specific test variations into the generated Postman collection.Target Selection
You can target specific operations for variations using:
openApiOperationId(String): The OpenAPIoperationId(e.g.,leadsAll).openApiOperation(String): A combination of method and path (e.g.,GET::/crm/leads).excludeForOperations(Array): Operations to skip.
Variation Properties
Each variation within the
variationsarray can include:name: The name used in the Postman collection.openApiResponse(Optional): The expected response code orcode::content-type(e.g.,404or200::text/*). If omitted, it defaults to the first response in the OpenAPI spec.openApiRequest(Optional): The request content-type (e.g.,application/*).overwrites: Custom modifications to the request data.tests: A collection ofcontractTests,contentTests, andextendTestsspecific to this variation.assignVariables: Postman collection variables assigned based on the variation's response.
What are Content Tests in Portman?
mainWhile Portman's default contract tests validate the structure and properties of requests and responses, Content Tests allow you to validate the actual values returned by an API.
Content tests are used to ensure that specific properties in the response body or headers match expected values, follow certain constraints (like length), or exist within a set of allowed values. This is useful for converting OpenAPI definitions into Postman collections that include functional validation of data, not just schema validation.