Skip to content

Commit 53e1753

Browse files
committed
fix: Add support for YAML
1 parent e7bf1ab commit 53e1753

9 files changed

Lines changed: 920 additions & 10 deletions

File tree

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = space
6+
indent_size = 2
7+
trim_trailing_whitespace = true
8+
end_of_line = lf
9+
insert_final_newline = true
10+
11+
[*.properties]
12+
trim_trailing_whitespace = false
13+
14+
[pom.xml]
15+
indent_style = unset

.pre-commit-config.yaml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
default_stages: [pre-commit]
22
repos:
33
- repo: https://github.com/pre-commit/pre-commit-hooks
4-
rev: v4.5.0
4+
rev: v6.0.0
55
hooks:
66
- id: end-of-file-fixer
77
- id: trailing-whitespace
88
- repo: https://github.com/Lucas-C/pre-commit-hooks
9-
rev: v1.5.4
9+
rev: v1.5.5
1010
hooks:
1111
- id: remove-crlf
1212
- id: remove-tabs
1313
args: [ --whitespaces-count=2 ]
1414
- repo: https://github.com/editorconfig-checker/editorconfig-checker.python
15-
rev: 2.7.3
15+
rev: 3.4.0
1616
hooks:
1717
- id: editorconfig-checker
1818
- repo: https://github.com/extenda/pre-commit-hooks
19-
rev: v0.11.0
19+
rev: v0.14.0
2020
hooks:
2121
- id: google-java-formatter
2222
- id: commitlint
2323
stages: [commit-msg]
24+
- repo: https://github.com/adrienverge/yamllint.git
25+
rev: v1.37.1
26+
hooks:
27+
- id: yamllint
28+
args: [ --strict, -c=.yamllint ]

.yamllint

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
extends: relaxed
2+
rules:
3+
colons:
4+
max-spaces-before: 0
5+
max-spaces-after: -1
6+
indentation:
7+
spaces: 2
8+
indent-sequences: true
9+
check-multi-line-strings: false
10+
line-length:
11+
max: 150
12+
allow-non-breakable-words: true
13+
level: error
14+
new-line-at-end-of-file: enable
15+
new-lines:
16+
type: unix
17+
trailing-spaces: {}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@
194194
<artifactId>maven-compiler-plugin</artifactId>
195195
<version>3.14.1</version>
196196
<configuration>
197-
<release>25</release>
197+
<release>21</release>
198198
</configuration>
199199
</plugin>
200200
</plugins>

src/main/java/com/retailsvc/http/openapi/SpecificationLoader.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.function.Function;
99
import org.slf4j.Logger;
1010
import org.slf4j.LoggerFactory;
11+
import org.yaml.snakeyaml.Yaml;
1112

1213
public class SpecificationLoader {
1314

@@ -29,10 +30,17 @@ public static byte[] load(String spec) {
2930
* @return The openapi model
3031
*/
3132
public static OpenApi parseSpecification(
32-
String specificationPath, Function<String, OpenApi> mapper) {
33+
String specificationPath, Function<String, OpenApi> mapper, Function<Object, String> toJson) {
3334
long t0 = System.currentTimeMillis();
3435
byte[] data = load(specificationPath);
3536
String openapiAsText = new String(data, StandardCharsets.UTF_8);
37+
38+
if (specificationPath.endsWith(".yaml") || specificationPath.endsWith(".yml")) {
39+
var yaml = new Yaml();
40+
Object yamlObj = yaml.load(openapiAsText);
41+
openapiAsText = toJson.apply(yamlObj);
42+
}
43+
3644
OpenApi spec = OpenApi.parse(mapper, openapiAsText);
3745

3846
LOG.debug(

src/test/java/com/retailsvc/http/ServerBaseTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public abstract class ServerBaseTest {
3131

3232
@BeforeEach
3333
void setUp() {
34-
specification = parseSpecification("openapi.json", s -> gson.fromJson(s, OpenApi.class));
34+
specification = parseSpecification("openapi.json", s -> gson.fromJson(s, OpenApi.class), null);
3535
}
3636

3737
@AfterEach

src/test/java/com/retailsvc/http/start/ServerLauncher.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static com.retailsvc.http.openapi.SpecificationLoader.parseSpecification;
44

55
import com.google.gson.Gson;
6+
import com.google.gson.GsonBuilder;
67
import com.retailsvc.http.ExceptionHandler;
78
import com.retailsvc.http.Handlers;
89
import com.retailsvc.http.OpenApiServer;
@@ -13,23 +14,27 @@
1314
import java.util.HashMap;
1415
import java.util.List;
1516
import java.util.Map;
17+
import java.util.function.Function;
1618
import org.slf4j.Logger;
1719
import org.slf4j.LoggerFactory;
1820

1921
public class ServerLauncher {
2022

2123
private static final Logger LOG = LoggerFactory.getLogger(ServerLauncher.class);
2224

23-
public static void main(String[] args) throws Exception {
25+
static void main() throws Exception {
2426
new ServerLauncher();
2527
}
2628

2729
public ServerLauncher() throws IOException {
2830
long t0 = System.currentTimeMillis();
2931

30-
final Gson gson = new Gson();
32+
final Gson gson = new GsonBuilder().setPrettyPrinting().create();
3133

32-
var specification = parseSpecification("openapi.json", s -> gson.fromJson(s, OpenApi.class));
34+
Function<String, OpenApi> jsonToSpec = contents -> gson.fromJson(contents, OpenApi.class);
35+
Function<Object, String> toJson = gson::toJson;
36+
37+
var specification = parseSpecification("openapi.yaml", jsonToSpec, toJson);
3338

3439
Map<String, HttpHandler> handlers = new HashMap<>();
3540
handlers.put("get-data", new GetDataHandler());

src/test/resources/openapi.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,16 @@
169169
}
170170
}
171171
}
172+
},
173+
"/anyOf": {
174+
"post": {
175+
176+
}
177+
},
178+
"/allOf": {
179+
"post": {
180+
181+
}
172182
}
173183
},
174184
"components": {

0 commit comments

Comments
 (0)