From c5284009a11e87f7001f4c137b7d5313ec1c842c Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Wed, 27 Jan 2021 15:15:40 +0100 Subject: [PATCH] Fix UriComponentsBuilder examples in ref docs Closes gh-26453 --- .../web/util/UriComponentsBuilderTests.java | 23 ++++++++++++++++++- src/docs/asciidoc/web/web-uris.adoc | 12 +++++----- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java b/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java index acce20fd60..92fbacb8c4 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -51,6 +51,27 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException */ class UriComponentsBuilderTests { + @Test // see gh-26453 + void examplesInReferenceManual() { + final String expected = "/hotel%20list/New%20York?q=foo%2Bbar"; + + URI uri = UriComponentsBuilder.fromPath("/hotel list/{city}") + .queryParam("q", "{q}") + .encode() + .buildAndExpand("New York", "foo+bar") + .toUri(); + assertThat(uri).asString().isEqualTo(expected); + + uri = UriComponentsBuilder.fromPath("/hotel list/{city}") + .queryParam("q", "{q}") + .build("New York", "foo+bar"); + assertThat(uri).asString().isEqualTo(expected); + + uri = UriComponentsBuilder.fromUriString("/hotel list/{city}?q={q}") + .build("New York", "foo+bar"); + assertThat(uri).asString().isEqualTo(expected); + } + @Test void plain() throws URISyntaxException { UriComponentsBuilder builder = UriComponentsBuilder.newInstance(); diff --git a/src/docs/asciidoc/web/web-uris.adoc b/src/docs/asciidoc/web/web-uris.adoc index ec1e48d451..92564d9524 100644 --- a/src/docs/asciidoc/web/web-uris.adoc +++ b/src/docs/asciidoc/web/web-uris.adoc @@ -82,7 +82,7 @@ as the following example shows: .build("Westin", "123") ---- -You shorter it further still with a full URI template, as the following example shows: +You can shorten it further still with a full URI template, as the following example shows: [source,java,indent=0,subs="verbatim,quotes",role="primary"] .Java @@ -94,7 +94,7 @@ You shorter it further still with a full URI template, as the following example [source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] .Kotlin ---- -val uri = UriComponentsBuilder + val uri = UriComponentsBuilder .fromUriString("https://example.com/hotels/{hotel}?q={q}") .build("Westin", "123") ---- @@ -250,7 +250,7 @@ as the following example shows: ---- URI uri = UriComponentsBuilder.fromPath("/hotel list/{city}") .queryParam("q", "{q}") - .build("New York", "foo+bar") + .build("New York", "foo+bar"); ---- [source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] .Kotlin @@ -265,13 +265,13 @@ You can shorten it further still with a full URI template, as the following exam [source,java,indent=0,subs="verbatim,quotes",role="primary"] .Java ---- - URI uri = UriComponentsBuilder.fromPath("/hotel list/{city}?q={q}") - .build("New York", "foo+bar") + URI uri = UriComponentsBuilder.fromUriString("/hotel list/{city}?q={q}") + .build("New York", "foo+bar"); ---- [source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] .Kotlin ---- - val uri = UriComponentsBuilder.fromPath("/hotel list/{city}?q={q}") + val uri = UriComponentsBuilder.fromUriString("/hotel list/{city}?q={q}") .build("New York", "foo+bar") ---- -- GitLab