======================== CODE SNIPPETS ======================== TITLE: Installing Dependencies for openapi-modifier Example (Bash) DESCRIPTION: This command installs all necessary Node.js package dependencies defined in the `package.json` file, which are required to run the `openapi-modifier` example. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/examples/example-cli-simple-json-config/README.md#_snippet_1 LANGUAGE: bash CODE: ``` npm install ``` ---------------------------------------- TITLE: Installing Dependencies (npm) DESCRIPTION: This command installs all necessary project dependencies listed in the `package.json` file, preparing the environment for development and building. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/drafts/en/contributing.md#_snippet_0 LANGUAGE: bash CODE: ``` npm install ``` ---------------------------------------- TITLE: Installing Dependencies for openapi-modifier DESCRIPTION: This command installs all necessary project dependencies, typically defined in the `package.json` file, required to run the `openapi-modifier` example. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/examples/example-cli-openapi-json/README.md#_snippet_0 LANGUAGE: bash CODE: ``` npm install ``` ---------------------------------------- TITLE: Installing Project Dependencies (npm) DESCRIPTION: This command installs all necessary project dependencies defined in `package.json` using npm, preparing the environment for running the OpenAPI conversion. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/examples/example-cli-openapi-yaml-to-json/README.md#_snippet_0 LANGUAGE: bash CODE: ``` npm install ``` ---------------------------------------- TITLE: Installing Dependencies for openapi-modifier (npm) DESCRIPTION: This command installs all necessary project dependencies defined in `package.json`. It's the first step to set up the project before running the modifier. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/examples/example-cli-openapi-yaml/README.md#_snippet_0 LANGUAGE: bash CODE: ``` npm install ``` ---------------------------------------- TITLE: Installing Project Dependencies (npm) DESCRIPTION: This command installs all necessary project dependencies defined in the `package.json` file. It is a prerequisite for running any other scripts in the project. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/examples/example-cli-generate-api-types/README.md#_snippet_0 LANGUAGE: bash CODE: ``` npm install ``` ---------------------------------------- TITLE: Installing Dependencies for OpenAPI Modifier Example (Bash) DESCRIPTION: This command installs all necessary project dependencies, including `openapi-modifier` for specification modification and `dtsgenerator` for TypeScript type generation, as defined in the `package.json` file. It is a prerequisite for running the API modification and type generation process. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/examples/example-cli-simple-generate-api-types/README.md#_snippet_0 LANGUAGE: bash CODE: ``` npm install ``` ---------------------------------------- TITLE: Running openapi-modifier via npm script (Bash) DESCRIPTION: This command executes the `openapi-modifier` using a predefined `start` script in `package.json`, typically configured to apply the modifications to the OpenAPI specification. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/examples/example-cli-simple-json-config/README.md#_snippet_2 LANGUAGE: bash CODE: ``` npm start ``` ---------------------------------------- TITLE: Installing and Using OpenAPI Modifier CLI DESCRIPTION: This snippet demonstrates how to install the `openapi-modifier` package as a development dependency using npm and provides a basic command-line example for modifying an OpenAPI specification. It shows how to specify input, output, and configuration files. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/drafts/ru/readme.md#_snippet_0 LANGUAGE: Shell CODE: ``` npm i --save-dev openapi-modifier openapi-modifier --input=input/openapi.yml --output=output/openapi.yml --config=openapi-modifier.config.js ``` ---------------------------------------- TITLE: Initial OpenAPI Schema for /pets GET Endpoint (Replacement Example) - YAML DESCRIPTION: This YAML snippet presents the initial OpenAPI schema for the `/pets` GET endpoint, which will be completely replaced by a new schema structure in the subsequent modification example. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/src/rules/patch-endpoint-response-schema/README.md#_snippet_5 LANGUAGE: yaml CODE: ``` paths: /pets: get: summary: List all pets responses: 200: content: 'application/json': schema: type: 'object' properties: items: type: 'array' items: type: 'object' properties: id: { type: 'string' } name: { type: 'string' } ``` ---------------------------------------- TITLE: Running OpenAPI Conversion via npm Script (Bash) DESCRIPTION: This command executes the `start` script defined in `package.json`, which typically wraps the `openapi-modifier` CLI command to perform the OpenAPI YAML to JSON conversion with applied modifications. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/examples/example-cli-openapi-yaml-to-json/README.md#_snippet_2 LANGUAGE: bash CODE: ``` npm start ``` ---------------------------------------- TITLE: Running openapi-modifier via npm script (Bash) DESCRIPTION: This command executes the `start` script defined in `package.json`, which typically wraps the `openapi-modifier` command with predefined input, output, and configuration paths. It's a convenient way to run the modification process. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/examples/example-cli-openapi-yaml/README.md#_snippet_2 LANGUAGE: bash CODE: ``` npm start ``` ---------------------------------------- TITLE: Installing Project Dependencies (npm) DESCRIPTION: Installs all necessary project dependencies using npm. This command should be run after cloning the repository to set up the development environment. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/contributing.md#_snippet_0 LANGUAGE: bash CODE: ``` npm install ``` ---------------------------------------- TITLE: Simple Component Descriptor Usage Examples (TypeScript) DESCRIPTION: Provides various examples of valid Simple Component Descriptor strings in TypeScript. These examples demonstrate how to specify just a component name, a component with a property path, or paths involving array elements and 'allOf', 'oneOf', or 'anyOf' elements. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/drafts/en/descriptor.md#_snippet_2 LANGUAGE: typescript CODE: ``` "TestDto" // Just component name "TestDto.foo.bar" // Component with property path "TestDto.foo.bar[].test" // Component with array element path "TestDto.allOf[2].foo" // Component with allOf element path "TestDto.oneOf[2].foo" // Component with oneOf element path "TestDto.anyOf[2].foo" // Component with anyOf element path ``` ---------------------------------------- TITLE: Installing Project Dependencies (npm) DESCRIPTION: This command installs all necessary project dependencies defined in the `package.json` file. It is a crucial first step before running any scripts. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/examples/example-package-openapi-yaml/README.md#_snippet_0 LANGUAGE: bash CODE: ``` npm install ``` ---------------------------------------- TITLE: Project Structure for openapi-modifier Example DESCRIPTION: This section illustrates the directory layout for the `example-cli-simple-npx` project, detailing the locations for the input OpenAPI file, the generated output file, the `openapi-modifier` configuration, and the `package.json`. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/examples/example-cli-simple-npx/README.md#_snippet_0 LANGUAGE: plaintext CODE: ``` example-cli-simple-npx/ ├── input/ │ └── openapi.yml # Input OpenAPI file ├── output/ │ └── openapi.yml # Output OpenAPI file (will be created) ├── openapi-modifier.config.js # Modifier configuration └── package.json # npm configuration ``` ---------------------------------------- TITLE: Running openapi-modifier Example via npm (Bash) DESCRIPTION: This command initiates the `openapi-modifier` process. It reads the `input/openapi.yml` file, applies the configured modification rules, and then saves the resulting modified OpenAPI specification to `output/openapi.yml`. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/examples/example-cli-simple-npx/README.md#_snippet_2 LANGUAGE: bash CODE: ``` npm start ``` ---------------------------------------- TITLE: Configuring Basic OpenAPI Spec Merging in JavaScript DESCRIPTION: This configuration example demonstrates how to merge an OpenAPI specification from a relative path. It uses the `merge-openapi-spec` rule and specifies the `path` to the YAML file to be merged into the current specification. This is a basic setup without explicit collision handling. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/src/rules/merge-openapi-spec/docs/en/_config.md#_snippet_0 LANGUAGE: javascript CODE: ``` module.exports = { pipeline: [ // ... other rules { rule: "merge-openapi-spec", config: { path: 'temp-openapi-specs/new-list-endpoints.yaml' // specify path to specification file to merge } } // ... other rules ] } ``` ---------------------------------------- TITLE: Simple Descriptor Usage Examples (TypeScript) DESCRIPTION: Provides various examples of valid simple descriptor strings in TypeScript, demonstrating how to specify just a component name, a property path, an array element path, or paths within allOf, oneOf, or anyOf elements. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/descriptor.md#_snippet_2 LANGUAGE: typescript CODE: ``` // Examples of valid values: "TestDto" // Just component name "TestDto.foo.bar" // Component with property path "TestDto.foo.bar[].test" // Component with array element path "TestDto.allOf[2].foo" // Component with allOf element path "TestDto.oneOf[2].foo" // Component with oneOf element path "TestDto.anyOf[2].foo" // Component with anyOf element path ``` ---------------------------------------- TITLE: Installing and Using OpenAPI Modifier via CLI DESCRIPTION: This snippet demonstrates how to install `openapi-modifier` as a development dependency using npm and then how to execute it from the command line. It specifies input and output OpenAPI files and a configuration file for modifications. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/drafts/en/readme.md#_snippet_3 LANGUAGE: shell CODE: ``` npm i --save-dev openapi-modifier openapi-modifier --input=input/openapi.yml --output=output/openapi.yml --config=openapi-modifier.config.js ``` ---------------------------------------- TITLE: Object Component Descriptor Usage Examples (JSON) DESCRIPTION: Illustrates valid Object Component Descriptor JSON structures. These examples show how to define a component name and optional explicit correction paths for properties or array items, without automatic path transformation. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/drafts/en/descriptor.md#_snippet_3 LANGUAGE: json CODE: ``` { "componentName": "TestDto" } { "componentName": "TestDto", "correction": "properties.foo.properties.bar" } { "componentName": "TestDto", "correction": "items.properties.foo" } ``` ---------------------------------------- TITLE: Installing openapi-modifier CLI DESCRIPTION: This command installs the `openapi-modifier` package as a development dependency using npm. It provides the `simple-text-file-modifier` CLI tool for text file manipulation. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/simple-text-file-modifier.md#_snippet_0 LANGUAGE: bash CODE: ``` npm install --save-dev openapi-modifier ``` ---------------------------------------- TITLE: Defining a Simple String in OpenAPI DESCRIPTION: This example demonstrates how to define a basic string type in OpenAPI, including properties like description, minimum and maximum length, pattern validation, and format (e.g., email). SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/schema-diff.md#_snippet_0 LANGUAGE: json CODE: ``` { "type": "string", "description": "Field description", "minLength": 1, "maxLength": 100, "pattern": "^[A-Za-z0-9]+$", "format": "email" } ``` ---------------------------------------- TITLE: Referencing a Schema Definition in OpenAPI DESCRIPTION: This example illustrates how to use a JSON reference (`$ref`) to point to an existing schema definition within the OpenAPI components section, promoting reusability and modularity. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/schema-diff.md#_snippet_5 LANGUAGE: json CODE: ``` { "$ref": "#/components/schemas/User" } ``` ---------------------------------------- TITLE: Building the Project (npm) DESCRIPTION: This command compiles the project source code, typically TypeScript, into executable JavaScript, preparing it for deployment or testing. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/drafts/en/contributing.md#_snippet_1 LANGUAGE: bash CODE: ``` npm run build ``` ---------------------------------------- TITLE: Configuring patch-endpoint-schema with Security Patch (JavaScript) DESCRIPTION: This example demonstrates how to configure the `patch-endpoint-schema` rule to add a security section to an endpoint's schema. It targets the 'GET /pets' endpoint and uses the 'merge' patch method to apply the `bearerAuth` security requirement. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/src/rules/patch-endpoint-schema/README.md#_snippet_0 LANGUAGE: JavaScript CODE: ``` module.exports = { pipeline: [ { rule: "patch-endpoint-schema", config: { endpointDescriptor: "GET /pets", // specify the endpoint to modify patchMethod: "merge", // use merge method to apply changes schemaDiff: { "security": [ // add security section to the schema { "bearerAuth": [] } ] } } } ] } ``` ---------------------------------------- TITLE: Running Tests (npm) DESCRIPTION: This command executes the project's test suite, ensuring that all components function as expected and validating code changes. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/drafts/en/contributing.md#_snippet_2 LANGUAGE: bash CODE: ``` npm test ``` ---------------------------------------- TITLE: Installing and Running OpenAPI Modifier via npm CLI DESCRIPTION: This snippet shows how to install `openapi-modifier` as a development dependency using `npm` and then execute it from the command line. It takes an input OpenAPI specification, produces an output file, and uses a specified configuration file. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/README.md#_snippet_3 LANGUAGE: shell CODE: ``` npm i --save-dev openapi-modifier openapi-modifier --input=input/openapi.yml --output=output/openapi.yml --config=openapi-modifier.config.js ``` ---------------------------------------- TITLE: Defining an Array Type in OpenAPI DESCRIPTION: This example demonstrates how to define an array in OpenAPI, specifying the type of its items, minimum and maximum item counts, and uniqueness constraints for the elements. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/schema-diff.md#_snippet_3 LANGUAGE: json CODE: ``` { "type": "array", "description": "Array of elements", "items": { "type": "string", "description": "Array element" }, "minItems": 1, "maxItems": 10, "uniqueItems": true } ``` ---------------------------------------- TITLE: Formatting Code (npm) DESCRIPTION: This command applies code formatting rules to the project's source files, ensuring consistent style across the codebase. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/drafts/en/contributing.md#_snippet_4 LANGUAGE: bash CODE: ``` npm run format ``` ---------------------------------------- TITLE: Running openapi-modifier Directly with Configuration (Bash) DESCRIPTION: This command directly invokes the `openapi-modifier` executable, specifying the input OpenAPI file, the output file for the modified specification, and the JSON configuration file to apply the desired transformations. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/examples/example-cli-simple-json-config/README.md#_snippet_3 LANGUAGE: bash CODE: ``` openapi-modifier --input=input/openapi.yml --output=output/openapi.yml --config=openapi-modifier.config.json ``` ---------------------------------------- TITLE: Configuring Basic Endpoint Parameter Schema Patching (JavaScript) DESCRIPTION: This configuration example demonstrates how to use the `patch-endpoint-parameter-schema` rule to modify an endpoint's query parameter. It specifies the target endpoint (`GET /api/list`), the parameter by name and `in` location (`test`, `query`), and adds an `enum` to its schema. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/src/rules/patch-endpoint-parameter-schema/README.md#_snippet_0 LANGUAGE: javascript CODE: ``` module.exports = { pipeline: [ // ... other rules { rule: "patch-endpoint-parameter-schema", config: { endpointDescriptor: 'GET /api/list', // specify the endpoint to modify parameterDescriptor: { name: 'test', // specify parameter name in: 'query', // specify that parameter is in query }, schemaDiff: { enum: ['foo', 'bar'], // add enum to parameter } }, } // ... other rules ] } ``` ---------------------------------------- TITLE: Generating README Documentation (npm) DESCRIPTION: This command executes a tool to automatically generate or update the project's README file, often based on source code comments or configuration. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/drafts/en/contributing.md#_snippet_5 LANGUAGE: bash CODE: ``` npm run tools:generate-readme ``` ---------------------------------------- TITLE: Running OpenAPI Modification Script DESCRIPTION: This command executes the predefined 'start' script in `package.json`, which in turn runs the `openapi-modifier` with specified input, output, and configuration files. It initiates the OpenAPI file modification process. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/examples/example-cli-openapi-json/README.md#_snippet_1 LANGUAGE: bash CODE: ``` npm start ``` ---------------------------------------- TITLE: Installing openapi-modifier via npm DESCRIPTION: This command installs the `openapi-modifier` package as a development dependency using npm. It's required to use the `simple-text-file-modifier` CLI tool for text file modifications. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/drafts/en/simple-text-file-modifier.md#_snippet_0 LANGUAGE: bash CODE: ``` npm install --save-dev openapi-modifier ``` ---------------------------------------- TITLE: Defining an Object Type in OpenAPI DESCRIPTION: This example shows how to define a complex object in OpenAPI, including its properties, required fields, behavior for additional properties, and limits on the number of properties. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/schema-diff.md#_snippet_4 LANGUAGE: json CODE: ``` { "type": "object", "description": "Complex object", "properties": { "name": { "type": "string", "description": "Object name" }, "value": { "type": "number", "description": "Object value" } }, "required": ["name"], "additionalProperties": false, "minProperties": 1, "maxProperties": 5 } ``` ---------------------------------------- TITLE: Running OpenAPI Specification Modification and Type Generation (Bash) DESCRIPTION: This command initiates the main process, which first modifies the input OpenAPI file using `openapi-modifier` based on the `openapi-modifier.config.ts` rules, and then generates TypeScript types from the modified OpenAPI file using `dtsgenerator`. It orchestrates the entire workflow described in the example. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/examples/example-cli-simple-generate-api-types/README.md#_snippet_1 LANGUAGE: bash CODE: ``` npm start ``` ---------------------------------------- TITLE: Object Descriptor Usage Examples (TypeScript) DESCRIPTION: Illustrates valid object descriptor configurations in TypeScript, showing how to define a component with or without a correction path, and how to specify paths for nested properties or array items explicitly. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/descriptor.md#_snippet_3 LANGUAGE: typescript CODE: ``` // Examples of valid values: { "componentName": "TestDto" } { "componentName": "TestDto", "correction": "properties.foo.properties.bar" } { "componentName": "TestDto", "correction": "items.properties.foo" } ``` ---------------------------------------- TITLE: Implementing Conditional Fields in OpenAPI DESCRIPTION: This example shows how to define conditional schema logic using `if`, `then`, and `else` keywords in OpenAPI, making certain fields required or optional based on other property values. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/schema-diff.md#_snippet_7 LANGUAGE: json CODE: ``` { "type": "object", "properties": { "type": { "type": "string", "enum": ["user", "admin"] }, "permissions": { "type": "array", "items": { "type": "string" } } }, "if": { "properties": { "type": { "const": "admin" } } }, "then": { "required": ["permissions"] } } ``` ---------------------------------------- TITLE: Combining Types with oneOf in OpenAPI DESCRIPTION: This example demonstrates how to use `oneOf` in OpenAPI to specify that a value can conform to exactly one of several provided schemas, allowing for flexible data types. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/schema-diff.md#_snippet_6 LANGUAGE: json CODE: ``` { "oneOf": [ { "type": "string" }, { "type": "number" } ], "description": "Value can be either a string or a number" } ``` ---------------------------------------- TITLE: Generating Rule Types (npm) DESCRIPTION: This command runs a utility to generate TypeScript type definitions for rules, which can improve type safety and developer experience. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/drafts/en/contributing.md#_snippet_6 LANGUAGE: bash CODE: ``` npm run tools:generate-rule-types ``` ---------------------------------------- TITLE: Configuring Endpoint Parameter Schema Patching in JavaScript DESCRIPTION: This example demonstrates a basic configuration for patching an endpoint's parameter schema using the `patch-endpoint-parameter-schema` rule. It specifies the target endpoint (`GET /api/list`), the parameter to modify (`test` in `query`), and adds an `enum` constraint to its schema. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/README.md#_snippet_20 LANGUAGE: javascript CODE: ``` module.exports = { pipeline: [ // ... other rules { rule: "patch-endpoint-parameter-schema", config: { endpointDescriptor: 'GET /api/list', // specify the endpoint to modify parameterDescriptor: { name: 'test', // specify parameter name in: 'query', // specify that parameter is in query }, schemaDiff: { enum: ['foo', 'bar'], // add enum to parameter } }, } // ... other rules ] } ``` ---------------------------------------- TITLE: Configuring Basic OpenAPI Spec Merge in JavaScript DESCRIPTION: This configuration example demonstrates how to merge an OpenAPI specification from a relative path. It specifies the `rule` as 'merge-openapi-spec' and provides the `path` to the YAML specification file to be merged into the current pipeline. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/src/rules/merge-openapi-spec/README.md#_snippet_0 LANGUAGE: JavaScript CODE: ``` module.exports = { pipeline: [ // ... other rules { rule: "merge-openapi-spec", config: { path: 'temp-openapi-specs/new-list-endpoints.yaml' // specify path to specification file to merge } } // ... other rules ] } ``` ---------------------------------------- TITLE: Initial OpenAPI Schema for /pets GET Endpoint - YAML DESCRIPTION: This YAML snippet shows the initial OpenAPI schema for the `/pets` GET endpoint, defining an array of pet objects with `id` and `name` properties, before any modifications are applied. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/src/rules/patch-endpoint-response-schema/README.md#_snippet_2 LANGUAGE: yaml CODE: ``` paths: /pets: get: summary: List all pets responses: 200: content: 'application/json': schema: type: 'object' properties: items: type: 'array' items: type: 'object' properties: id: { type: 'string' } name: { type: 'string' } ``` ---------------------------------------- TITLE: Configuring File Modifications in TypeScript DESCRIPTION: This TypeScript configuration example provides rules for modifying a text file. It prepends a warning comment, appends an end-of-file marker, and replaces 'Components.' with 'ApiComponents.' using a regular expression, similar to the JavaScript example. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/simple-text-file-modifier.md#_snippet_3 LANGUAGE: typescript CODE: ``` export default { addBefore: "// This file was auto-generated. Do not edit manually.\n", addAfter: "\n// End of auto-generated file", replace: [ { searchValue: /Components\./g, replaceValue: 'ApiComponents.' } ] }; ``` ---------------------------------------- TITLE: Configuring `change-content-type` Rule with Multiple Content Type Mappings in JavaScript DESCRIPTION: This example illustrates a more detailed configuration for the `change-content-type` rule. It shows how to map specific content types like `application/xml` and `text/plain` to `application/json`, in addition to a wildcard replacement for any other content types. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/src/rules/change-content-type/README.md#_snippet_1 LANGUAGE: JavaScript CODE: ``` module.exports = { pipeline: [ // ... other rules { rule: "change-content-type", config: { map: { "application/xml": "application/json", // replace application/xml with application/json "text/plain": "application/json", // replace text/plain with application/json "*/*": "application/json" // replace all other content types with application/json } }, } // ... other rules ] } ``` ---------------------------------------- TITLE: Defining a Number Type in OpenAPI DESCRIPTION: This example illustrates how to define a numeric type in OpenAPI, including constraints like minimum, maximum, exclusive bounds, multipleOf, and format (e.g., float, double, int32, int64). SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/schema-diff.md#_snippet_2 LANGUAGE: json CODE: ``` { "type": "number", "description": "Numeric value", "minimum": 0, "maximum": 100, "exclusiveMinimum": true, "exclusiveMaximum": true, "multipleOf": 0.5, "format": "float" } ``` ---------------------------------------- TITLE: Defining a String Enum in OpenAPI DESCRIPTION: This example shows how to create a string enumeration in OpenAPI, allowing only a predefined set of values and specifying a default value for the field. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/schema-diff.md#_snippet_1 LANGUAGE: json CODE: ``` { "type": "string", "enum": ["a", "b", "c"], "description": "Choose one of the allowed values", "default": "a" } ``` ---------------------------------------- TITLE: Configuring Endpoint Schema Patching with Security (JavaScript) DESCRIPTION: This example demonstrates a basic configuration for the `patch-endpoint-schema` rule. It specifies modifying the 'GET /pets' endpoint using the 'merge' patch method and adds a 'security' section with 'bearerAuth' to its schema. This configuration is used to apply security definitions to a specific API endpoint. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/README.md#_snippet_27 LANGUAGE: javascript CODE: ``` module.exports = { pipeline: [ { rule: "patch-endpoint-schema", config: { endpointDescriptor: "GET /pets", // specify the endpoint to modify patchMethod: "merge", // use merge method to apply changes schemaDiff: { "security": [ // add security section to the schema { "bearerAuth": [] } ] } } } ] } ``` ---------------------------------------- TITLE: Running OpenAPI Conversion Directly with CLI (Bash) DESCRIPTION: This command directly invokes the `openapi-modifier` command-line interface. It specifies `input/openapi.yml` as the source OpenAPI file and `output/openapi.json` as the destination for the converted and modified JSON output. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/examples/example-cli-openapi-yaml-to-json/README.md#_snippet_3 LANGUAGE: bash CODE: ``` openapi-modifier --input=input/openapi.yml --output=output/openapi.json ``` ---------------------------------------- TITLE: Cleaning Build Directories (npm) DESCRIPTION: This command removes generated build artifacts and temporary files, ensuring a clean state for new builds. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/drafts/en/contributing.md#_snippet_3 LANGUAGE: bash CODE: ``` npm run clear ``` ---------------------------------------- TITLE: Configuring patch-component-schema Rule in JavaScript DESCRIPTION: This example demonstrates a basic configuration for the `patch-component-schema` rule within a JavaScript pipeline. It targets the 'TestDTO' component and modifies its type to 'string', illustrating a simple schema update. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/src/rules/patch-component-schema/README.md#_snippet_0 LANGUAGE: javascript CODE: ``` module.exports = { pipeline: [ // ... other rules { rule: "patch-component-schema", config: { descriptor: 'TestDTO', // specify the component to modify schemaDiff: { type: 'string', // change component type to string }, }, } // ... other rules ] } ``` ---------------------------------------- TITLE: Configuring Endpoint Response Schema Patching (Basic) - JavaScript DESCRIPTION: This configuration example demonstrates how to use the `patch-endpoint-response-schema` rule to modify a specific endpoint's response schema. It targets the `GET /api/list` endpoint and adds an `enum` constraint to the `status` field within an array in the response. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/README.md#_snippet_25 LANGUAGE: javascript CODE: ``` module.exports = { pipeline: [ // ... other rules { rule: "patch-endpoint-response-schema", config: { endpointDescriptor: 'GET /api/list', // specify the endpoint to modify correction: '[].status', // specify path to status field in response array schemaDiff: { enum: ['foo', 'bar'], // add enum to status field }, }, } // ... other rules ] } ``` ---------------------------------------- TITLE: Installing OpenAPI Modifier via npm DESCRIPTION: This command installs the `openapi-modifier` package as a development dependency in your project. Using `--save-dev` ensures it's listed in your `package.json` under `devDependencies`, indicating it's required for development but not for production. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/drafts/en/readme.md#_snippet_1 LANGUAGE: Bash CODE: ``` npm install --save-dev openapi-modifier ``` ---------------------------------------- TITLE: Configuring patch-endpoint-schema for Response Schema Correction (JavaScript) DESCRIPTION: This example shows a more detailed configuration for `patch-endpoint-schema`, targeting a specific field within a response schema. It modifies the schema of the 'GET /pets' endpoint's 200 OK response content to include an `enum` array, using `endpointDescriptorCorrection` to pinpoint the exact location. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/src/rules/patch-endpoint-schema/README.md#_snippet_1 LANGUAGE: JavaScript CODE: ``` module.exports = { pipeline: [ { rule: "patch-endpoint-schema", config: { patchMethod: 'merge', endpointDescriptor: "GET /pets", endpointDescriptorCorrection: 'responses.200.content.*/*.schema', schemaDiff: { enum: ['3', '4'], }, } } ] } ``` ---------------------------------------- TITLE: Detailed Configuration for `change-endpoints-basepath` Rule in JavaScript DESCRIPTION: This snippet provides a more comprehensive configuration example for the `change-endpoints-basepath` rule. It illustrates how to replace a specific prefix (`/public/v1/service/api`) with a new one (`/api`) and explicitly sets `ignoreOperationCollisions` to `false` to ensure conflict checking. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/src/rules/change-endpoints-basepath/README.md#_snippet_1 LANGUAGE: js CODE: ``` module.exports = { pipeline: [ // ... other rules { rule: "change-endpoints-basepath", config: { map: { '/public/v1/service/api': '/api', // replace the prefix /public/v1/service/api with /api }, ignoreOperationCollisions: false, // do not ignore operation conflicts when replacing paths }, } // ... other rules ] } ``` ---------------------------------------- TITLE: Configuring Endpoint Filtering with `enabled` in JavaScript DESCRIPTION: This configuration example demonstrates how to use the `enabled` parameter to explicitly keep a specific endpoint. Only the `GET /foo/ping` endpoint will be included in the OpenAPI specification, while all other endpoints will be removed. This is useful for whitelisting a small set of desired endpoints. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/src/rules/filter-endpoints/docs/en/_config.md#_snippet_0 LANGUAGE: javascript CODE: ``` module.exports = { pipeline: [ // ... other rules { rule: "filter-endpoints", config: { enabled: [ 'GET /foo/ping' // keep only GET /foo/ping endpoint, all others will be removed ], }, } // ... other rules ] } ``` ---------------------------------------- TITLE: Defining a Simple String in OpenAPI DESCRIPTION: This snippet defines an OpenAPI schema for a basic string type, including common constraints such as minimum and maximum length, a regular expression pattern for validation, and a format hint like 'email'. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/drafts/en/schema-diff.md#_snippet_0 LANGUAGE: json CODE: ``` { "type": "string", "description": "Field description", "minLength": 1, "maxLength": 100, "pattern": "^[A-Za-z0-9]+$", "format": "email" // Supported formats: email, date, date-time, uri, uuid, etc. } ``` ---------------------------------------- TITLE: Example of Operation Collision in OpenAPI YAML DESCRIPTION: This YAML snippet illustrates an operation collision scenario where two distinct API paths, `/api/v1/pets` and `/v1/pets`, both define a GET operation. If `/api/v1` is replaced with `/v1`, both paths would resolve to `/v1/pets`, causing a conflict. The snippet shows the original conflicting paths. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/src/rules/change-endpoints-basepath/docs/en/_notes.md#_snippet_0 LANGUAGE: YAML CODE: ``` paths: /api/v1/pets: get: summary: List all pets /v1/pets: get: summary: Get pet by id ``` ---------------------------------------- TITLE: Detailed Endpoint Schema Patching with Enum Correction (JavaScript) DESCRIPTION: This more detailed configuration example for the `patch-endpoint-schema` rule targets the 'GET /pets' endpoint. It uses the 'merge' method and specifies `endpointDescriptorCorrection` to apply a schema difference (adding an `enum` property with values '3' and '4') to a specific part of the response schema, such as `responses.200.content.*/*.schema`. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/README.md#_snippet_28 LANGUAGE: javascript CODE: ``` module.exports = { pipeline: [ { rule: "patch-endpoint-schema", config: { patchMethod: 'merge', endpointDescriptor: "GET /pets", endpointDescriptorCorrection: 'responses.200.content.*/*.schema', schemaDiff: { enum: ['3', '4'], }, } } ] } ``` ---------------------------------------- TITLE: Running Complete API Type Generation Workflow (npm) DESCRIPTION: This command executes the entire API type generation workflow sequentially, including OpenAPI specification modification, TypeScript type generation, and patching of the generated types. It orchestrates all defined scripts. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/examples/example-cli-generate-api-types/README.md#_snippet_1 LANGUAGE: bash CODE: ``` npm run start ``` ---------------------------------------- TITLE: Initial OpenAPI YAML for Pet Name Description DESCRIPTION: This snippet displays the initial OpenAPI 3.0 YAML definition for the `/pets` POST endpoint, focusing on the `name` property within the `requestBody` schema. It includes an initial `description` for the `name` field. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/src/rules/patch-endpoint-request-body-schema/docs/en/_motivation.md#_snippet_3 LANGUAGE: yaml CODE: ``` paths: /pets: post: requestBody: content: application/json: schema: type: object properties: name: type: string description: Pet name ``` ---------------------------------------- TITLE: OpenAPI Specification After Unused Component Removal (YAML) DESCRIPTION: This YAML snippet illustrates the OpenAPI specification after the `remove-unused-components` rule has been applied with the configuration from the previous example. It demonstrates that `UnusedSchema` and `UnusedResponse` have been successfully removed, while `User` and `NotFound` (which were explicitly ignored) remain, resulting in a cleaner and more concise specification. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/src/rules/remove-unused-components/README.md#_snippet_4 LANGUAGE: yaml CODE: ``` components: schemas: User: type: object properties: name: type: string responses: NotFound: description: Not found ``` ---------------------------------------- TITLE: Excluding Internal Endpoints in OpenAPI Modifier (JavaScript) DESCRIPTION: This JavaScript configuration for the 'openapi-modifier-config.js' file applies the 'filter-endpoints' rule to remove all endpoints whose paths start with '/internal'. This is a practical example of how to use `disabledPathRegExp` to clean up an OpenAPI specification by excluding internal or private API routes. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/src/rules/filter-endpoints/README.md#_snippet_5 LANGUAGE: JavaScript CODE: ``` module.exports = { pipeline: [ { rule: "filter-endpoints", config: { disabledPathRegExp: [/^/internal/] } } ] } ``` ---------------------------------------- TITLE: Referencing an OpenAPI Schema Definition DESCRIPTION: This snippet illustrates how to reference an existing schema definition within an OpenAPI specification using the '$ref' keyword. This mechanism promotes reusability and modularity by allowing schemas to be defined once and referenced multiple times. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/drafts/en/schema-diff.md#_snippet_5 LANGUAGE: json CODE: ``` { "$ref": "#/components/schemas/User" } ``` ---------------------------------------- TITLE: Advanced Configuration for remove-unused-components Rule (JavaScript) DESCRIPTION: This example illustrates a more detailed configuration for the `remove-unused-components` rule. It demonstrates how to use the `ignore` parameter to specify components (by name or regular expression) that should not be removed, and the `printDeletedComponents` parameter to log the list of components that were deleted to the console. This allows for fine-grained control over the cleanup process. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/src/rules/remove-unused-components/README.md#_snippet_1 LANGUAGE: js CODE: ``` module.exports = { pipeline: [ // ... other rules { rule: "remove-unused-components", config: { ignore: [ "NotFoundDTO", /^Error.*/, // ignore all components starting with Error /.*Response$/ // ignore all components ending with Response ], printDeletedComponents: true // print list of deleted components to console }, } // ... other rules ] } ``` ---------------------------------------- TITLE: Installing OpenAPI Modifier via npm DESCRIPTION: This command demonstrates how to install the `openapi-modifier` package as a development dependency using npm. The `--save-dev` flag ensures it's added to the `devDependencies` section of the `package.json` file, indicating it's required for development but not for production. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/README.md#_snippet_1 LANGUAGE: Bash CODE: ``` npm install --save-dev openapi-modifier ``` ---------------------------------------- TITLE: Directly Running openapi-modifier with CLI Arguments (Bash) DESCRIPTION: This command directly invokes the `openapi-modifier` CLI tool, specifying the input OpenAPI file, the desired output file, and the configuration file. This provides explicit control over the modification process without relying on `package.json` scripts. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/examples/example-cli-openapi-yaml/README.md#_snippet_3 LANGUAGE: bash CODE: ``` openapi-modifier --input=input/openapi.yml --output=output/openapi.yml --config=openapi-modifier.config.ts ``` ---------------------------------------- TITLE: Removing Unused Endpoint Parameter using openapi-modifier (YAML, JavaScript) DESCRIPTION: This example demonstrates how to remove an unused query parameter (`version`) from a specific endpoint (`GET /pets/{petId}`) in an OpenAPI specification using the `openapi-modifier` tool. It shows the OpenAPI definition before and after the modification, along with the JavaScript configuration required for the `remove-parameter` rule. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/src/rules/remove-parameter/docs/en/_motivation.md#_snippet_0 LANGUAGE: yaml CODE: ``` paths: /pets/{petId}: get: summary: Get pet by ID parameters: - name: petId in: path required: true schema: type: string - name: version in: query required: false schema: type: string ``` LANGUAGE: javascript CODE: ``` module.exports = { pipeline: [ { rule: "remove-parameter", config: { endpointDescriptor: "GET /pets/{petId}", parameterDescriptor: { name: "version", in: "query" } } } ] } ``` LANGUAGE: yaml CODE: ``` paths: /pets/{petId}: get: summary: Get pet by ID parameters: - name: petId in: path required: true schema: type: string ``` ---------------------------------------- TITLE: Running OpenAPI Modifier via NPX CLI DESCRIPTION: This command demonstrates how to execute the `openapi-modifier` tool using `npx`. It specifies the input OpenAPI specification file, the desired output file, and the path to a configuration file. This is a quick way to run the tool without global installation. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/README.md#_snippet_2 LANGUAGE: shell CODE: ``` npx openapi-modifier --input=input/openapi.yml --output=output/openapi.yml --config=openapi-modifier.config.js ``` ---------------------------------------- TITLE: Detailed Configuration for Patching Endpoint Response Schema - JavaScript DESCRIPTION: This example provides a more detailed configuration for the `patch-endpoint-response-schema` rule. It specifically targets the `GET /api/list` endpoint's 200 OK response with `application/json` content type. The `status` field is updated with an `enum`, and the `deepmerge` method is explicitly used for applying the schema changes, allowing for more complex merging behavior. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/src/rules/patch-endpoint-response-schema/docs/en/_config.md#_snippet_1 LANGUAGE: JavaScript CODE: ``` module.exports = { pipeline: [ // ... other rules { rule: "patch-endpoint-response-schema", config: { endpointDescriptor: 'GET /api/list', // specify the endpoint to modify correction: '[].status', // specify path to status field in response array code: 200, // specify response code to apply changes to contentType: 'application/json', // specify content type to apply changes to schemaDiff: { enum: ['foo', 'bar'], // add enum to status field }, patchMethod: 'deepmerge' // use deepmerge method for deep merging changes }, } // ... other rules ] } ``` ---------------------------------------- TITLE: Replaced OpenAPI Schema for /pets GET Endpoint - YAML DESCRIPTION: This YAML snippet displays the OpenAPI schema for the `/pets` GET endpoint after applying the `patch-endpoint-response-schema` rule with the `replace` method. It shows the new `data` array structure, which has completely replaced the original `items` array. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/src/rules/patch-endpoint-response-schema/README.md#_snippet_7 LANGUAGE: yaml CODE: ``` paths: /pets: get: summary: List all pets responses: 200: content: 'application/json': schema: type: 'object' properties: data: type: 'array' items: type: 'object' properties: id: { type: 'string' } name: { type: 'string' } description: { type: 'string' } ``` ---------------------------------------- TITLE: Configuring File Modifications in JavaScript DESCRIPTION: This JavaScript configuration example defines rules for modifying a text file. It adds a warning comment to the beginning, an end-of-file marker, and replaces all occurrences of 'Components.' with 'ApiComponents.' using a regular expression. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/simple-text-file-modifier.md#_snippet_2 LANGUAGE: javascript CODE: ``` module.exports = { addBefore: "// This file was auto-generated. Do not edit manually.\n", addAfter: "\n// End of auto-generated file", replace: [ { searchValue: /Components\./g, replaceValue: 'ApiComponents.' } ] }; ``` ---------------------------------------- TITLE: OpenAPI YAML Before Base Path Modification DESCRIPTION: This YAML snippet shows an example of an OpenAPI `paths` definition before applying the `change-endpoints-basepath` rule. It illustrates an endpoint with a long base path (`/public/api/v1/pets`) that needs to be shortened. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/src/rules/change-endpoints-basepath/README.md#_snippet_2 LANGUAGE: yaml CODE: ``` paths: /public/api/v1/pets: get: summary: List all pets ``` ---------------------------------------- TITLE: Defining Main OpenAPI Specification (openapi.yaml) DESCRIPTION: This YAML snippet defines the primary OpenAPI specification for a 'Main API', including a '/pets' endpoint with a GET method. It serves as the base specification to which other API definitions will be merged. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/src/rules/merge-openapi-spec/README.md#_snippet_2 LANGUAGE: yaml CODE: ``` openapi: 3.0.0 info: title: Main API paths: /pets: get: summary: List all pets responses: 200: content: 'application/json': schema: type: 'object' ``` ---------------------------------------- TITLE: Using OpenAPI Modifier CLI with npx DESCRIPTION: This command demonstrates how to execute the `openapi-modifier` command-line interface using `npx`. It specifies the `--input` OpenAPI file, the `--output` path for the modified file, and the `--config` file that defines the modification rules. This is a common way to run Node.js package executables without globally installing them. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/drafts/en/readme.md#_snippet_2 LANGUAGE: Shell CODE: ``` npx openapi-modifier --input=input/openapi.yml --output=output/openapi.yml --config=openapi-modifier.config.js ``` ---------------------------------------- TITLE: Defining a Number Type in OpenAPI DESCRIPTION: This snippet illustrates how to define an OpenAPI schema for a numeric type, including constraints such as minimum and maximum values, exclusive bounds, multiples, and a format hint like 'float' or 'int32'. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/drafts/en/schema-diff.md#_snippet_2 LANGUAGE: json CODE: ``` { "type": "number", "description": "Numeric value", "minimum": 0, "maximum": 100, "exclusiveMinimum": true, "exclusiveMaximum": true, "multipleOf": 0.5, "format": "float" // Supported formats: float, double, int32, int64 } ``` ---------------------------------------- TITLE: Removing Common Parameter Component using openapi-modifier (YAML, JavaScript) DESCRIPTION: This example illustrates how to remove a globally defined parameter component (`ApiKeyHeader`) from the `components.parameters` section of an OpenAPI specification. This is useful for cleaning up definitions that interfere with type generation. The example includes the OpenAPI definition before and after the modification, along with the `openapi-modifier` JavaScript configuration. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/src/rules/remove-parameter/docs/en/_motivation.md#_snippet_1 LANGUAGE: yaml CODE: ``` components: parameters: ApiKeyHeader: name: X-API-Key in: header required: true schema: type: string ``` LANGUAGE: javascript CODE: ``` module.exports = { pipeline: [ { rule: "remove-parameter", config: { parameterDescriptor: { name: "X-API-Key", in: "header" } } } ] } ``` LANGUAGE: yaml CODE: ``` components: parameters: {} ``` ---------------------------------------- TITLE: Configuring `remove-unused-components` Rule (Detailed) - JavaScript DESCRIPTION: This snippet provides a more detailed configuration example for the `remove-unused-components` rule. It illustrates how to use the `ignore` array to specify components or regular expressions to exclude from removal, and how to enable `printDeletedComponents` to log the removed components to the console. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/README.md#_snippet_39 LANGUAGE: js CODE: ``` module.exports = { pipeline: [ // ... other rules { rule: "remove-unused-components", config: { ignore: [ "NotFoundDTO", /^Error.*/, // ignore all components starting with Error /.*Response$/ // ignore all components ending with Response ], printDeletedComponents: true // print list of deleted components to console } } // ... other rules ] } ``` ---------------------------------------- TITLE: Configuring File Modifications in JSON DESCRIPTION: This JSON configuration example specifies rules for modifying a text file. It adds a warning comment to the beginning, an end-of-file marker, and performs a string replacement of 'Components.' with 'ApiComponents.'. Note that regular expressions are not directly supported in JSON for `searchValue`. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/simple-text-file-modifier.md#_snippet_4 LANGUAGE: json CODE: ``` { "addBefore": "// This file was auto-generated. Do not edit manually.\n", "addAfter": "\n// End of auto-generated file", "replace": [ { "searchValue": "Components.", "replaceValue": "ApiComponents." } ] } ``` ---------------------------------------- TITLE: Configuring remove-min-items Rule (Detailed) - JavaScript DESCRIPTION: This JavaScript configuration shows a more detailed setup for the `remove-min-items` rule. By setting `showUnusedWarning` to `true`, the rule will issue a warning if no schemas containing the `minItems` property are found in the specification. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/README.md#_snippet_34 LANGUAGE: JavaScript CODE: ``` module.exports = { pipeline: [ // ... other rules { rule: "remove-min-items", config: { showUnusedWarning: true // show warning if no schemas with minItems are found in the specification } } // ... other rules ] } ``` ---------------------------------------- TITLE: Applying merge Method to OpenAPI Schema (JavaScript) DESCRIPTION: This example demonstrates the `merge` method's behavior. It shows how top-level properties are overwritten, and nested objects are completely replaced rather than merged, as seen with the `properties` object. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/merge-vs-deepmerge.md#_snippet_0 LANGUAGE: javascript CODE: ``` // Original object { type: 'object', properties: { name: { type: 'string' }, age: { type: 'number' } } } // Applying merge with: { properties: { name: { type: 'string', format: 'email' } } } // Result: { type: 'object', properties: { name: { type: 'string', format: 'email' } } } ``` ---------------------------------------- TITLE: OpenAPI Modifier Configuration File Structure DESCRIPTION: This JavaScript snippet outlines the structure of a configuration file for `openapi-modifier`. It defines optional logger settings (verbose, minLevel), required input and output paths for OpenAPI files, and a `pipeline` array for applying modification rules. An example rule `change-content-type` is included, showing how to disable a rule and configure its specific parameters. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/drafts/ru/readme.md#_snippet_2 LANGUAGE: JavaScript CODE: ``` module.exports = { // (optional) Logger settings logger: { verbose: true, // Enable verbose logging minLevel: 0 // Minimum logging level: 0 - trace, 1 - debug, 2 - info, 3 - warn, 4 - error }, // Path to the input OpenAPI specification file input: './openapi.yaml', // Path to the output file output: './modified-openapi.yaml', // Pipeline of rules to apply (see all available rules with configuration examples below) pipeline: [ { rule: "change-content-type", disabled: false, // (optional) Disable rule config: { map: { "*/*": "application/json" } } } // Other rules... ] } ``` ---------------------------------------- TITLE: Configuring Basic Component Schema Patching in JavaScript DESCRIPTION: This example demonstrates a basic configuration for the `patch-component-schema` rule. It shows how to specify a component using a simple descriptor (`TestDTO`) and change its type to `string` using `schemaDiff`. This configuration applies a simple merge by default. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/README.md#_snippet_18 LANGUAGE: js CODE: ``` module.exports = { pipeline: [ // ... other rules { rule: "patch-component-schema", config: { descriptor: 'TestDTO', // specify the component to modify schemaDiff: { type: 'string', // change component type to string }, }, } // ... other rules ] } ``` ---------------------------------------- TITLE: Simple Component Descriptor Format DESCRIPTION: Illustrates the string format used by the simple descriptor, where the component name and correction path are separated by dots. This format is compact and relies on automatic path transformation. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/descriptor.md#_snippet_0 LANGUAGE: Configuration String CODE: ``` "ComponentName.path.to.property" ``` ---------------------------------------- TITLE: Defining a String Enum in OpenAPI DESCRIPTION: This snippet demonstrates how to define an OpenAPI schema for a string type that restricts its value to a predefined list of options (enum) and specifies a default value if none is provided. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/drafts/en/schema-diff.md#_snippet_1 LANGUAGE: json CODE: ``` { "type": "string", "enum": ["a", "b", "c"], "description": "Choose one of the allowed values", "default": "a" } ``` ---------------------------------------- TITLE: OpenAPI Specification with Unused Components (YAML) DESCRIPTION: This YAML snippet represents an OpenAPI specification containing both used (`User`, `NotFound`) and unused (`UnusedSchema`, `UnusedResponse`) components. It serves as an input example to demonstrate how the `remove-unused-components` rule identifies and targets components that are not referenced elsewhere in the document for removal. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/src/rules/remove-unused-components/README.md#_snippet_2 LANGUAGE: yaml CODE: ``` components: schemas: User: type: object properties: name: type: string UnusedSchema: type: object properties: field: type: string responses: NotFound: description: Not found UnusedResponse: description: Unused response ``` ---------------------------------------- TITLE: Executing openapi-modifier CLI Directly DESCRIPTION: This command directly invokes the `openapi-modifier` CLI tool, specifying the input OpenAPI JSON file, the desired output path for the modified file, and the TypeScript configuration file to apply the modification rules. It's the underlying command executed by `npm start`. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/examples/example-cli-openapi-json/README.md#_snippet_2 LANGUAGE: bash CODE: ``` openapi-modifier --input=input/openapi.json --output=output/openapi.json --config=openapi-modifier.config.ts ``` ---------------------------------------- TITLE: Defining Conditional Fields in OpenAPI DESCRIPTION: This snippet demonstrates how to use 'if' and 'then' keywords to define conditional schema requirements. It specifies that the 'permissions' field is required only if the 'type' field has a value of 'admin'. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/drafts/en/schema-diff.md#_snippet_7 LANGUAGE: json CODE: ``` { "type": "object", "properties": { "type": { "type": "string", "enum": ["user", "admin"] }, "permissions": { "type": "array", "items": { "type": "string" } } }, "if": { "properties": { "type": { "const": "admin" } } }, "then": { "required": ["permissions"] } } ``` ---------------------------------------- TITLE: Combining Types with oneOf in OpenAPI DESCRIPTION: This snippet shows how to define a schema where a value can conform to one of several specified types using the 'oneOf' keyword. This allows for flexible data structures where a field can accept different types of input. SOURCE: https://github.com/itwillwork/openapi-modifier/blob/main/docs/drafts/en/schema-diff.md#_snippet_6 LANGUAGE: json CODE: ``` { "oneOf": [ { "type": "string" }, { "type": "number" } ], "description": "Value can be either a string or a number" } ```