diff --git a/http-generator-core/src/main/java/io/avaje/http/generator/core/javadoc/JavadocParser.java b/http-generator-core/src/main/java/io/avaje/http/generator/core/javadoc/JavadocParser.java index 79b3c446d..2d18bbe05 100644 --- a/http-generator-core/src/main/java/io/avaje/http/generator/core/javadoc/JavadocParser.java +++ b/http-generator-core/src/main/java/io/avaje/http/generator/core/javadoc/JavadocParser.java @@ -19,22 +19,25 @@ class JavadocParser { private static final int PARAM_DESC = 7; private static final int RETURN_DESC = 8; private static final int IGNORE = 9; + private static final int CODE = 10; private static final String DEPRECATED = "deprecated"; private int previousState = TEXT; - private StringBuilder currentParam = new StringBuilder(); - private StringBuilder currentDoclet = new StringBuilder(); + private final StringBuilder currentParam = new StringBuilder(); + private final StringBuilder currentDoclet = new StringBuilder(); private StringBuilder currentContent; private int state = TEXT; private String returnDesc = ""; - private Map params = new LinkedHashMap<>(); + private final Map params = new LinkedHashMap<>(); private boolean deprecated; +private boolean hasReturn; + /** * Parse the javadoc. */ @@ -48,6 +51,7 @@ Javadoc parse(String text) { currentContent = mainContent; for (char c : text.toCharArray()) { + switch (state) { case RETURN_DESC: if (c == '\n') { @@ -57,6 +61,11 @@ Javadoc parse(String text) { if (state == PARAM_DESC) { processSetParam(); } + case CODE: + if (state == CODE && c == '}') { + state = previousState; + break; + } case TEXT: processText(c); continue; @@ -77,32 +86,44 @@ Javadoc parse(String text) { } } - if (state == RETURN_DESC) { - returnDesc = currentContent.toString(); + if (hasReturn) { + returnDesc = mergeLines(currentContent.toString()); } return splitMain(mainContent.toString().trim()); } private void processSetParam() { - params.put(currentParam.toString(), currentContent.toString().trim()); + params.put(currentParam.toString(), mergeLines(currentContent.toString().trim())); } private void processReturnDesc(StringBuilder mainContent) { state = TEXT; previousState = TEXT; returnDesc = currentContent.toString(); - currentContent = mainContent; + currentContent = new StringBuilder(returnDesc); } private void processText(char c) { - if (c == '{' || c == '@') { - currentDoclet.delete(0, currentDoclet.length()); - state = DOCLET_START; - } else if (c == '<') { - state = TAG_START; - } else if (c != '}' && c != '>') { - currentContent.append(c); + switch (c) { + case '{': + case '@': + currentDoclet.delete(0, currentDoclet.length()); + state = DOCLET_START; + break; + case '<': + state = TAG_START; + break; + case '\n': + if (state == CODE) { + currentContent.append("\\n"); + return; + } + default: + if (c != '}' && c != '>') { + currentContent.append(c); + } + break; } } @@ -132,23 +153,22 @@ private void processDocletStart(char c) { deprecated = true; } state = IGNORE; - } else { - if (docletName.equals("param")) { - currentParam.delete(0, currentParam.length()); - state = PARAM_NAME; - } - if (docletName.equals("return")) { - currentContent = new StringBuilder(); - state = RETURN_DESC; - previousState = RETURN_DESC; - } + } else if ("param".equals(docletName)) { + currentParam.delete(0, currentParam.length()); + state = PARAM_NAME; + } else if ("return".equals(docletName)) { + currentContent = new StringBuilder(); + state = RETURN_DESC; + previousState = RETURN_DESC; + hasReturn=true; + } else if ("@code".equals(docletName)) { + state = CODE; } } else { currentDoclet.append(c); } } - private Javadoc splitMain(String mainText) { String desc = ""; diff --git a/http-generator-core/src/test/java/io/avaje/http/generator/core/javadoc/JavadocParserTest.java b/http-generator-core/src/test/java/io/avaje/http/generator/core/javadoc/JavadocParserTest.java index 14166b45c..ef40dd149 100644 --- a/http-generator-core/src/test/java/io/avaje/http/generator/core/javadoc/JavadocParserTest.java +++ b/http-generator-core/src/test/java/io/avaje/http/generator/core/javadoc/JavadocParserTest.java @@ -145,4 +145,22 @@ public void lines() { assertThat(parser.mergeLines("\n one \n two \n three \n")).isEqualTo("one two three"); } + + @Test + public void parse_param_with_newline() { + + JavadocParser parser = new JavadocParser(); + + Javadoc doc = parser.parse( + "This is a description with bold and {@code some code}\n" + + "@since 1.0\n" + + "@param foo The\n foo param\n" + + "@param bar The {@code \n} param\n" + + "@return The {@value return}\n value\n"); + + assertEquals("This is a description with bold and some code", doc.getSummary()); + assertEquals("The foo param", doc.getParams().get("foo")); + assertEquals("The \\n param", doc.getParams().get("bar")); + assertEquals("The return value", doc.getReturnDescription()); + } }