Skip to content
This repository was archived by the owner on Jan 21, 2024. It is now read-only.

Commit a3e6ee2

Browse files
authored
Merge pull request #65 from raml-org/i59_add_oas30_support
Add OAS 3.0 support
2 parents 81e8fee + d01902b commit a3e6ee2

36 files changed

+1299
-26
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Build status](https://img.shields.io/travis/raml-org/webapi-parser.svg?style=flat)](https://travis-ci.org/raml-org/webapi-parser)
44
[![Coverage Status](https://coveralls.io/repos/github/raml-org/webapi-parser/badge.svg?branch=master)](https://coveralls.io/github/raml-org/webapi-parser?branch=master)
55

6-
API Spec parser based on [AMF](https://github.com/aml-org/amf). Currently supports RAML 0.8, 1.0 and OAS 2.0.
6+
API Spec parser based on [AMF](https://github.com/aml-org/amf). Currently supports RAML 0.8, RAML 1.0, OAS 2.0 and OAS 3.0(beta).
77

88
This project is a thin wrapper that exposes API Spec-related capabilities from [AMF](https://github.com/aml-org/amf). It is written in Scala and offered in two versions: [JavaScript](#javascript) and [Java](#java).
99

build.sbt

+4-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ val settings = Common.settings ++ Common.publish ++ Seq(
2727
credentials ++= Common.credentials(),
2828
libraryDependencies ++= Seq(
2929
"org.scalatest" %%% "scalatest" % "3.0.5" % "test",
30-
"com.github.amlorg" %%% "amf-webapi" % "4.0.2",
31-
"com.github.amlorg" %%% "amf-validation" % "4.0.2"
30+
"com.github.amlorg" %%% "amf-webapi" % "4.0.3",
31+
"com.github.amlorg" %%% "amf-validation" % "4.0.3"
3232
)
3333
)
3434

@@ -79,7 +79,8 @@ lazy val webapi = crossProject(JSPlatform, JVMPlatform)
7979
)
8080
.jsSettings(
8181
scalaJSModuleKind := ModuleKind.CommonJSModule,
82-
Compile / fullOptJS / artifactPath := baseDirectory.value / "target" / "artifact" / "webapi-parser-module.js"
82+
Compile / fullOptJS / artifactPath := baseDirectory.value / "target" / "artifact" / "webapi-parser-module.js",
83+
scalacOptions += "-P:scalajs:suppressExportDeprecations"
8384
)
8485

8586
lazy val webapiJVM = webapi.jvm.in(file("./jvm"))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"title": "API with Types",
5+
"version": ""
6+
},
7+
"paths": {
8+
"/users/{id}": {
9+
"get": {
10+
"operationId": "GET_users-id",
11+
"responses": {
12+
"200": {
13+
"description": "",
14+
"content": {
15+
"application/json": {
16+
"schema": {
17+
"$ref": "#/components/schemas/User"
18+
}
19+
}
20+
}
21+
}
22+
}
23+
},
24+
"parameters": [
25+
{
26+
"in": "path",
27+
"name": "id",
28+
"required": true,
29+
"schema": {
30+
"type": "something"
31+
}
32+
}
33+
]
34+
}
35+
},
36+
"components": {
37+
"schemas": {
38+
"User": {
39+
"type": "object",
40+
"required": [
41+
"firstName",
42+
"lastName",
43+
"age"
44+
],
45+
"properties": {
46+
"firstName": {
47+
"type": "string"
48+
},
49+
"lastName": {
50+
"type": "string"
51+
},
52+
"age": {
53+
"minimum": 0,
54+
"maximum": 99,
55+
"type": "integer"
56+
}
57+
}
58+
}
59+
}
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"title": "API with Types",
5+
"version": ""
6+
},
7+
"paths": {
8+
"/users/{id}": {
9+
"get": {
10+
"operationId": "GET_users-id",
11+
"responses": {
12+
"200": {
13+
"description": "",
14+
"content": {
15+
"application/json": {
16+
"schema": {
17+
"$ref": "#/components/schemas/User"
18+
}
19+
}
20+
}
21+
}
22+
}
23+
},
24+
"parameters": [
25+
{
26+
"in": "path",
27+
"name": "id",
28+
"required": true,
29+
"schema": {
30+
"type": "string"
31+
}
32+
}
33+
]
34+
}
35+
},
36+
"components": {
37+
"schemas": {
38+
"User": {
39+
"type": "object",
40+
"required": [
41+
"firstName",
42+
"lastName",
43+
"age"
44+
],
45+
"properties": {
46+
"firstName": {
47+
"type": "string"
48+
},
49+
"lastName": {
50+
"type": "string"
51+
},
52+
"age": {
53+
"minimum": 0,
54+
"maximum": 99,
55+
"type": "integer"
56+
}
57+
}
58+
}
59+
}
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
openapi: 3.0.0
2+
info:
3+
title: API with Types
4+
version: ""
5+
paths:
6+
"/users/{id}":
7+
get:
8+
operationId: GET_users-id
9+
responses:
10+
"200":
11+
description: ""
12+
content:
13+
application/json:
14+
schema:
15+
$ref: "#/components/schemas/User"
16+
parameters:
17+
- in: path
18+
name: id
19+
required: true
20+
schema:
21+
type: string
22+
components:
23+
schemas:
24+
User:
25+
type: object
26+
properties:
27+
firstName:
28+
type: string
29+
lastName:
30+
type: string
31+
age:
32+
minimum: 0
33+
maximum: 99
34+
type: integer
35+
required:
36+
- firstName
37+
- lastName
38+
- age

examples/java/src/main/java/co/acme/demo/WebApiParserDemo.java

+14
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
import co.acme.parse.Raml10Parsing;
44
import co.acme.parse.Raml08Parsing;
55
import co.acme.parse.Oas20Parsing;
6+
import co.acme.parse.Oas30Parsing;
67
import co.acme.parse.AmfGraphParsing;
78
import co.acme.generate.Raml10Generation;
89
import co.acme.generate.Raml08Generation;
910
import co.acme.generate.Oas20Generation;
11+
import co.acme.generate.Oas30Generation;
1012
import co.acme.generate.AmfGraphGeneration;
1113
import co.acme.validate.Raml10Validation;
1214
import co.acme.validate.Raml08Validation;
1315
import co.acme.validate.Oas20Validation;
16+
import co.acme.validate.Oas30Validation;
1417
import co.acme.validate.AmfGraphValidation;
1518
import co.acme.model.Raml10Building;
1619
import co.acme.model.Raml10Navigation;
@@ -36,6 +39,11 @@ public static void main(String[] args) throws ExecutionException, InterruptedExc
3639
Oas20Parsing.parseYamlString();
3740
Oas20Parsing.parseYamlFile();
3841

42+
Oas30Parsing.parseString();
43+
Oas30Parsing.parseFile();
44+
Oas30Parsing.parseYamlString();
45+
Oas30Parsing.parseYamlFile();
46+
3947
AmfGraphParsing.parseString();
4048
AmfGraphParsing.parseFile();
4149

@@ -50,12 +58,18 @@ public static void main(String[] args) throws ExecutionException, InterruptedExc
5058
Oas20Generation.generateYamlString();
5159
Oas20Generation.generateYamlFile();
5260

61+
Oas30Generation.generateString();
62+
Oas30Generation.generateFile();
63+
Oas30Generation.generateYamlString();
64+
Oas30Generation.generateYamlFile();
65+
5366
AmfGraphGeneration.generateString();
5467
AmfGraphGeneration.generateFile();
5568

5669
Raml10Validation.validate();
5770
Raml08Validation.validate();
5871
Oas20Validation.validate();
72+
Oas30Validation.validate();
5973
AmfGraphValidation.validate();
6074

6175
Raml10Building.buildApi();

examples/java/src/main/java/co/acme/generate/Oas20Generation.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class Oas20Generation {
1010
// Example of generating OAS 2.0 JSON file
1111
public static void generateFile() throws InterruptedException, ExecutionException {
1212
// Parse OAS 2.0 JSON file to get WebApi Model
13-
final WebApiBaseUnit result = Oas20.parse("file://../api-specs/oas/api-with-types.json").get();
13+
final WebApiBaseUnit result = Oas20.parse("file://../api-specs/oas20/api-with-types.json").get();
1414

1515
// Resolve parsed model (optional)
1616
final WebApiBaseUnit resolved = Oas20.resolve(result).get();
@@ -25,7 +25,7 @@ public static void generateFile() throws InterruptedException, ExecutionExceptio
2525
// Example of generating OAS 2.0 JSON string
2626
public static void generateString() throws InterruptedException, ExecutionException {
2727
// Parse OAS 2.0 JSON file to get WebApi Model
28-
final WebApiBaseUnit result = Oas20.parse("file://../api-specs/oas/api-with-types.json").get();
28+
final WebApiBaseUnit result = Oas20.parse("file://../api-specs/oas20/api-with-types.json").get();
2929

3030
// Generate OAS 2.0 JSON string
3131
final String output = Oas20.generateString(result).get();
@@ -35,7 +35,7 @@ public static void generateString() throws InterruptedException, ExecutionExcept
3535
// Example of generating OAS 2.0 YAML file
3636
public static void generateYamlFile() throws InterruptedException, ExecutionException {
3737
// Parse OAS 2.0 YAML file to get WebApi Model
38-
final WebApiBaseUnit result = Oas20.parseYaml("file://../api-specs/oas/api-with-types.yaml").get();
38+
final WebApiBaseUnit result = Oas20.parseYaml("file://../api-specs/oas20/api-with-types.yaml").get();
3939

4040
// Resolve parsed model (optional)
4141
final WebApiBaseUnit resolved = Oas20.resolve(result).get();
@@ -50,7 +50,7 @@ public static void generateYamlFile() throws InterruptedException, ExecutionExce
5050
// Example of generating OAS 2.0 YAML string
5151
public static void generateYamlString() throws InterruptedException, ExecutionException {
5252
// Parse OAS 2.0 YAML file to get WebApi Model
53-
final WebApiBaseUnit result = Oas20.parseYaml("file://../api-specs/oas/api-with-types.yaml").get();
53+
final WebApiBaseUnit result = Oas20.parseYaml("file://../api-specs/oas20/api-with-types.yaml").get();
5454

5555
// Generate OAS 2.0 YAML string
5656
final String output = Oas20.generateYamlString(result).get();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package co.acme.generate;
2+
3+
import webapi.Oas30;
4+
import webapi.WebApiBaseUnit;
5+
6+
import java.util.concurrent.ExecutionException;
7+
8+
public class Oas30Generation {
9+
10+
// Example of generating OAS 3.0 JSON file
11+
public static void generateFile() throws InterruptedException, ExecutionException {
12+
// Parse OAS 3.0 JSON file to get WebApi Model
13+
final WebApiBaseUnit result = Oas30.parse("file://../api-specs/oas30/api-with-types.json").get();
14+
15+
// Resolve parsed model (optional)
16+
final WebApiBaseUnit resolved = Oas30.resolve(result).get();
17+
18+
String fpath = "file://generated.json";
19+
20+
// Generate OAS 3.0 JSON file
21+
Oas30.generateFile(resolved, fpath).get();
22+
System.out.println("Generating Oas30 JSON file at: " + fpath);
23+
}
24+
25+
// Example of generating OAS 3.0 JSON string
26+
public static void generateString() throws InterruptedException, ExecutionException {
27+
// Parse OAS 3.0 JSON file to get WebApi Model
28+
final WebApiBaseUnit result = Oas30.parse("file://../api-specs/oas30/api-with-types.json").get();
29+
30+
// Generate OAS 3.0 JSON string
31+
final String output = Oas30.generateString(result).get();
32+
System.out.println("Generating Oas30 JSON string: " + output);
33+
}
34+
35+
// Example of generating OAS 3.0 YAML file
36+
public static void generateYamlFile() throws InterruptedException, ExecutionException {
37+
// Parse OAS 3.0 YAML file to get WebApi Model
38+
final WebApiBaseUnit result = Oas30.parseYaml("file://../api-specs/oas30/api-with-types.yaml").get();
39+
40+
// Resolve parsed model (optional)
41+
final WebApiBaseUnit resolved = Oas30.resolve(result).get();
42+
43+
String fpath = "file://generated.yaml";
44+
45+
// Generate OAS 3.0 YAML file
46+
Oas30.generateYamlFile(resolved, fpath).get();
47+
System.out.println("Generating Oas30 YAML file at: " + fpath);
48+
}
49+
50+
// Example of generating OAS 3.0 YAML string
51+
public static void generateYamlString() throws InterruptedException, ExecutionException {
52+
// Parse OAS 3.0 YAML file to get WebApi Model
53+
final WebApiBaseUnit result = Oas30.parseYaml("file://../api-specs/oas30/api-with-types.yaml").get();
54+
55+
// Generate OAS 3.0 YAML string
56+
final String output = Oas30.generateYamlString(result).get();
57+
System.out.println("Generating Oas30 YAML string: " + output);
58+
}
59+
}

examples/java/src/main/java/co/acme/model/Raml10Navigation.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ public static void navigateApi() throws InterruptedException, ExecutionException
2929
// Access security scheme from root
3030
System.out.println(
3131
"First security scheme name: " +
32-
api.security().get(0).scheme().name().value());
32+
api.security().get(0).schemes().get(0).scheme().name().value());
3333
System.out.println(
3434
"First security scheme description: " +
35-
api.security().get(0).scheme().description().value());
35+
api.security().get(0).schemes().get(0).scheme().description().value());
3636

3737
// Endpoint /users
3838
EndPoint users = (EndPoint) api.endPoints().get(0);

examples/java/src/main/java/co/acme/parse/Oas20Parsing.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class Oas20Parsing {
1515
// Example of parsing OAS 2.0 JSON file
1616
public static void parseFile() throws InterruptedException, ExecutionException {
1717
// Parse the file
18-
final WebApiBaseUnit result = Oas20.parse("file://../api-specs/oas/api-with-types.json").get();
18+
final WebApiBaseUnit result = Oas20.parse("file://../api-specs/oas20/api-with-types.json").get();
1919

2020
// Log parsed model API
2121
System.out.println("Parsed Oas20 JSON file. Expected unit encoding webapi: " + ((WebApiDocument) result).encodes());
@@ -53,7 +53,7 @@ public static void parseString() throws InterruptedException, ExecutionException
5353
// Example of parsing OAS 2.0 YAML file
5454
public static void parseYamlFile() throws InterruptedException, ExecutionException {
5555
// Parse the file
56-
final WebApiBaseUnit result = Oas20.parseYaml("file://../api-specs/oas/api-with-types.yaml").get();
56+
final WebApiBaseUnit result = Oas20.parseYaml("file://../api-specs/oas20/api-with-types.yaml").get();
5757

5858
// Log parsed model API
5959
System.out.println("Parsed Oas20 YAML file. Expected unit encoding webapi: " + ((WebApiDocument) result).encodes());

0 commit comments

Comments
 (0)