Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documenting enum schemas #304

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions example/source/pets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,19 @@ components:
type: string
tag:
type: string
status:
$ref: "#/components/schemas/PetStatus"
Pets:
type: array
items:
$ref: "#/components/schemas/Pet"
PetStatus:
type: string
description: pet status in the store
enum:
- pending
- available
- sold
Error:
required:
- code
Expand Down
8 changes: 8 additions & 0 deletions lib/govuk_tech_docs/api_reference/templates/schema.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@
</tbody>
</table>
<% end %>
<% if schema.enum %>
<p>This schema can be any one of the following <%= schema.type.pluralize %>:</p>
<ul class='<%= id.parameterize %>-enum'>
<% schema.enum.sort.each do |value| %>
<li><%= value %></li>
<% end %>
</ul>
<% end %>
42 changes: 42 additions & 0 deletions spec/api_reference/renderer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,47 @@
expect(rendered).to have_css("div#server-list>p>strong", text: "Development")
expect(rendered).to have_css("div#server-list>p>strong>p>em", text: "Development")
end

it "renders a schema" do
@spec["components"] = {
"schemas": {
"Pet": {
"properties": {
"id": { "type": "integer", "format": "int64" },
},
},
},
}
document = Openapi3Parser.load(@spec)

render = described_class.new(@app, document)
rendered = render.api_full(document.info, document.servers)

rendered = Capybara::Node::Simple.new(rendered)
expect(rendered).to have_css("h2#schemas", text: "Schemas")
expect(rendered).to have_css("h3#schema-pet", text: "Pet")
expect(rendered).to have_css("table.schema-pet", text: "id")
end

it "renders an enum schema" do
@spec["components"] = {
"schemas": {
"Pet": {
"type": "string",
"enum": %w[pending available sold],
},
},
}
document = Openapi3Parser.load(@spec)

render = described_class.new(@app, document)
rendered = render.api_full(document.info, document.servers)

rendered = Capybara::Node::Simple.new(rendered)
expect(rendered).to have_css("h3#schema-pet", text: "Pet")
expect(rendered).to have_css(".schema-pet-enum", text: "pending")
expect(rendered).to have_css(".schema-pet-enum", text: "available")
expect(rendered).to have_css(".schema-pet-enum", text: "sold")
end
end
end