Skip to content

Commit

Permalink
Support for schemas with enums
Browse files Browse the repository at this point in the history
If a schema provides an list of enum values, generate them as a (sorted) list
  • Loading branch information
benilovj committed Jun 23, 2022
1 parent 52d890b commit e3dc6b1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
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 %>
21 changes: 21 additions & 0 deletions spec/api_reference/renderer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,26 @@
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

0 comments on commit e3dc6b1

Please sign in to comment.