Skip to content

Commit 0b103fd

Browse files
committed
Raise the minimum supported version of AsciidoctorJ to 3.0
Closes gh-957
1 parent 85ebc4e commit 0b103fd

File tree

9 files changed

+118
-37
lines changed

9 files changed

+118
-37
lines changed

Diff for: docs/src/docs/asciidoc/getting-started.adoc

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ If you want to use `WebTestClient` or REST Assured rather than MockMvc, add a de
7878
<4> Add `spring-restdocs-asciidoctor` as a dependency of the Asciidoctor plugin.
7979
This will automatically configure the `snippets` attribute for use in your `.adoc` files to point to `target/generated-snippets`.
8080
It will also allow you to use the `operation` block macro.
81+
It requires AsciidoctorJ 3.0.
8182

8283
[source,indent=0,subs="verbatim,attributes",role="secondary"]
8384
.Gradle
@@ -114,6 +115,7 @@ It will also allow you to use the `operation` block macro.
114115
<3> Add a dependency on `spring-restdocs-asciidoctor` in the `asciidoctorExt` configuration.
115116
This will automatically configure the `snippets` attribute for use in your `.adoc` files to point to `build/generated-snippets`.
116117
It will also allow you to use the `operation` block macro.
118+
It requires AsciidoctorJ 3.0.
117119
<4> Add a dependency on `spring-restdocs-mockmvc` in the `testImplementation` configuration.
118120
If you want to use `WebTestClient` or REST Assured rather than MockMvc, add a dependency on `spring-restdocs-webtestclient` or `spring-restdocs-restassured` respectively instead.
119121
<5> Configure a `snippetsDir` property that defines the output location for generated snippets.

Diff for: docs/src/docs/asciidoc/working-with-asciidoctor.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ This section covers how to include Asciidoc snippets.
2828

2929
You can use a macro named `operation` to import all or some of the snippets that have been generated for a specific operation.
3030
It is made available by including `spring-restdocs-asciidoctor` in your project's <<getting-started-build-configuration, build configuration>>.
31+
`spring-restdocs-asciidoctor` requires AsciidoctorJ 3.0.
3132

3233
The target of the macro is the name of the operation.
3334
In its simplest form, you can use the macro to include all of the snippets for an operation, as shown in the following example:

Diff for: spring-restdocs-asciidoctor/build.gradle

-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
plugins {
22
id "java-library"
33
id "maven-publish"
4-
id "io.spring.compatibility-test" version "0.0.3"
54
}
65

76
description = "Spring REST Docs Asciidoctor Extension"
@@ -20,11 +19,3 @@ dependencies {
2019

2120
testRuntimeOnly("org.asciidoctor:asciidoctorj-pdf")
2221
}
23-
24-
compatibilityTest {
25-
dependency("AsciidoctorJ") { asciidoctorj ->
26-
asciidoctorj.groupId = "org.asciidoctor"
27-
asciidoctorj.artifactId = "asciidoctorj"
28-
asciidoctorj.versions = ["3.0.0"]
29-
}
30-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2014-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.restdocs.asciidoctor;
18+
19+
import org.asciidoctor.ast.Document;
20+
import org.asciidoctor.extension.Preprocessor;
21+
import org.asciidoctor.extension.PreprocessorReader;
22+
import org.asciidoctor.extension.Reader;
23+
24+
/**
25+
* {@link Preprocessor} that sets defaults for REST Docs-related {@link Document}
26+
* attributes.
27+
*
28+
* @author Andy Wilkinson
29+
*/
30+
final class DefaultAttributesPreprocessor extends Preprocessor {
31+
32+
private final SnippetsDirectoryResolver snippetsDirectoryResolver = new SnippetsDirectoryResolver();
33+
34+
@Override
35+
public Reader process(Document document, PreprocessorReader reader) {
36+
document.setAttribute("snippets", this.snippetsDirectoryResolver.getSnippetsDirectory(document.getAttributes()),
37+
false);
38+
return reader;
39+
}
40+
41+
}

Diff for: spring-restdocs-asciidoctor/src/main/java/org/springframework/restdocs/asciidoctor/RestDocsExtensionRegistry.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2024 the original author or authors.
2+
* Copyright 2014-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,9 +28,7 @@ public final class RestDocsExtensionRegistry implements ExtensionRegistry {
2828

2929
@Override
3030
public void register(Asciidoctor asciidoctor) {
31-
asciidoctor.rubyExtensionRegistry()
32-
.loadClass(RestDocsExtensionRegistry.class.getResourceAsStream("/extensions/default_attributes.rb"))
33-
.preprocessor("DefaultAttributes");
31+
asciidoctor.javaExtensionRegistry().preprocessor(new DefaultAttributesPreprocessor());
3432
asciidoctor.rubyExtensionRegistry()
3533
.loadClass(RestDocsExtensionRegistry.class.getResourceAsStream("/extensions/operation_block_macro.rb"))
3634
.blockMacro("operation", "OperationBlockMacro");

Diff for: spring-restdocs-asciidoctor/src/main/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolver.java

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2024 the original author or authors.
2+
* Copyright 2014-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,14 +29,9 @@
2929
*
3030
* @author Andy Wilkinson
3131
*/
32-
public class SnippetsDirectoryResolver {
32+
class SnippetsDirectoryResolver {
3333

34-
/**
35-
* Returns the snippets directory derived from the given {@code attributes}.
36-
* @param attributes the attributes
37-
* @return the snippets directory
38-
*/
39-
public File getSnippetsDirectory(Map<String, Object> attributes) {
34+
File getSnippetsDirectory(Map<String, Object> attributes) {
4035
if (System.getProperty("maven.home") != null) {
4136
return getMavenSnippetsDirectory(attributes);
4237
}

Diff for: spring-restdocs-asciidoctor/src/main/resources/extensions/default_attributes.rb

-14
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2014-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.restdocs.asciidoctor;
18+
19+
import java.io.File;
20+
21+
import org.asciidoctor.Asciidoctor;
22+
import org.asciidoctor.Attributes;
23+
import org.asciidoctor.Options;
24+
import org.junit.Test;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link DefaultAttributesPreprocessor}.
30+
*
31+
* @author Andy Wilkinson
32+
*/
33+
public class DefaultAttributesPreprocessorTests {
34+
35+
@Test
36+
public void snippetsAttributeIsSet() {
37+
String converted = createAsciidoctor().convert("{snippets}", createOptions("projectdir=../../.."));
38+
assertThat(converted).contains("build" + File.separatorChar + "generated-snippets");
39+
}
40+
41+
@Test
42+
public void snippetsAttributeFromConvertArgumentIsNotOverridden() {
43+
String converted = createAsciidoctor().convert("{snippets}",
44+
createOptions("snippets=custom projectdir=../../.."));
45+
assertThat(converted).contains("custom");
46+
}
47+
48+
@Test
49+
public void snippetsAttributeFromDocumentPreambleIsNotOverridden() {
50+
String converted = createAsciidoctor().convert(":snippets: custom\n{snippets}",
51+
createOptions("projectdir=../../.."));
52+
assertThat(converted).contains("custom");
53+
}
54+
55+
private Options createOptions(String attributes) {
56+
Options options = Options.builder().build();
57+
options.setAttributes(Attributes.builder().arguments(attributes).build());
58+
return options;
59+
}
60+
61+
private Asciidoctor createAsciidoctor() {
62+
Asciidoctor asciidoctor = Asciidoctor.Factory.create();
63+
asciidoctor.javaExtensionRegistry().preprocessor(new DefaultAttributesPreprocessor());
64+
return asciidoctor;
65+
}
66+
67+
}

Diff for: spring-restdocs-platform/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ dependencies {
1515
api("org.apache.pdfbox:pdfbox:2.0.27")
1616
api("org.apache.tomcat.embed:tomcat-embed-core:11.0.2")
1717
api("org.apache.tomcat.embed:tomcat-embed-el:11.0.2")
18-
api("org.asciidoctor:asciidoctorj:2.5.7")
19-
api("org.asciidoctor:asciidoctorj-pdf:2.3.3")
18+
api("org.asciidoctor:asciidoctorj:3.0.0")
19+
api("org.asciidoctor:asciidoctorj-pdf:2.3.19")
2020
api("org.assertj:assertj-core:3.23.1")
2121
api("org.hamcrest:hamcrest-core:1.3")
2222
api("org.hamcrest:hamcrest-library:1.3")

0 commit comments

Comments
 (0)