From e3dc6b15806654fc99d16884a5956f833da76239 Mon Sep 17 00:00:00 2001 From: Jake Benilov Date: Thu, 23 Jun 2022 17:12:47 +0100 Subject: [PATCH] Support for schemas with enums If a schema provides an list of enum values, generate them as a (sorted) list --- .../api_reference/templates/schema.html.erb | 8 +++++++ spec/api_reference/renderer_spec.rb | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/govuk_tech_docs/api_reference/templates/schema.html.erb b/lib/govuk_tech_docs/api_reference/templates/schema.html.erb index 051f43d6..8747e2cb 100644 --- a/lib/govuk_tech_docs/api_reference/templates/schema.html.erb +++ b/lib/govuk_tech_docs/api_reference/templates/schema.html.erb @@ -27,3 +27,11 @@ <% end %> +<% if schema.enum %> +

This schema can be any one of the following <%= schema.type.pluralize %>:

+ +<% end %> diff --git a/spec/api_reference/renderer_spec.rb b/spec/api_reference/renderer_spec.rb index 913ea8c9..745279ff 100644 --- a/spec/api_reference/renderer_spec.rb +++ b/spec/api_reference/renderer_spec.rb @@ -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