Skip to content

Commit 6c4981b

Browse files
committed
eclipse code formatter (#8)
1 parent 64f0b2c commit 6c4981b

File tree

12 files changed

+749
-4
lines changed

12 files changed

+749
-4
lines changed

openapi-processor-core/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ dependencies {
7474
implementation (libs.commons.text)
7575
implementation (libs.commonmark)
7676
implementation (libs.format.java.google)
77+
implementation (libs.format.java.eclipse)
7778
implementation (libs.slf4j)
7879

7980
testImplementation (project(":openapi-processor-core-parser-api"))

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/converter/ApiOptions.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ class ApiOptions: MappingSettings {
9292
*/
9393
var formatCode = false
9494

95+
/**
96+
* formatter: google or eclipse
97+
*/
98+
var formatCodeFormatter: String? = null
99+
95100
/**
96101
* enable/disable the @Generated annotation (optional).
97102
*/

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/converter/OptionsConverter.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ class OptionsConverter(private val checkObsoleteProcessorOptions: Boolean = fals
8585

8686
options.javadoc = mapping.options.javadoc
8787
options.oneOfInterface = mapping.options.oneOfInterface
88-
options.formatCode = mapping.options.formatCode
88+
89+
val (enableFormatCode, formatCodeFormatter) = checkFormatter(mapping.options)
90+
options.formatCode = enableFormatCode
91+
options.formatCodeFormatter = formatCodeFormatter
92+
8993
options.generatedAnnotation = mapping.options.generatedAnnotation
9094
options.generatedDate = mapping.options.generatedDate
9195
options.jsonPropertyAnnotation = JsonPropertyAnnotationMode.findBy(
@@ -128,6 +132,15 @@ class OptionsConverter(private val checkObsoleteProcessorOptions: Boolean = fals
128132
}
129133
}
130134

135+
private fun checkFormatter(options: Options): Pair<Boolean, String?> {
136+
return when (options.formatCode) {
137+
"true" -> Pair(true, "google")
138+
"google" -> Pair(true, "google")
139+
"eclipse" -> Pair(true, "eclipse")
140+
else -> Pair(false, null)
141+
}
142+
}
143+
131144
private fun checkServerUrl(options: Options): Pair<Boolean, Int?> {
132145
return when (options.basePath.serverUrl) {
133146
"false" -> Pair(false, null)

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/processor/mapping/v2/Options.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ data class Options(
7070
/**
7171
* enable/disable the code formatter (optional)
7272
*/
73-
val formatCode: Boolean = false,
73+
val formatCode: String = "false",
7474

7575
/**
7676
* enable/disable the @Generated annotation (optional)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package io.openapiprocessor.core.writer.java
2+
3+
import io.openapiprocessor.core.writer.SourceFormatter
4+
import org.eclipse.jdt.core.formatter.CodeFormatter
5+
import org.eclipse.jdt.core.ToolFactory
6+
import org.eclipse.jface.text.Document
7+
import java.io.StringReader
8+
import java.util.*
9+
import java.util.stream.Collectors
10+
11+
12+
class EclipseFormatter: SourceFormatter {
13+
private lateinit var formatter: CodeFormatter
14+
15+
init {
16+
initFormatter()
17+
}
18+
19+
override fun format(raw: String): String {
20+
try {
21+
val textEdit = formatter.format(
22+
CodeFormatter.K_COMPILATION_UNIT + CodeFormatter.F_INCLUDE_COMMENTS,
23+
raw,
24+
0,
25+
raw.length,
26+
0,
27+
"\n"
28+
)
29+
30+
val document = Document(raw)
31+
textEdit.apply(document)
32+
33+
return correctEndOfFile(document.get())
34+
} catch (e: Exception) {
35+
throw FormattingException("failed to format the generated source: \n>>\n$raw\n<<", e)
36+
}
37+
}
38+
39+
private fun correctEndOfFile(formatted: String): String {
40+
return StringBuilder()
41+
.append(formatted)
42+
.append("\n")
43+
.toString()
44+
}
45+
46+
private fun initFormatter() {
47+
formatter = ToolFactory.createCodeFormatter(convertOptions(loadStyleOptions()))
48+
}
49+
50+
private fun loadStyleOptions(): Properties {
51+
val content = this.javaClass.getResource("/formatter.properties")!!.readText()
52+
val reader = StringReader(content)
53+
val properties = Properties()
54+
properties.load(reader)
55+
return properties
56+
}
57+
58+
private fun convertOptions(prop: Properties): HashMap<String, String> {
59+
return prop.entries
60+
.stream()
61+
.collect(
62+
Collectors.toMap(
63+
{ e -> e.toString() },
64+
{ v -> v.toString() },
65+
{ _, next -> next })
66+
{ HashMap() }
67+
)
68+
}
69+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
org.eclipse.jdt.core.compiler.source = 1.8
2+
org.eclipse.jdt.core.formatter.tabulation.char = space
3+
org.eclipse.jdt.core..formatter.tabulation.size = 4
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
##
2+
## mapping example with keys & values
3+
##
4+
5+
# mapping format [required]
6+
openapi-processor-mapping: v10
7+
8+
options: # general processor options [required]
9+
10+
# target package [required]: io.openapiprocessor.generated (default)
11+
package-name: io.openapiprocessor.generated
12+
13+
# validation annotation (optional): true or false (default), javax, jakarta.
14+
bean-validation: jakarta
15+
16+
# generate javadoc from OpenAPI 'description' properties.
17+
javadoc: true
18+
19+
# enable/disable code formatter: false (default), true (= google), google or eclipse
20+
format-code: false
21+
22+
# suffix for model class names and enum names. Default is none.
23+
model-name-suffix: Resource
24+
25+
# default (i.e pojo) or record
26+
model-type: default
27+
28+
# default, string, supplier
29+
enum-type: default
30+
31+
# generate common interface for an `oneOf` object list (optional): true or false (default)
32+
one-of-interface: true
33+
34+
# enable/disable deletion of targetDir: true (default) or false.
35+
clear-target-dir: false
36+
37+
# enable/disable @Generated annotation
38+
generated-annotation: true
39+
40+
# enable/disable date on @Generated annotation
41+
generated-date: true
42+
43+
# control @JsonProperty annotation (always|auto|never)
44+
json-property-annotation: always
45+
46+
# target-dir related configuration
47+
target-dir:
48+
clear: true
49+
layout: standard
50+
51+
# server-url related configuration
52+
base-path:
53+
# false, true = 0, 1, ..
54+
prefix: 0
55+
# property resource with the uri base path
56+
properties-name: openapi.properties
57+
58+
logging:
59+
mapping: true
60+
mapping-target: stdout
61+
62+
compatibility:
63+
bean-validation-valid-on-reactive: false
64+
identifier-word-break-from-digit-to-letter: false
65+
66+
map: # the type mappings
67+
68+
# global mappings, apply to all paths/endpoints
69+
70+
# response wrapper (optional)
71+
result: org.springframework.http.ResponseEntity
72+
73+
# result style (optional, default "success")
74+
result-style: all
75+
76+
# reactive single wrapper (optional)
77+
single: reactor.core.publisher.Mono
78+
79+
# reactive array wrapper (optional)
80+
multi: reactor.core.publisher.Flux
81+
82+
types: # global type mappings (optional)
83+
84+
- type: array => java.util.Collection
85+
86+
- type: Schema => java.util.Map
87+
generics:
88+
- java.lang.String
89+
- java.lang.Double
90+
91+
- type: Schema @ io.openapiprocessor.Annotation()
92+
93+
parameters: # global parameter mappings (optional)
94+
95+
- name: foo => java.util.List
96+
- name: bar => com.github.hauner.openapi.Bar
97+
- name: param @ io.openapiprocessor.Annotation()
98+
- type: Schema @ io.openapiprocessor.Annotation()
99+
100+
responses: # global response mappings (optional)
101+
102+
- content: application/vnd.something => java.util.List
103+
- content: application/json => com.github.hauner.openapi.FooBar
104+
105+
106+
paths: # path/endpoint specific mappings (optional)
107+
108+
/first: # a path/endpoint from the openapi.yaml
109+
110+
# generate endpoint to a separate "excluded" interface
111+
exclude: true
112+
113+
/second: # another path/endpoint from the openapi.yaml
114+
115+
# path mappings, allow the same mappings as on the global level (except the "paths" property)
116+
# all mappings apply only to the parent path overriding any matching global mapping
117+
118+
# override top level "result" property, "plain" means no wrapper
119+
result: plain
120+
single: reactor.core.publisher.Mono
121+
multi: reactor.core.publisher.Flux
122+
123+
types:
124+
- type: Schema => java.util.Collection
125+
126+
parameters:
127+
- name: foo => java.util.List
128+
- add: bar => java.util.Set
129+
- type: Schema @ io.openapiprocessor.Annotation()
130+
131+
responses:
132+
- content: application/vnd.any => java.util.Set
133+
- content: application/json => java.util.Map
134+
135+
/third:
136+
result: plain
137+
138+
# path mappings can be limited to a specific http method
139+
140+
patch:
141+
# path method mappings, allow the same mappings as on the global level (except the "paths" property)
142+
# all mappings apply only to the parent path and method overriding any matching global mapping
143+
144+
null: org.openapitools.jackson.nullable.JsonNullable = JsonNullable.undefined()
145+
146+
extensions:
147+
x-something: foo @ some.Annotation
148+
x-something-else:
149+
- foo @ some.custom.FooAnnotation
150+
- bar @ some.custom.BarAnnotation

0 commit comments

Comments
 (0)