From eaddc1b769aa1b7af1c6d4c46ae9c02a44c6798d Mon Sep 17 00:00:00 2001 From: petekirby-ee Date: Thu, 1 Jun 2023 09:23:07 +0100 Subject: [PATCH] API-7009 Fix invalid IDs for links in API documentation table (#462) --- .../models/Documentation.scala | 1 + .../views/include/documentGroups.scala.html | 2 +- .../models/DocumentationSpec.scala | 42 +++++++++++++------ 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/app/uk/gov/hmrc/apidocumentation/models/Documentation.scala b/app/uk/gov/hmrc/apidocumentation/models/Documentation.scala index 9ec6e7bd..ecfa1f75 100644 --- a/app/uk/gov/hmrc/apidocumentation/models/Documentation.scala +++ b/app/uk/gov/hmrc/apidocumentation/models/Documentation.scala @@ -46,6 +46,7 @@ trait Documentation { lazy val isRestOrXmlApi = label == REST_API || label == XML_API + lazy val nameAsId = name.toLowerCase().replaceAll(" ", "-").replaceAll("[^a-z0-9-]", "") } object Documentation { diff --git a/app/uk/gov/hmrc/apidocumentation/views/include/documentGroups.scala.html b/app/uk/gov/hmrc/apidocumentation/views/include/documentGroups.scala.html index ce7f58c7..287cc45e 100644 --- a/app/uk/gov/hmrc/apidocumentation/views/include/documentGroups.scala.html +++ b/app/uk/gov/hmrc/apidocumentation/views/include/documentGroups.scala.html @@ -35,7 +35,7 @@ @if(index == 0) { @category.displayName } else { } - @document.name + @document.name @document.label.displayName diff --git a/test/uk/gov/hmrc/apidocumentation/models/DocumentationSpec.scala b/test/uk/gov/hmrc/apidocumentation/models/DocumentationSpec.scala index ab556ed8..9d7c7871 100644 --- a/test/uk/gov/hmrc/apidocumentation/models/DocumentationSpec.scala +++ b/test/uk/gov/hmrc/apidocumentation/models/DocumentationSpec.scala @@ -363,19 +363,6 @@ class DocumentationSpec extends HmrcSpec { result shouldBe Map.empty } - - def anApiDefinition(name: String, categories: Option[Seq[APICategory]] = None, isTestSupport: Option[Boolean] = None) = - APIDefinition("serviceName", name, "description", "context", None, isTestSupport, Seq(APIVersion("1.0", None, STABLE, Seq.empty)), categories) - - def anXmlApiDefinition(name: String, categories: Option[Seq[APICategory]] = None) = - XmlApiDocumentation(name, "description", "context", categories) - - def aServiceGuide(name: String, categories: Option[Seq[APICategory]] = None) = - ServiceGuide(name, "context", categories) - - def aRoadMap(name: String, categories: Option[Seq[APICategory]] = None) = - RoadMap(name, "context", categories) - } "decoratedUriPattern" should { @@ -403,4 +390,33 @@ class DocumentationSpec extends HmrcSpec { Endpoint("Get Today's Date", uriPattern, HttpMethod.GET, parameters) } } + + "nameAsId" should { + "swap spaces for hyphens and lower case" in { + val api1 = anApiDefinition("Hello World") + api1.nameAsId shouldBe "hello-world" + } + + "remove brackets" in { + val api1 = anApiDefinition("Income Tax (MTD) end-to-end service guide") + api1.nameAsId shouldBe "income-tax-mtd-end-to-end-service-guide" + } + + "remove any other chars" in { + val api1 = anApiDefinition("Income Tax (MTD):+{}=#@£!& [end-to-end service guide]") + api1.nameAsId shouldBe "income-tax-mtd-end-to-end-service-guide" + } + } + + def anApiDefinition(name: String, categories: Option[Seq[APICategory]] = None, isTestSupport: Option[Boolean] = None) = + APIDefinition("serviceName", name, "description", "context", None, isTestSupport, Seq(APIVersion("1.0", None, STABLE, Seq.empty)), categories) + + def anXmlApiDefinition(name: String, categories: Option[Seq[APICategory]] = None) = + XmlApiDocumentation(name, "description", "context", categories) + + def aServiceGuide(name: String, categories: Option[Seq[APICategory]] = None) = + ServiceGuide(name, "context", categories) + + def aRoadMap(name: String, categories: Option[Seq[APICategory]] = None) = + RoadMap(name, "context", categories) }