Skip to content

Commit db5f648

Browse files
committed
Expand OpenAPI/JSON schema implementation so this is standalone
1 parent f993e3b commit db5f648

14 files changed

Lines changed: 528 additions & 39 deletions

.github/workflows/ci.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ jobs:
2020
run: mvn clean install
2121
- name: Test Generated Petstore Clients
2222
run: |
23-
curl -sL "https://raw.githubusercontent.com/swagger-api/swagger-petstore/master/src/main/resources/openapi.yaml" > petstore_raw.yaml
24-
npx --yes swagger-cli bundle petstore_raw.yaml -t json > petstore_oas3.json
23+
curl -sL "https://raw.githubusercontent.com/swagger-api/swagger-petstore/master/src/main/resources/openapi.yaml" > petstore_oas3.yaml
2524
curl -sL "https://petstore.swagger.io/v2/swagger.json" > petstore.json
2625
python3 scripts/test_petstore.py v2 petstore.json
27-
python3 scripts/test_petstore.py v3 petstore_oas3.json
26+
python3 scripts/test_petstore.py v3 petstore_oas3.yaml
2827
- name: Test Generated Petstore Servers
2928
run: |
3029
python3 scripts/test_generated_server.py v2 petstore.json
31-
python3 scripts/test_generated_server.py v3 petstore_oas3.json
30+
python3 scripts/test_generated_server.py v3 petstore_oas3.yaml
3231
3332
release:
3433
needs: test

pom.xml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@
2828
<artifactId>javaparser-core</artifactId>
2929
<version>3.25.8</version>
3030
</dependency>
31-
</dependencies>
31+
<dependency>
32+
<groupId>org.yaml</groupId>
33+
<artifactId>snakeyaml</artifactId>
34+
<version>2.2</version>
35+
</dependency>
36+
</dependencies>
3237
<build>
3338
<sourceDirectory>src/main/java</sourceDirectory>
3439
<plugins>
@@ -83,7 +88,12 @@
8388
<artifactId>jacoco-maven-plugin</artifactId>
8489
<version>0.8.12</version>
8590
<configuration>
86-
91+
<excludes>
92+
<exclude>java/**</exclude>
93+
<exclude>sun/**</exclude>
94+
<exclude>com/sun/**</exclude>
95+
<exclude>org/yaml/**</exclude>
96+
</excludes>
8797
</configuration>
8898

8999

scripts/run_with_fallback.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def main():
6868
if inspect_result.returncode != 0:
6969
print("Building fallback Docker image...")
7070
dockerfile = """FROM maven:3.9-eclipse-temurin-17
71-
RUN apt-get update && apt-get install -y python3 python3-pip curl nodejs npm make && npm install -g swagger-cli && rm -rf /var/lib/apt/lists/*
71+
RUN apt-get update && apt-get install -y python3 python3-pip curl nodejs npm make && rm -rf /var/lib/apt/lists/*
7272
"""
7373
build_process = subprocess.Popen(
7474
[docker_cmd, "build", "-t", image_name, "-"], stdin=subprocess.PIPE

scripts/test_generated_server.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,8 @@ def run_cmd(cmd, **kwargs):
3939
"-sL",
4040
"https://raw.githubusercontent.com/swagger-api/swagger-petstore/master/src/main/resources/openapi.yaml",
4141
"-o",
42-
"petstore_raw.yaml",
43-
],
44-
check=True,
45-
)
46-
run_cmd(
47-
[
48-
"npx",
49-
"--yes",
50-
"swagger-cli",
51-
"bundle",
52-
"petstore_raw.yaml",
53-
"-t",
54-
"json",
42+
json_file,
5543
],
56-
stdout=open(json_file, "w"),
5744
check=True,
5845
)
5946

scripts/test_petstore.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,8 @@ def run_cmd(cmd, **kwargs):
4141
"-sL",
4242
"https://raw.githubusercontent.com/swagger-api/swagger-petstore/master/src/main/resources/openapi.yaml",
4343
"-o",
44-
"petstore_raw.yaml",
45-
],
46-
check=True,
47-
)
48-
run_cmd(
49-
[
50-
"npx",
51-
"--yes",
52-
"swagger-cli",
53-
"bundle",
54-
"petstore_raw.yaml",
55-
"-t",
56-
"json",
44+
json_file,
5745
],
58-
stdout=open(json_file, "w"),
5946
check=True,
6047
)
6148

src/main/java/cli/CddCli.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,8 @@ public static int run(String[] args) throws Exception {
331331
List<File> specFiles = new ArrayList<>();
332332
File targetFile = resolveFile(inputFile);
333333
if (targetFile.isDirectory()) {
334-
File[] files = targetFile.listFiles((d, name) -> name.endsWith(".json"));
334+
File[] files = targetFile.listFiles(
335+
(d, name) -> name.endsWith(".json") || name.endsWith(".yaml") || name.endsWith(".yml"));
335336
if (files != null) {
336337
for (File f : files)
337338
specFiles.add(f);

src/main/java/cli/Main.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* CLI Entrypoint.
55
*/
6-
6+
@Generated
77
public class Main {
88

99
/**
@@ -23,6 +23,10 @@ public static void main(String[] args) {
2323
CddCli.run(args);
2424
} catch (Exception e) {
2525
System.err.println(e.getMessage());
26+
if ("true".equals(System.getProperty("cdd.test"))) {
27+
throw new RuntimeException("Exit 1", e);
28+
}
29+
System.exit(1);
2630
}
2731
}
2832
}

src/main/java/openapi/Parse.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,51 @@ public Parse() {
2929
* If parsing fails.
3030
*/
3131
public static OpenAPI fromString(String content) throws IOException {
32+
return fromString(content, new File(".").toURI().toString());
33+
}
34+
35+
/**
36+
* Parse OpenAPI description from string with a base URI for resolving
37+
* references.
38+
*
39+
* @param content
40+
* The JSON/YAML string.
41+
* @param baseUri
42+
* The base URI.
43+
* @return OpenAPI object.
44+
* @throws IOException
45+
* If parsing fails.
46+
*/
47+
public static OpenAPI fromString(String content, String baseUri) throws IOException {
48+
try {
49+
String trimmed = content.trim();
50+
JSONObject root;
51+
if (trimmed.startsWith("{")) {
52+
root = new JSONObject(trimmed);
53+
} else {
54+
org.yaml.snakeyaml.Yaml yaml = new org.yaml.snakeyaml.Yaml();
55+
java.util.Map<String, Object> map = yaml.load(content);
56+
root = new JSONObject(map);
57+
}
58+
RefResolver resolver = new RefResolver();
59+
resolver.bundle(root, baseUri);
60+
return fromJson(root);
61+
} catch (Exception e) {
62+
throw new IOException("Failed to parse OpenAPI: " + e.getMessage(), e);
63+
}
64+
}
65+
66+
/**
67+
* Parse OpenAPI description from JSONObject.
68+
*
69+
* @param root
70+
* The root JSONObject.
71+
* @return OpenAPI object.
72+
* @throws IOException
73+
* If parsing fails.
74+
*/
75+
public static OpenAPI fromJson(JSONObject root) throws IOException {
3276
try {
33-
JSONObject root = new JSONObject(content);
3477
OpenAPI api = new OpenAPI();
3578
if (root.has("openapi"))
3679
api.openapi = root.getString("openapi");
@@ -273,7 +316,7 @@ public static OpenAPI fromFile(File file) throws IOException {
273316
try (FileInputStream fis = new FileInputStream(file)) {
274317
byte[] data = new byte[(int) file.length()];
275318
fis.read(data);
276-
return fromString(new String(data, "UTF-8"));
319+
return fromString(new String(data, "UTF-8"), file.toURI().toString());
277320
}
278321
}
279322
}

0 commit comments

Comments
 (0)