Skip to content

Commit 281b5a6

Browse files
committed
improve formatter configuration (openapi-processor/openapi-processor-spring#331)
1 parent 87f9333 commit 281b5a6

File tree

13 files changed

+353
-26
lines changed

13 files changed

+353
-26
lines changed

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/writer/java/EclipseFormatter.kt

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
package io.openapiprocessor.core.writer.java
77

88
import io.openapiprocessor.core.writer.SourceFormatter
9-
import org.eclipse.jdt.core.formatter.CodeFormatter
109
import org.eclipse.jdt.core.ToolFactory
10+
import org.eclipse.jdt.core.formatter.CodeFormatter
11+
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants
1112
import org.eclipse.jface.text.Document
1213
import java.io.StringReader
1314
import java.util.*
14-
import java.util.stream.Collectors
1515

1616

17-
class EclipseFormatter: SourceFormatter {
17+
private const val LINE_SEPARATOR = "\n"
18+
19+
class EclipseFormatter : SourceFormatter {
1820
private lateinit var formatter: CodeFormatter
1921

2022
init {
@@ -29,46 +31,41 @@ class EclipseFormatter: SourceFormatter {
2931
0,
3032
raw.length,
3133
0,
32-
"\n"
34+
LINE_SEPARATOR
3335
)
3436

3537
val document = Document(raw)
3638
textEdit.apply(document)
3739

38-
return correctEndOfFile(document.get())
40+
val text = document.get()
41+
val trimmed = trimEnd(text)
42+
return trimmed
43+
3944
} catch (e: Exception) {
4045
throw FormattingException("failed to format the generated source: \n>>\n$raw\n<<", e)
4146
}
4247
}
4348

44-
private fun correctEndOfFile(formatted: String): String {
45-
return StringBuilder()
46-
.append(formatted)
47-
.append("\n")
48-
.toString()
49+
fun trimEnd(source: String): String {
50+
return source.lines()
51+
.joinToString(LINE_SEPARATOR) {
52+
it.trimEnd()
53+
}
4954
}
5055

5156
private fun initFormatter() {
52-
formatter = ToolFactory.createCodeFormatter(convertOptions(loadStyleOptions()))
57+
formatter = ToolFactory.createCodeFormatter(loadStyleOptions())
5358
}
5459

55-
private fun loadStyleOptions(): Properties {
60+
private fun loadStyleOptions(): Map<String, String> {
61+
val conventions = DefaultCodeFormatterConstants.getJavaConventionsSettings()
5662
val content = this.javaClass.getResource("/formatter.properties")!!.readText()
5763
val reader = StringReader(content)
5864
val properties = Properties()
5965
properties.load(reader)
60-
return properties
61-
}
6266

63-
private fun convertOptions(prop: Properties): HashMap<String, String> {
64-
return prop.entries
65-
.stream()
66-
.collect(
67-
Collectors.toMap(
68-
{ e -> e.toString() },
69-
{ v -> v.toString() },
70-
{ _, next -> next })
71-
{ HashMap() }
72-
)
67+
conventions.putAll(properties as Map<out String, String>)
68+
69+
return conventions
7370
}
7471
}
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1-
org.eclipse.jdt.core.compiler.source = 1.8
1+
#org.eclipse.jdt.core.compiler.source = 14
2+
#org.eclipse.jdt.core.compiler.compliance = 14
3+
4+
org.eclipse.jdt.core.formatter.lineSplit = 120
25
org.eclipse.jdt.core.formatter.tabulation.char = space
3-
org.eclipse.jdt.core..formatter.tabulation.size = 4
6+
org.eclipse.jdt.core.formatter.tabulation.size = 4
7+
org.eclipse.jdt.core.formatter.alignment_for_enum_constants = 49
8+
org.eclipse.jdt.core.formatter.comment.format_javadoc_comments = true
9+
org.eclipse.jdt.core.formatter.comment.insert_new_line_for_parameter = do not insert
10+
org.eclipse.jdt.core.formatter.comment.clear_blank_lines_in_javadoc_comment = true
11+
12+
org.eclipse.jdt.core.formatter.alignment_for_record_components = 52
13+
org.eclipse.jdt.core.formatter.keep_record_declaration_on_one_line = one_line_if_empty
14+
org.eclipse.jdt.core.formatter.parentheses_positions_in_record_declaration = separate_lines_if_wrapped

openapi-processor-core/src/testInt/kotlin/io/openapiprocessor/core/ProcessorTestSets.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ val ALL_30: List<TestParams> = listOf(
2121
test30_D_("endpoint-exclude"),
2222
test30_D_("endpoint-http-mapping"), // framework specific
2323
test30_DR("extension-mapping"),
24+
test30_DR("format-eclipse"),
2425
test30_DR("generated"),
2526
test30_DR("javadoc"),
2627
test30_DR("javadoc-with-mapping"),
@@ -97,6 +98,7 @@ val ALL_31: List<TestParams> = listOf(
9798
test31_D_("bean-validation-requestbody-mapping"),
9899
test31_DR("components-requestbodies"),
99100
test31_DR("extension-mapping"),
101+
test31_DR("format-eclipse"),
100102
test31_DR("deprecated"),
101103
test31_D_("endpoint-exclude"),
102104
test31_D_("endpoint-http-mapping"), // framework specific
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
items:
2+
- inputs/openapi30.yaml
3+
- inputs/openapi31.yaml
4+
- inputs/mapping.yaml
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
openapi-processor-mapping: v11
2+
3+
options:
4+
package-name: generated
5+
format-code: eclipse
6+
javadoc: true
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
openapi: 3.0.3
2+
info:
3+
title: test javadoc
4+
version: 1.0.0
5+
6+
paths:
7+
/foo:
8+
get:
9+
description: |
10+
a *markdown* description with **text**
11+
12+
- one list item
13+
- second list item
14+
15+
```
16+
code block
17+
```
18+
19+
more
20+
21+
parameters:
22+
- name: f-o-o
23+
description: this is a *parameter* description
24+
example: this is an example
25+
in: query
26+
schema:
27+
$ref: '#/components/schemas/Foo'
28+
- name: bar
29+
description: this is another *parameter* description
30+
example: this is another example
31+
in: query
32+
schema:
33+
type: string
34+
requestBody:
35+
description: this is the request body
36+
content:
37+
application/json:
38+
schema:
39+
type: array
40+
items:
41+
$ref: '#/components/schemas/Foo'
42+
required: true
43+
responses:
44+
'200':
45+
description: this is a *response* description
46+
content:
47+
application/json:
48+
schema:
49+
$ref: '#/components/schemas/Foo'
50+
51+
components:
52+
schemas:
53+
54+
Foo:
55+
description: >
56+
this is the *Foo* schema description
57+
type: object
58+
properties:
59+
foo-bar:
60+
description: >
61+
*property* description
62+
type: string
63+
enum:
64+
# actually OpenAPI 3.0 doesn't allow this, but it is easier to support it instead of being strict.
65+
description: >
66+
enum *property* description
67+
$ref: '#/components/schemas/FooEnum'
68+
69+
FooEnum:
70+
description: "this is an *enum* description"
71+
type: string
72+
enum: ['foo', 'bar']
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
openapi: 3.1.0
2+
info:
3+
title: test javadoc
4+
version: 1.0.0
5+
6+
paths:
7+
/foo:
8+
get:
9+
description: |
10+
a *markdown* description with **text**
11+
12+
- one list item
13+
- second list item
14+
15+
```
16+
code block
17+
```
18+
19+
more
20+
21+
parameters:
22+
- name: f-o-o
23+
description: this is a *parameter* description
24+
example: this is an example
25+
in: query
26+
schema:
27+
$ref: '#/components/schemas/Foo'
28+
- name: bar
29+
description: this is another *parameter* description
30+
example: this is another example
31+
in: query
32+
schema:
33+
type: string
34+
requestBody:
35+
description: this is the request body
36+
content:
37+
application/json:
38+
schema:
39+
type: array
40+
items:
41+
$ref: '#/components/schemas/Foo'
42+
required: true
43+
responses:
44+
'200':
45+
description: this is a *response* description
46+
content:
47+
application/json:
48+
schema:
49+
$ref: '#/components/schemas/Foo'
50+
51+
components:
52+
schemas:
53+
54+
Foo:
55+
description: >
56+
this is the *Foo* schema description
57+
type: object
58+
properties:
59+
foo-bar:
60+
description: >
61+
*property* description
62+
type: string
63+
enum:
64+
description: >
65+
enum *property* description
66+
$ref: '#/components/schemas/FooEnum'
67+
68+
FooEnum:
69+
description: "this is an *enum* description"
70+
type: string
71+
enum: ['foo', 'bar']
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
items:
2+
- outputs/api/Api.java
3+
- outputs/<model>/Foo.java
4+
- outputs/<model>/FooEnum.java
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package generated.api;
2+
3+
import annotation.Mapping;
4+
import annotation.Parameter;
5+
import generated.model.Foo;
6+
import generated.support.Generated;
7+
8+
@Generated(value = "openapi-processor-core", version = "test")
9+
public interface Api {
10+
11+
/**
12+
* a <em>markdown</em> description with <strong>text</strong>
13+
* <ul>
14+
* <li>one list item</li>
15+
* <li>second list item</li>
16+
* </ul>
17+
*
18+
* <pre>
19+
* <code>code block
20+
* </code>
21+
* </pre>
22+
*
23+
* more
24+
*
25+
* @param fOO this is a <em>parameter</em> description
26+
* @param bar this is another <em>parameter</em> description
27+
* @param body this is the request body
28+
* @return this is a <em>response</em> description
29+
*/
30+
@Mapping("/foo")
31+
Foo getFoo(@Parameter Foo fOO, @Parameter String bar, @Parameter Foo[] body);
32+
33+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package generated.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import generated.support.Generated;
5+
6+
/**
7+
* this is the <em>Foo</em> schema description
8+
*/
9+
@Generated(value = "openapi-processor-core", version = "test")
10+
public class Foo {
11+
12+
/**
13+
* <em>property</em> description
14+
*/
15+
@JsonProperty("foo-bar")
16+
private String fooBar;
17+
18+
/**
19+
* enum <em>property</em> description
20+
*/
21+
@JsonProperty("enum")
22+
private FooEnum aEnum;
23+
24+
public String getFooBar() {
25+
return fooBar;
26+
}
27+
28+
public void setFooBar(String fooBar) {
29+
this.fooBar = fooBar;
30+
}
31+
32+
public FooEnum getEnum() {
33+
return aEnum;
34+
}
35+
36+
public void setEnum(FooEnum aEnum) {
37+
this.aEnum = aEnum;
38+
}
39+
40+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package generated.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonValue;
5+
import generated.support.Generated;
6+
7+
/**
8+
* this is an <em>enum</em> description
9+
*/
10+
@Generated(value = "openapi-processor-core", version = "test")
11+
public enum FooEnum {
12+
FOO("foo"),
13+
BAR("bar");
14+
15+
private final String value;
16+
17+
FooEnum(String value) {
18+
this.value = value;
19+
}
20+
21+
@JsonValue
22+
public String getValue() {
23+
return this.value;
24+
}
25+
26+
@JsonCreator
27+
public static FooEnum fromValue(String value) {
28+
for (FooEnum val : FooEnum.values()) {
29+
if (val.value.equals(value)) {
30+
return val;
31+
}
32+
}
33+
throw new IllegalArgumentException(value);
34+
}
35+
}

0 commit comments

Comments
 (0)