Skip to content

Commit

Permalink
fix: Exporting to YAML preserves nil values in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
zorbash committed Oct 2, 2024
1 parent 3ffce33 commit f3cd32b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/open_api_spex/open_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,15 @@ defmodule OpenApiSpex.OpenApi do
defmodule YmlrEncoder do
@moduledoc false

def encode(api_spec = %{}, _options) do
def encode(api_spec = %OpenApi{}, _options) do
api_spec
|> OpenApi.to_map()
|> Ymlr.document()
end

def encode(api_spec = %{}, _options) do
Ymlr.document(api_spec)
end
end

@yaml_encoder YmlrEncoder
Expand Down
29 changes: 29 additions & 0 deletions test/support/tasks/openapi.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
{
"components": {
"schemas": {
"Person": {
"example": {
"first_name": "John",
"last_name": "Doe",
"nickname": null
},
"properties": {
"first_name": {
"type": "string"
},
"last_name": {
"type": "string"
},
"nickname": {
"nullable": true,
"type": "string"
}
},
"required": [
"first_name",
"last_name",
"nickname"
],
"type": "object"
}
}
},
"info": {
"title": "Test spec",
"version": "1.0"
Expand Down
20 changes: 20 additions & 0 deletions test/support/tasks/openapi.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
---
components:
schemas:
Person:
example:
first_name: John
last_name: Doe
nickname:
properties:
first_name:
type: string
last_name:
type: string
nickname:
nullable: true
type: string
required:
- first_name
- last_name
- nickname
type: object
info:
title: Test spec
version: '1.0'
Expand Down
20 changes: 19 additions & 1 deletion test/support/tasks/spec_module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,25 @@ defmodule OpenApiSpexTest.Tasks.SpecModule do
},
servers: [
%Server{url: "http://localhost:4000"}
]
],
components: %{
schemas: %{
"Person" => %OpenApiSpex.Schema{
type: :object,
properties: %{
first_name: %OpenApiSpex.Schema{type: :string},
last_name: %OpenApiSpex.Schema{type: :string},
nickname: %OpenApiSpex.Schema{type: :string, nullable: true}
},
required: [:first_name, :last_name, :nickname],
example: %{
first_name: "John",
last_name: "Doe",
nickname: nil
}
}
}
}
}
end
end

0 comments on commit f3cd32b

Please sign in to comment.