Skip to content

Commit dbbe2b8

Browse files
man-at-homejimschubert
authored andcommitted
[asciidoc] fix names of parameters (#4440)
Fixes #4408 * fix header hierarchy in model section * small fix markup on dataformat enum (italic) * fixes parameter names casing
1 parent 7e6e87a commit dbbe2b8

File tree

9 files changed

+1618
-168
lines changed

9 files changed

+1618
-168
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AsciidocDocumentationCodegen.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ public void execute(final Template.Fragment frag, final Writer out) throws IOExc
8181
if (Files.isRegularFile(filePathToInclude)) {
8282
LOGGER.debug(
8383
"including " + ++includeCount + ". file into markup from: " + filePathToInclude.toString());
84-
out.write("\ninclude::" + relativeFileName + "[]\n");
84+
out.write("\ninclude::" + relativeFileName + "[opts=optional]\n");
8585
} else {
8686
LOGGER.debug(++notFoundCount + ". file not found, skip include for: " + filePathToInclude.toString());
87-
out.write("\n// markup not found, no include ::" + relativeFileName + "[]\n");
87+
out.write("\n// markup not found, no include ::" + relativeFileName + "[opts=optional]\n");
8888
}
8989
}
9090
}
@@ -158,6 +158,8 @@ public CodegenType getTag() {
158158
*/
159159
static String sanitize(final String name) {
160160
String sanitized = name == null ? "" : name.trim();
161+
sanitized = sanitized.replace("//", "/"); // rest paths may or may not end with slashes, leading to redundant
162+
// path separators.
161163
return sanitized.startsWith(File.separator) || sanitized.startsWith("/") ? sanitized.substring(1) : sanitized;
162164
}
163165

@@ -200,10 +202,10 @@ public AsciidocDocumentationCodegen() {
200202

201203
cliOptions.add(new CliOption(SNIPPET_DIR,
202204
"path with includable markup snippets (e.g. test output generated by restdoc, default: .")
203-
.defaultValue("."));
205+
.defaultValue("."));
204206
cliOptions.add(new CliOption(SPEC_DIR,
205207
"path with includable markup spec files (e.g. handwritten additional docs, default: .")
206-
.defaultValue(".."));
208+
.defaultValue(".."));
207209

208210
additionalProperties.put("appName", "OpenAPI Sample description");
209211
additionalProperties.put("appDescription", "A sample OpenAPI documentation");

modules/openapi-generator/src/main/resources/asciidoc-documentation/index.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
:keywords: openapi, rest, {{appName}}
99
:specDir: {{specDir}}
1010
:snippetDir: {{snippetDir}}
11-
:generator-template: v1 2019-09-03
11+
:generator-template: v1 2019-11-10
1212
:info-url: {{infoUrl}}
1313
:app-name: {{appName}}
1414

modules/openapi-generator/src/main/resources/asciidoc-documentation/model.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{{#model}}
66

77
[#{{classname}}]
8-
==== _{{classname}}_ {{title}}
8+
=== _{{classname}}_ {{title}}
99

1010
{{unescapedDescription}}
1111

@@ -15,11 +15,11 @@
1515
| Field Name| Required| Type| Description| Format
1616

1717
{{#vars}}
18-
| {{name}}
18+
| {{baseName}}
1919
| {{#required}}X{{/required}}
2020
| {{dataType}} {{#isContainer}} of <<{{complexType}}>>{{/isContainer}}
2121
| {{description}}
22-
| {{{dataFormat}}} {{#isEnum}}Enum: _ {{#_enum}}{{this}}, {{/_enum}}{{/isEnum}} _
22+
| {{{dataFormat}}} {{#isEnum}}_Enum:_ {{#_enum}}{{this}}, {{/_enum}}{{/isEnum}}
2323

2424
{{/vars}}
2525
|===

modules/openapi-generator/src/main/resources/asciidoc-documentation/param.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
| {{paramName}}
1+
| {{baseName}}
22
| {{description}} {{#baseType}}<<{{baseType}}>>{{/baseType}}
33
| {{^required}}-{{/required}}{{#required}}X{{/required}}
44
| {{defaultValue}}

modules/openapi-generator/src/test/java/org/openapitools/codegen/asciidoc/AsciidocSampleGeneratorTest.java

Lines changed: 72 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,25 @@
1212
import org.openapitools.codegen.config.CodegenConfigurator;
1313
import org.openapitools.codegen.languages.AsciidocDocumentationCodegen;
1414
import org.testng.Assert;
15+
import org.testng.annotations.AfterClass;
16+
import org.testng.annotations.BeforeClass;
1517
import org.testng.annotations.Test;
1618

19+
/** several asciidoc content checks with sample openapi v3. */
1720
public class AsciidocSampleGeneratorTest {
1821

19-
/**
20-
* ensure api-docs.json includes sample and spec files into markup.
21-
* @throws Exception generic exception
22-
*/
23-
@Test
24-
public void testSampleAsciidocMarkupGenerationFromJsonWithSpecsAndSamples() throws Exception {
22+
public String markupContent = null;
23+
public String markupFileName = null;
2524

26-
File outputTempDirectory = Files.createTempDirectory("test-asciidoc-sample-generator.").toFile();
25+
File specDir = new File("src/test/resources/3_0/asciidoc/specs/");
26+
File snippetDir = new File("src/test/resources/3_0/asciidoc/generated-snippets/");
2727

28-
File specDir = new File("src/test/resources/3_0/asciidoc/specs/");
29-
File snippetDir = new File("src/test/resources/3_0/asciidoc/generated-snippets/");
28+
@BeforeClass
29+
public void beforeClassGenerateTestMarkup() throws Exception {
3030

31-
Assert.assertTrue(specDir.exists(), "test cancel, not specdDir found to use." + specDir.getPath());
31+
File outputTempDirectory = Files.createTempDirectory("test-asciidoc-sample-generator.").toFile();
32+
33+
Assert.assertTrue(specDir.exists(), "test cancel, not specDir found to use." + specDir.getPath());
3234
Assert.assertTrue(snippetDir.exists(), "test cancel, not snippedDir found to use." + snippetDir.getPath());
3335

3436
final CodegenConfigurator configurator = new CodegenConfigurator().setGeneratorName("asciidoc")
@@ -40,46 +42,77 @@ public void testSampleAsciidocMarkupGenerationFromJsonWithSpecsAndSamples() thro
4042
DefaultGenerator generator = new DefaultGenerator();
4143
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
4244

43-
boolean markupFileGenerated = false;
44-
4545
for (File file : files) {
4646
if (file.getName().equals("index.adoc")) {
47-
markupFileGenerated = true;
48-
String markupContent = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
47+
this.markupFileName = file.getAbsoluteFile().toString();
48+
this.markupContent = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
49+
}
50+
}
51+
}
4952

50-
// include correct values from cli.
51-
Assert.assertTrue(markupContent.contains(":specDir: " + specDir.toString()),
52-
"expected :specDir: in: " + markupContent.substring(0, 350));
53-
Assert.assertTrue(markupContent.contains(":snippetDir: " + snippetDir.toString()),
54-
"expected :snippetDir: in: " + markupContent.substring(0, 350));
53+
@AfterClass
54+
public void afterClassCleanUpTestMarkup() throws Exception {
55+
if (this.markupFileName != null) {
56+
Files.deleteIfExists(Paths.get(this.markupFileName));
57+
}
58+
}
5559

56-
// include correct markup from separate directories, relative links
57-
Assert.assertTrue(markupContent.contains("include::rest/project/GET/spec.adoc[]"),
58-
"expected project spec.adoc to be included in " + file.getAbsolutePath());
60+
@Test
61+
public void testMarkupExistence() {
62+
Assert.assertNotNull(this.markupContent, "asciidoc content index.adoc not created.");
63+
}
5964

60-
Assert.assertTrue(markupContent.contains("include::rest/project/GET/implementation.adoc[]"),
61-
"expected project implementation.adoc to be included in " + file.getAbsolutePath());
65+
/**
66+
* ensure api-docs.json includes sample and spec files directory as attributes.
67+
*/
68+
@Test
69+
public void testSampleAsciidocMarkupGenerationFromJsonWithAttributes() throws Exception {
70+
Assert.assertTrue(markupContent.contains(":specDir: " + specDir.toString()),
71+
"expected :specDir: in: " + markupContent.substring(0, 350));
72+
Assert.assertTrue(markupContent.contains(":snippetDir: " + snippetDir.toString()),
73+
"expected :snippetDir: in: " + markupContent.substring(0, 350));
74+
}
75+
76+
/**
77+
* ensure api-docs.json includes sample and spec files into markup.
78+
*/
79+
@Test
80+
public void testSampleAsciidocMarkupGenerationFromJsonWithIncludes() throws Exception {
6281

63-
Assert.assertTrue(markupContent.contains("include::rest/project/GET/http-request.adoc[]"),
64-
"expected project http-request.adoc to be included in " + file.getAbsolutePath());
82+
// include correct markup from separate directories, relative links
83+
Assert.assertTrue(markupContent.contains("include::rest/project/GET/spec.adoc["),
84+
"expected project spec.adoc to be included in " + markupFileName);
6585

66-
Assert.assertTrue(markupContent.contains("include::rest/project/GET/http-response.adoc[]"),
67-
"expected project http-response.adoc to be included in " + file.getAbsolutePath());
86+
Assert.assertTrue(markupContent.contains("include::rest/project/GET/implementation.adoc["),
87+
"expected project implementation.adoc to be included in " + markupFileName);
6888

69-
Assert.assertTrue(markupContent.contains("link:rest/project/GET/GET.json["),
70-
"expected link: not found in file: " + file.getAbsoluteFile());
89+
Assert.assertTrue(markupContent.contains("include::rest/project/GET/http-request.adoc["),
90+
"expected project http-request.adoc to be included in " + markupFileName);
7191

72-
// extract correct value from json
73-
Assert.assertTrue(markupContent.contains("= time@work rest api"),
74-
"missing main header for api spec from json: " + markupContent.substring(0, 100));
75-
}
76-
Files.deleteIfExists(Paths.get(file.getAbsolutePath()));
77-
}
92+
Assert.assertTrue(markupContent.contains("include::rest/project/GET/http-response.adoc["),
93+
"expected project http-response.adoc to be included in " + markupFileName);
7894

79-
Assert.assertTrue(markupFileGenerated, "index.adoc is not generated!");
95+
Assert.assertTrue(markupContent.contains("link:rest/project/GET/GET.json["),
96+
"expected link: not found in file: " + markupFileName);
97+
}
8098

81-
Files.deleteIfExists(Paths.get(outputTempDirectory.getAbsolutePath(), ".openapi-generator"));
82-
Files.deleteIfExists(Paths.get(outputTempDirectory.getAbsolutePath()));
99+
/**
100+
* markup doc header content.
101+
*/
102+
@Test
103+
public void testSampleAsciidocMarkupGenerationFromJsonWithContent() throws Exception {
104+
Assert.assertTrue(markupContent.contains("= time@work rest api"),
105+
"missing main header for api spec from json: " + markupContent.substring(0, 100));
106+
107+
}
108+
109+
/**
110+
* fix: parameter name unchanged.
111+
*/
112+
@Test
113+
public void testSampleAsciidocMarkupGenerationParameterNameUnchanged() throws Exception {
114+
Assert.assertTrue(markupContent.contains("from-iso-date-string"),
115+
"keep parameter name from-iso-date-string unchanged.");
83116
}
84117

85118
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/asciidoc/IncludeMarkupFilterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void testIncludeMarkupFilterDoesNotIncludeMissingFile() {
2626
final Map<String, Object> ctx = context("specinclude", generator.new IncludeMarkupLambda("DOES_NOT_EXIST"));
2727

2828
final String result = execute("{{#specinclude}}not.an.existing.file.adoc{{/specinclude}}", ctx);
29-
Assert.assertTrue(result.contains("// markup not found, no include ::not.an.existing.file.adoc[]"),
29+
Assert.assertTrue(result.contains("// markup not found, no include ::not.an.existing.file.adoc["),
3030
"unexpected filtered " + result);
3131
}
3232

modules/openapi-generator/src/test/resources/3_0/asciidoc/api-docs.json

Lines changed: 1416 additions & 1 deletion
Large diffs are not rendered by default.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.1.3-SNAPSHOT
1+
4.2.1-SNAPSHOT

0 commit comments

Comments
 (0)