Skip to content

Commit fb82ea8

Browse files
feat(core): added factoryMethods
1 parent 729c5b8 commit fb82ea8

56 files changed

Lines changed: 2291 additions & 4 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/content/docs/reference/configuration/output.mdx

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,6 +1677,72 @@ export interface Pet {
16771677
}
16781678
```
16791679

1680+
## factoryMethods
1681+
1682+
**Type:** `Object`
1683+
**Default:** `{ generate: false }`
1684+
1685+
Generate factory methods for DTOs (Data Transfer Objects) initialized with safe default values. Useful for testing and initializing empty state.
1686+
Functionality handles OpenAPI `readOnly` and `writeOnly` flags to generate appropriate payload structures:
1687+
- **Required properties:** Always included in the factory output, regardless of their visibility flags.
1688+
- **Optional `readOnly` properties:** Always omitted from the factory output, as they would be dropped by the server.
1689+
- **Optional `writeOnly` properties:** Always included in the factory output (even if `optionalPropertyStrategy` is set to `'omit'`).
1690+
1691+
```ts title="orval.config.ts"
1692+
export default defineConfig({
1693+
petstore: {
1694+
output: {
1695+
factoryMethods: {
1696+
generate: true,
1697+
functionNamePrefix: 'create',
1698+
mode: 'separate-file',
1699+
optionalPropertyStrategy: 'include',
1700+
outputDirectory: `#output.workspace.schemas`,
1701+
},
1702+
},
1703+
},
1704+
});
1705+
```
1706+
1707+
### generate
1708+
1709+
**Type:** `Boolean`
1710+
**Default:** `false`
1711+
1712+
Enables or disables factory generation.
1713+
1714+
### functionNamePrefix
1715+
1716+
**Type:** `String`
1717+
**Default:** `'create'`
1718+
1719+
Prefix for the generated factory function names.
1720+
1721+
### mode
1722+
1723+
**Type:** `'inline-with-schema' | 'separate-file' | 'combined-separate-file'`
1724+
**Default:** `'separate-file'`
1725+
1726+
Where to generate the factory methods:
1727+
- `inline-with-schema`: Appends the factory function to the schema file.
1728+
- `separate-file`: Creates a `{schema}.factory.ts` with factory method. By default it is placed next to schema file.
1729+
- `combined-separate-file`: Aggregates all factory methods into a single `factoryMethods.ts` file.
1730+
1731+
### optionalPropertyStrategy
1732+
1733+
**Type:** `'include' | 'omit'`
1734+
**Default:** `'include'`
1735+
1736+
Determines whether optional schema properties are included in the default factory output.
1737+
1738+
### outputDirectory
1739+
1740+
**Type:** `String`
1741+
**Default:** `#output.workspace.schemas`
1742+
1743+
Defaults to the value configured in `#output.workspace.schemas`.
1744+
Determines where factory methods will be generated (can be used to generated methods away from schema directory).
1745+
Takes effect only when used `mode` is `separate-file` or `combined-separate-file`.
16801746
---
16811747

16821748
## Other Options

packages/angular/src/http-client.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ const createOutput = (
125125
optionsParamRequired: false,
126126
unionAddMissingProperties: false,
127127
propertySortOrder: 'Specification',
128+
factoryMethods: {
129+
generate: false,
130+
functionNamePrefix: 'create',
131+
mode: 'inline-with-schema',
132+
outputDirectory: '',
133+
optionalPropertyStrategy: 'omit',
134+
},
128135
...overrides,
129136
} satisfies NormalizedOutputOptions;
130137

packages/angular/src/http-resource.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ const createOutput = (
135135
optionsParamRequired: false,
136136
unionAddMissingProperties: false,
137137
propertySortOrder: 'Specification',
138+
factoryMethods: {
139+
generate: false,
140+
functionNamePrefix: 'create',
141+
mode: 'inline-with-schema',
142+
outputDirectory: '',
143+
optionalPropertyStrategy: 'omit',
144+
},
138145
...overrides,
139146
} satisfies NormalizedOutputOptions;
140147

0 commit comments

Comments
 (0)