Skip to content

Commit e1f8873

Browse files
feat: implement default values injection and refactor transform processing
- Add support for injecting default values into OpenAPI specs via configuration - Introduce generic helper to unify transform processing logic - Add tests for default value processing, covering: - Disabled default injection - No rules configured - Parameter and property-level default application - Refactor vendor extension handling to use the shared transform helper - Update Homebrew and Scoop templates to reflect new features: - Default value injection - Response schema flattening
1 parent bd23b03 commit e1f8873

File tree

13 files changed

+2188
-381
lines changed

13 files changed

+2188
-381
lines changed

.openapirc.yaml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,78 @@ vendor_extensions:
6868
page_size_param: "$request.{limit_param}"
6969
results_path: "$response.{results_field}"
7070
required_fields: ["cursor_param", "results_field"]
71+
72+
# Default values configuration
73+
default_values:
74+
enabled: true
75+
rules:
76+
# Set default values for query parameters
77+
query_limit_defaults:
78+
target:
79+
location: "parameter"
80+
condition:
81+
parameter_in: "query"
82+
type: "integer"
83+
property_name: "(limit|size|page_size|per_page)"
84+
value: 20
85+
priority: 10
86+
87+
query_sort_defaults:
88+
target:
89+
location: "parameter"
90+
condition:
91+
parameter_in: "query"
92+
type: "string"
93+
property_name: "(sort|order|direction)"
94+
value: "asc"
95+
priority: 9
96+
97+
# Set defaults for boolean properties in components
98+
boolean_defaults:
99+
target:
100+
location: "component"
101+
condition:
102+
type: "boolean"
103+
property_name: "(active|enabled|is_.*)"
104+
value: true
105+
priority: 8
106+
107+
# Set defaults for role/status fields
108+
role_defaults:
109+
target:
110+
location: "component"
111+
condition:
112+
type: "string"
113+
property_name: "role"
114+
value: "user"
115+
priority: 7
116+
117+
status_defaults:
118+
target:
119+
location: "component"
120+
condition:
121+
type: "string"
122+
property_name: "status"
123+
value: "active"
124+
priority: 6
125+
126+
# Set defaults for array fields in response schemas
127+
array_defaults:
128+
target:
129+
location: "response"
130+
condition:
131+
type: "array"
132+
http_methods: ["get"]
133+
value: []
134+
priority: 5
135+
136+
# Set defaults for enum fields
137+
priority_enum_defaults:
138+
target:
139+
location: "component"
140+
condition:
141+
type: "string"
142+
property_name: "priority"
143+
has_enum: true
144+
value: "medium"
145+
priority: 4

README.md

Lines changed: 214 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ OpenMorph is a production-grade CLI and TUI tool for transforming OpenAPI vendor
1919
## Features
2020

2121
- Transform OpenAPI vendor extension keys in YAML/JSON
22+
- **Default values injection** - Automatically set default values for parameters, schemas, and responses with rule-based matching
2223
- **Vendor-specific pagination extensions** - Auto-inject Fern, Speakeasy, and other vendor pagination metadata
2324
- **Auto-detection of array fields** - Automatically find results arrays in response schemas
2425
- Interactive TUI for reviewing and approving changes
@@ -211,7 +212,7 @@ openmorph --input ./openapi --vendor-providers fern --config config.yaml
211212

212213
### Example: Complete Transformation
213214

214-
Transform keys, clean up pagination, and add vendor extensions:
215+
Transform keys, clean up pagination, add vendor extensions, and set default values:
215216

216217
```sh
217218
openmorph --input ./openapi \
@@ -223,6 +224,22 @@ openmorph --input ./openapi \
223224
--config ./config.yaml
224225
```
225226

227+
Your `config.yaml` can include all features:
228+
229+
```yaml
230+
mappings:
231+
x-foo: x-bar
232+
vendor_extensions:
233+
enabled: true
234+
providers:
235+
fern:
236+
# ... fern config
237+
default_values:
238+
enabled: true
239+
rules:
240+
# ... default value rules
241+
```
242+
226243
### Example: Pagination Priority
227244

228245
Transform APIs to use only checkpoint pagination (highest priority):
@@ -485,6 +502,202 @@ openmorph --input ./openapi --dry-run --config config.yaml
485502
results_path: "$response.data"
486503
```
487504

505+
## Default Values
506+
507+
OpenMorph includes a powerful default values feature that allows you to automatically set default values throughout your OpenAPI specifications. This feature supports complex rule-based matching and can be applied to parameters, request bodies, response schemas, and component schemas.
508+
509+
### Overview
510+
511+
The Default Values feature allows you to:
512+
513+
- Set defaults for **parameter schemas** (path, query, header, cookie)
514+
- Set defaults for **request body schemas**
515+
- Set defaults for **response schemas**
516+
- Set defaults for **component schemas** (reusable objects)
517+
- Apply defaults to **arrays** and **enum fields**
518+
- Use **regex patterns** for flexible property matching
519+
- Configure **rule priorities** for precise control
520+
521+
### Configuration
522+
523+
Default values are configured through the `default_values` section in your config file:
524+
525+
```yaml
526+
default_values:
527+
enabled: true
528+
rules:
529+
# Set default limit for pagination parameters
530+
query_limit_defaults:
531+
target:
532+
location: "parameter"
533+
condition:
534+
parameter_in: "query"
535+
type: "integer"
536+
property_name: "(limit|size|page_size|per_page)"
537+
value: 20
538+
priority: 10
539+
540+
# Set default sort direction
541+
query_sort_defaults:
542+
target:
543+
location: "parameter"
544+
condition:
545+
parameter_in: "query"
546+
type: "string"
547+
property_name: "(sort|order|direction)"
548+
value: "asc"
549+
priority: 9
550+
551+
# Boolean fields default to true
552+
boolean_defaults:
553+
target:
554+
location: "component"
555+
condition:
556+
type: "boolean"
557+
property_name: "(active|enabled|is_.*)"
558+
value: true
559+
priority: 8
560+
```
561+
562+
### Configuration Options
563+
564+
#### Target
565+
566+
- `location`: Where to apply defaults
567+
- `"parameter"` - URL parameters (query, path, header, cookie)
568+
- `"request_body"` - Request body schemas
569+
- `"response"` - Response body schemas
570+
- `"component"` - Component schemas (reusable objects)
571+
- `property`: Optional specific property name to target
572+
- `path`: Optional JSONPath-like selector for precise targeting
573+
574+
#### Conditions
575+
576+
- `type`: Schema type constraint (`"string"`, `"integer"`, `"boolean"`, `"array"`, `"object"`)
577+
- `parameter_in`: For parameters - where they're located (`"query"`, `"path"`, `"header"`, `"cookie"`)
578+
- `http_methods`: List of HTTP methods to target (`["get", "post"]`)
579+
- `path_patterns`: List of regex patterns for API paths (`["/api/v1/.*"]`)
580+
- `has_enum`: Only apply to fields with enum constraints
581+
- `is_array`: Only apply to array-type fields
582+
- `property_name`: Regex pattern to match property names (`"(limit|size|page_size)"`)
583+
- `required`: Apply only to required (`true`) or optional (`false`) fields
584+
585+
#### Values
586+
587+
- `value`: Simple default value (string, number, boolean, array, object)
588+
- `template`: Complex template object for structured defaults
589+
- `priority`: Rule priority (higher numbers = higher priority)
590+
591+
### Usage Examples
592+
593+
**Apply defaults using config file:**
594+
595+
```bash
596+
openmorph --input ./openapi --config config.yaml
597+
```
598+
599+
**Preview defaults changes:**
600+
601+
```bash
602+
openmorph --input ./openapi --config config.yaml --dry-run
603+
```
604+
605+
**Combine with other transformations:**
606+
607+
```bash
608+
openmorph --input ./openapi \
609+
--mapping x-operation-group-name=x-fern-sdk-group-name \
610+
--vendor-providers fern \
611+
--pagination-priority cursor,offset,none \
612+
--flatten-responses \
613+
--config config.yaml
614+
```
615+
616+
### Advanced Examples
617+
618+
**Complex object defaults:**
619+
620+
```yaml
621+
default_values:
622+
enabled: true
623+
rules:
624+
settings_defaults:
625+
target:
626+
location: "component"
627+
condition:
628+
type: "object"
629+
property_name: "settings"
630+
template:
631+
theme: "light"
632+
notifications: true
633+
language: "en"
634+
priority: 4
635+
```
636+
637+
**Array response defaults:**
638+
639+
```yaml
640+
default_values:
641+
enabled: true
642+
rules:
643+
array_defaults:
644+
target:
645+
location: "response"
646+
condition:
647+
type: "array"
648+
http_methods: ["get"]
649+
path_patterns: ["/api/v1/.*"]
650+
value: []
651+
priority: 5
652+
```
653+
654+
### Rule Priority and Ordering
655+
656+
Rules are processed in priority order (highest priority first), allowing you to:
657+
658+
1. Set broad defaults with low priority
659+
2. Override with specific defaults using higher priority
660+
3. Ensure consistent application order across runs
661+
662+
```yaml
663+
default_values:
664+
enabled: true
665+
rules:
666+
# Broad rule - low priority
667+
all_strings:
668+
condition:
669+
type: "string"
670+
value: "default"
671+
priority: 1
672+
673+
# Specific rule - high priority (overrides above)
674+
user_names:
675+
condition:
676+
type: "string"
677+
property_name: "name"
678+
value: "Anonymous"
679+
priority: 10
680+
```
681+
682+
### Best Practices
683+
684+
1. **Use Regex Patterns**: Property name matching supports regex for flexible targeting
685+
2. **Prioritize Rules**: Use priority to control application order
686+
3. **Test with Dry-Run**: Always preview changes before applying
687+
4. **Backup Files**: Enable backup for safe operations
688+
5. **Combine Features**: Use alongside vendor extensions and other transformations
689+
6. **Document Rules**: Use clear rule names and comments in config files
690+
691+
### Integration
692+
693+
The defaults feature integrates seamlessly with other OpenMorph features:
694+
695+
- **Vendor Extensions**: Applied before vendor extensions
696+
- **Response Flattening**: Works on original schemas before flattening
697+
- **Validation**: Validates resulting OpenAPI specs
698+
- **Interactive Mode**: Preview all changes together
699+
- **Backup**: Automatic backup before modifications
700+
488701
## Interactive TUI Controls
489702

490703
- `j`/`k` or `left`/`right`: Navigate files

0 commit comments

Comments
 (0)