Skip to content

Commit f3cd32b

Browse files
committedOct 2, 2024··
fix: Exporting to YAML preserves nil values in examples
1 parent 3ffce33 commit f3cd32b

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed
 

‎lib/open_api_spex/open_api.ex

+5-1
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,15 @@ defmodule OpenApiSpex.OpenApi do
9696
defmodule YmlrEncoder do
9797
@moduledoc false
9898

99-
def encode(api_spec = %{}, _options) do
99+
def encode(api_spec = %OpenApi{}, _options) do
100100
api_spec
101101
|> OpenApi.to_map()
102102
|> Ymlr.document()
103103
end
104+
105+
def encode(api_spec = %{}, _options) do
106+
Ymlr.document(api_spec)
107+
end
104108
end
105109

106110
@yaml_encoder YmlrEncoder

‎test/support/tasks/openapi.json

+29
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,33 @@
11
{
2+
"components": {
3+
"schemas": {
4+
"Person": {
5+
"example": {
6+
"first_name": "John",
7+
"last_name": "Doe",
8+
"nickname": null
9+
},
10+
"properties": {
11+
"first_name": {
12+
"type": "string"
13+
},
14+
"last_name": {
15+
"type": "string"
16+
},
17+
"nickname": {
18+
"nullable": true,
19+
"type": "string"
20+
}
21+
},
22+
"required": [
23+
"first_name",
24+
"last_name",
25+
"nickname"
26+
],
27+
"type": "object"
28+
}
29+
}
30+
},
231
"info": {
332
"title": "Test spec",
433
"version": "1.0"

‎test/support/tasks/openapi.yaml

+20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
---
2+
components:
3+
schemas:
4+
Person:
5+
example:
6+
first_name: John
7+
last_name: Doe
8+
nickname:
9+
properties:
10+
first_name:
11+
type: string
12+
last_name:
13+
type: string
14+
nickname:
15+
nullable: true
16+
type: string
17+
required:
18+
- first_name
19+
- last_name
20+
- nickname
21+
type: object
222
info:
323
title: Test spec
424
version: '1.0'

‎test/support/tasks/spec_module.ex

+19-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,25 @@ defmodule OpenApiSpexTest.Tasks.SpecModule do
1818
},
1919
servers: [
2020
%Server{url: "http://localhost:4000"}
21-
]
21+
],
22+
components: %{
23+
schemas: %{
24+
"Person" => %OpenApiSpex.Schema{
25+
type: :object,
26+
properties: %{
27+
first_name: %OpenApiSpex.Schema{type: :string},
28+
last_name: %OpenApiSpex.Schema{type: :string},
29+
nickname: %OpenApiSpex.Schema{type: :string, nullable: true}
30+
},
31+
required: [:first_name, :last_name, :nickname],
32+
example: %{
33+
first_name: "John",
34+
last_name: "Doe",
35+
nickname: nil
36+
}
37+
}
38+
}
39+
}
2240
}
2341
end
2442
end

0 commit comments

Comments
 (0)
Please sign in to comment.