Skip to content

Commit e8aa3de

Browse files
authored
Merge pull request #695 from kmos/fix-687
Move markdown call to post with serialized JSON payload
2 parents a2091a3 + 52aeb00 commit e8aa3de

File tree

3 files changed

+106
-4
lines changed

3 files changed

+106
-4
lines changed

src/main/java/org/gitlab4j/api/MarkdownApi.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package org.gitlab4j.api;
22

3-
import javax.ws.rs.core.Form;
4-
import javax.ws.rs.core.Response;
53
import org.gitlab4j.api.GitLabApi.ApiVersion;
64
import org.gitlab4j.api.models.Markdown;
5+
import org.gitlab4j.api.models.MarkdownRequest;
6+
7+
import javax.ws.rs.core.Response;
78

89
/**
910
* This class provides an entry point to all the GitLab API markdown calls.
@@ -30,8 +31,26 @@ public Markdown getMarkdown(String text) throws GitLabApiException {
3031
throw new GitLabApiException("Api version must be v4");
3132
}
3233

33-
Form formData = new GitLabApiForm().withParam("text", text, true);
34-
Response response = post(Response.Status.OK, formData.asMap(), "markdown");
34+
return getMarkdown(new MarkdownRequest(text, true));
35+
}
36+
37+
/**
38+
* Render an arbitrary Markdown document.
39+
*
40+
* <pre><code>GitLab Endpoint: POST /api/v4/markdown</code></pre>
41+
*
42+
* @param markdownRequest a request of markdown transformation
43+
* @return a Markdown instance with transformed info
44+
* @throws GitLabApiException if any exception occurs
45+
* @since GitLab 11.0
46+
*/
47+
public Markdown getMarkdown(MarkdownRequest markdownRequest) throws GitLabApiException {
48+
49+
if (!isApiVersion(ApiVersion.V4)) {
50+
throw new GitLabApiException("Api version must be v4");
51+
}
52+
53+
Response response = post(Response.Status.OK, markdownRequest, "markdown");
3554
return (response.readEntity(Markdown.class));
3655
}
3756
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.gitlab4j.api.models;
2+
3+
public class MarkdownRequest {
4+
private String text;
5+
private boolean gfm;
6+
7+
public MarkdownRequest(String text, boolean gfm) {
8+
this.text = text;
9+
this.gfm = gfm;
10+
}
11+
12+
public String getText() {
13+
return text;
14+
}
15+
16+
public void setText(String text) {
17+
this.text = text;
18+
}
19+
20+
public boolean isGfm() {
21+
return gfm;
22+
}
23+
24+
public void setGfm(boolean gfm) {
25+
this.gfm = gfm;
26+
}
27+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.gitlab4j.api;
2+
3+
import org.gitlab4j.api.models.Markdown;
4+
import org.junit.Before;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
import org.junit.experimental.categories.Category;
8+
9+
import static org.junit.Assert.*;
10+
import static org.junit.Assume.assumeTrue;
11+
12+
@Category(IntegrationTest.class)
13+
public class TestMarkdownApi extends AbstractIntegrationTest {
14+
15+
private static final String EXPECTED_HTML_FOR_SPECIAL = "<p data-sourcepos=\"1:1-1:104\" dir=\"auto\">Hello world! <gl-emoji title=\"party popper\" data-name=\"tada\" data-unicode-version=\"6.0\">🎉</gl-emoji> <code>xml &lt;profiles&gt; &lt;version&gt;${maven-surefire-plugin.version}&lt;/version&gt; &lt;/profiles&gt;</code></p>";
16+
public static final String SPECIAL_CHAR_EXAMPLE = "Hello world! :tada: ```xml <profiles> <version>${maven-surefire-plugin.version}</version> </profiles>```";
17+
18+
public static final String NORMAL_HTML_EXAMPLE = "<h1 data-sourcepos=\"1:1-1:4\" dir=\"auto\">\n" +
19+
"<a id=\"user-content-h1\" class=\"anchor\" href=\"#h1\" aria-hidden=\"true\"></a>H1</h1>\n" +
20+
"<h2 data-sourcepos=\"2:2-2:6\" dir=\"auto\">\n" +
21+
"<a id=\"user-content-h2\" class=\"anchor\" href=\"#h2\" aria-hidden=\"true\"></a>H2</h2>\n" +
22+
"<h3 data-sourcepos=\"3:2-3:7\" dir=\"auto\">\n" +
23+
"<a id=\"user-content-h3\" class=\"anchor\" href=\"#h3\" aria-hidden=\"true\"></a>H3</h3>\n" +
24+
"<h4 data-sourcepos=\"4:2-4:8\" dir=\"auto\">\n" +
25+
"<a id=\"user-content-h4\" class=\"anchor\" href=\"#h4\" aria-hidden=\"true\"></a>H4</h4>\n" +
26+
"<h5 data-sourcepos=\"5:2-5:9\" dir=\"auto\">\n" +
27+
"<a id=\"user-content-h5\" class=\"anchor\" href=\"#h5\" aria-hidden=\"true\"></a>H5</h5>\n" +
28+
"<h6 data-sourcepos=\"6:2-6:10\" dir=\"auto\">\n" +
29+
"<a id=\"user-content-h6\" class=\"anchor\" href=\"#h6\" aria-hidden=\"true\"></a>H6</h6>";
30+
31+
private static GitLabApi gitLabApi;
32+
33+
@BeforeClass
34+
public static void setUp() throws Exception {
35+
gitLabApi = baseTestSetup();
36+
}
37+
38+
@Before
39+
public void beforeMethod() {
40+
assumeTrue(gitLabApi != null);
41+
}
42+
43+
@Test
44+
public void testMarkdownWithSpecialCharacters() throws GitLabApiException {
45+
Markdown markdown = gitLabApi.getMarkdownApi().getMarkdown(SPECIAL_CHAR_EXAMPLE);
46+
47+
assertEquals(EXPECTED_HTML_FOR_SPECIAL, markdown.getHtml());
48+
}
49+
50+
@Test
51+
public void testMarkdownWithNormalText() throws GitLabApiException {
52+
Markdown markdown = gitLabApi.getMarkdownApi().getMarkdown("# H1 \n ## H2 \n ### H3 \n #### H4 \n ##### H5 \n ###### H6");
53+
54+
assertEquals(NORMAL_HTML_EXAMPLE, markdown.getHtml());
55+
}
56+
}

0 commit comments

Comments
 (0)