Skip to content

Commit aeaaa11

Browse files
committed
feat: generate .default() in OpenAPI for fields with default
1 parent 562b45b commit aeaaa11

4 files changed

Lines changed: 39 additions & 2 deletions

File tree

docs/guide/representations/attributes/encode-decode.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,20 @@ attribute :name, empty: true, writable: true
8888

8989
The database stores `NULL` for missing values, but the frontend expects empty strings. `empty: true` handles the conversion.
9090

91-
In the Zod export, `empty: true` produces `default('')` instead of `nullable()`:
91+
In exports, `empty: true` produces a default empty string instead of `nullable`:
9292

9393
```typescript
94+
// Zod
9495
name: z.string().default('')
9596
```
9697

98+
```yaml
99+
# OpenAPI
100+
name:
101+
type: string
102+
default: ''
103+
```
104+
97105
## Examples
98106
99107
- [Value Transforms](/examples/value-transforms) — Transform values during serialization and handle nil/empty conversion

docs/guide/types/modifiers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ interface Example {
121121
const ExampleSchema = z.object({
122122
title: z.string().min(1).max(255),
123123
count: z.number().int().min(0).optional(),
124-
notes: z.string().nullable().optional(),
124+
notes: z.string().nullable().default(null),
125125
});
126126
```
127127

lib/apiwork/export/open_api.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,12 @@ def map_field(param)
324324

325325
schema[:format] = param.format.to_s if param.formattable? && param.format
326326

327+
if param.respond_to?(:default) && !param.default.nil?
328+
schema[:default] = param.default
329+
elsif param.optional? && param.nullable?
330+
schema[:default] = nil
331+
end
332+
327333
apply_nullable(schema, param.nullable?)
328334
end
329335

spec/apiwork/export/open_api_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
string :old_field, deprecated: true
5353
string :email, format: :email
5454
integer :min_field, max: 100, min: 0
55+
boolean :archived, default: false, optional: true
56+
string :category, default: 'general', optional: true
57+
string :memo, nullable: true, optional: true
5558
end
5659

5760
object :receipt do
@@ -237,6 +240,26 @@
237240
end
238241
end
239242

243+
context 'with default values' do
244+
it 'includes null default for nullable optional field' do
245+
property = schemas['invoice'][:properties]['memo']
246+
247+
expect(property[:default]).to be_nil
248+
end
249+
250+
it 'includes boolean default' do
251+
property = schemas['invoice'][:properties]['archived']
252+
253+
expect(property[:default]).to be(false)
254+
end
255+
256+
it 'includes string default' do
257+
property = schemas['invoice'][:properties]['category']
258+
259+
expect(property[:default]).to eq('general')
260+
end
261+
end
262+
240263
context 'with object types' do
241264
it 'generates properties and required fields' do
242265
invoice_schema = schemas['invoice']

0 commit comments

Comments
 (0)