You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by sg...@apache.org on 2020/04/05 13:58:57 UTC
[freemarker-generator] branch master updated: FREEMARKER-139
freemarker-cli: Provide GsonTool to align with Maven plugin (#10)
This is an automated email from the ASF dual-hosted git repository.
sgoeschl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/freemarker-generator.git
The following commit(s) were added to refs/heads/master by this push:
new fa7207d FREEMARKER-139 freemarker-cli: Provide GsonTool to align with Maven plugin (#10)
fa7207d is described below
commit fa7207d4cbc9ef7d5958d30a809df1ec9fd325ad
Author: Siegfried Goeschl <si...@gmail.com>
AuthorDate: Sun Apr 5 15:58:51 2020 +0200
FREEMARKER-139 freemarker-cli: Provide GsonTool to align with Maven plugin (#10)
---
CHANGELOG.md | 4 +-
.../freemarker/generator/base/uri/NamedUri.java | 3 +-
freemarker-generator-cli/README.md | 108 +-
freemarker-generator-cli/run-samples.sh | 13 +-
.../site/sample/json/swagger-spec.json | 1228 ++++++++++++++++----
.../site/sample/yaml/swagger-spec.yaml | 700 +++++++++++
.../src/main/config/freemarker-cli.properties | 1 +
.../src/main/resources/freemarker-cli.properties | 1 +
.../src/main/scripts/run-samples.sh | 12 +-
.../freemarker/generator/cli/ExamplesTest.java | 8 +-
.../freemarker/generator/cli/ManualTest.java | 4 +-
.../templates/json/csv/swagger-endpoints.ftl | 26 +-
.../templates/json/yaml/transform.ftl | 17 +
.../templates/yaml/json/transform.ftl | 17 +
freemarker-generator-tools/pom.xml | 77 +-
.../freemarker/generator/tools/gson/GsonTool.java | 67 ++
.../generator/tools/snakeyaml/SnakeYamlTool.java | 29 +-
.../src/test/data/json/swagger.json | 1054 +++++++++++++++++
.../src/test/data/yaml/swagger.yaml | 700 +++++++++++
.../generator/tools/gson/GsonToolTest.java | 113 ++
.../generator/tools/jsonpath/JsonPathToolTest.java | 2 +-
.../tools/snakeyaml/SnakeYamlToolTest.java | 21 +
22 files changed, 3896 insertions(+), 309 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1867116..2846d0a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file. We try to a
## 0.1.0-SNAPSHOT
### Added
+* [FREEMARKER-139] freemarker-cli: Provide GsonTool to align with Maven plugin
* An environment variable can bes passed as `DataSource`
* [FREEMARKER-135] Support user-supplied names for `DataSource` on the command line
* [FREEMARKER-129] Support `DataSource` exclude pattern in addition to include pattern
@@ -34,4 +35,5 @@ All notable changes to this project will be documented in this file. We try to a
[FREEMARKER-134]: https://issues.apache.org/jira/browse/FREEMARKER-134
[FREEMARKER-135]: https://issues.apache.org/jira/browse/FREEMARKER-135
[FREEMARKER-136]: https://issues.apache.org/jira/browse/FREEMARKER-136
-[FREEMARKER-138]: https://issues.apache.org/jira/browse/FREEMARKER-1386
\ No newline at end of file
+[FREEMARKER-138]: https://issues.apache.org/jira/browse/FREEMARKER-138
+[FREEMARKER-139]: https://issues.apache.org/jira/browse/FREEMARKER-139
\ No newline at end of file
diff --git a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/uri/NamedUri.java b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/uri/NamedUri.java
index 39efcad..77cd1fa 100644
--- a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/uri/NamedUri.java
+++ b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/uri/NamedUri.java
@@ -103,8 +103,7 @@ public class NamedUri {
public File getFile() {
if (UriUtils.isFileUri(uri)) {
return new File(uri.getPath().substring(1));
- }
- else {
+ } else {
return new File(uri.getPath());
}
}
diff --git a/freemarker-generator-cli/README.md b/freemarker-generator-cli/README.md
index 0bd0a95..3fde98b 100644
--- a/freemarker-generator-cli/README.md
+++ b/freemarker-generator-cli/README.md
@@ -52,7 +52,7 @@ Some years later the not-so-small-any-longer-and-not-having-tests Groovy script
* Support multiple source files/directories for a single transformation
* Support transformation of Property files using plain-vanilla JDK
* Support transformation of CSV files using [Apache Commons CSV](https://commons.apache.org/proper/commons-csv/)
-* Support transformation of JSON using [Jayway's JSONPath](https://github.com/jayway/JsonPath)
+* Support transformation of JSON using [Jayway's JSONPath](https://github.com/jayway/JsonPath) and [GSON](https://github.com/google/gson)
* Support transformation of Excel using [Apache POI](https://poi.apache.org)
* Support transformation of YAML using [SnakeYAML](https://bitbucket.org/asomov/snakeyaml/wiki/Home)
* Support transformation of HTML using [JSoup](https://jsoup.org)
@@ -76,28 +76,29 @@ You can test the installation by executing
```text
> ./bin/freemarker-cli -t templates/info.ftl
-
FreeMarker CLI Information
----------------------------------------------------------------------------
+------------------------------------------------------------------------------
FreeMarker version : 2.3.29
Template name : templates/info.ftl
Language : en
Locale : en_US
-Timestamp : Feb 22, 2020 4:42:01 PM
+Timestamp : Apr 4, 2020 12:39:28 PM
Output encoding : UTF-8
Output format : plainText
FreeMarker CLI Template Directories
----------------------------------------------------------------------------
-[1] /Users/sgoeschl/work/github/apache/freemarker-generator/freemarker-generator-cli/target/appassembler
+------------------------------------------------------------------------------
+[#1] /Users/sgoeschl/work/github/apache/freemarker-generator/freemarker-generator-cli/target/appassembler
+[#2] /Users/sgoeschl/.freemarker-cli
FreeMarker CLI Tools
----------------------------------------------------------------------------
+------------------------------------------------------------------------------
- CSVTool : Process CSV files using Apache Commons CSV (see https://commons.apache.org/proper/commons-csv/)
- ExcelTool : Process Excels files (XLS, XLSX) using Apache POI (see https://poi.apache.org)
- ExecTool : Execute command line tools using Apache Commons Exec (see https://commons.apache.org/proper/commons-exec/)
- FreeMarkerTool : Expose useful Apache FreeMarker classes
- GrokTool : Process text files using Grok expressions (see https://github.com/thekrakken/java-grok)
+- GsonTool : Process JSON files using GSON (see https://github.com/google/gson)
- JsonPathTool : Process JSON files using Java JSON Path (see https://github.com/json-path/JsonPath)
- JsoupTool : Process HTML files using Jsoup (see https://jsoup.org)
- PropertiesTool : Process JDK properties files
@@ -107,21 +108,21 @@ FreeMarker CLI Tools
- YamlTool : Process YAML files using SnakeYAML(see https://bitbucket.org/asomov/snakeyaml/wiki/Home)
FreeMarker CLI DataSources
----------------------------------------------------------------------------
+------------------------------------------------------------------------------
User Supplied Parameters
----------------------------------------------------------------------------
+------------------------------------------------------------------------------
User Supplied System Properties
----------------------------------------------------------------------------
+------------------------------------------------------------------------------
SystemTool
----------------------------------------------------------------------------
+------------------------------------------------------------------------------
Command line : -t, templates/info.ftl
Host Name : W0GL5179.local
Java Home : /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home
User Name : sgoeschl
-Timestamp : 1,582,386,121,793
+Timestamp : 1,585,996,768,896
Writer : org.apache.freemarker.generator.base.util.NonClosableWriterWrapper
```
@@ -148,40 +149,44 @@ templates/excel/csv/transform.ftl
templates/excel/csv/custom.ftl
templates/html/csv/dependencies.ftl
templates/json/csv/swagger-endpoints.ftl
+templates/json/yaml/transform.ftl
templates/json/md/github-users.ftl
templates/properties/csv/locker-test-users.ftl
templates/yaml/txt/transform.ftl
+templates/yaml/json/transform.ftl
templates/xml/txt/recipients.ftl
Created the following sample files in ./target/out
-total 1344
--rw-r--r-- 1 sgoeschl staff 646 Feb 22 16:43 combined-access.log.txt
--rw-r--r-- 1 sgoeschl staff 22548 Feb 22 16:43 contract.html
--rw-r--r-- 1 sgoeschl staff 7933 Feb 22 16:43 contract.md
--rw-r--r-- 1 sgoeschl staff 784 Feb 22 16:43 curl.sh
--rw-r--r-- 1 sgoeschl staff 232 Feb 22 16:43 customer.txt
--rw-r--r-- 1 sgoeschl staff 15084 Feb 22 16:43 demo.txt
--rw-r--r-- 1 sgoeschl staff 1310 Feb 22 16:43 dependencies.csv
--rw-r--r-- 1 sgoeschl staff 2029 Feb 22 16:43 github-users-curl.md
--rw-r--r-- 1 sgoeschl staff 2668 Feb 22 16:43 info.txt
--rw-r--r-- 1 sgoeschl staff 66 Feb 22 16:43 interactive-html.txt
--rw-r--r-- 1 sgoeschl staff 16 Feb 22 16:43 interactive-json.txt
--rw-r--r-- 1 sgoeschl staff 10 Feb 22 16:43 interactive-xml.txt
--rw-r--r-- 1 sgoeschl staff 285 Feb 22 16:43 locker-test-users.csv
--rw-r--r-- 1 sgoeschl staff 6341 Feb 22 16:43 locker-test-users.fo
--rw-r--r-- 1 sgoeschl staff 5526 Feb 22 16:43 locker-test-users.pdf
--rw-r--r-- 1 sgoeschl staff 921 Feb 22 16:43 recipients.txt
--rw-r--r-- 1 sgoeschl staff 910 Feb 22 16:43 sales-records.md
--rw-r--r-- 1 sgoeschl staff 379 Feb 22 16:43 swagger-spec.csv
--rw-r--r-- 1 sgoeschl staff 156 Feb 22 16:43 test-multiple-sheets.xlsx.csv
--rw-r--r-- 1 sgoeschl staff 1917 Feb 22 16:43 test-multiple-sheets.xlsx.html
--rw-r--r-- 1 sgoeschl staff 389 Feb 22 16:43 test-multiple-sheets.xlsx.md
--rw-r--r-- 1 sgoeschl staff 150 Feb 22 16:43 test-transform-xls.csv
--rw-r--r-- 1 sgoeschl staff 1556 Feb 22 16:43 test.xls.html
--rw-r--r-- 1 sgoeschl staff 1558 Feb 22 16:43 test.xslx.html
--rw-r--r-- 1 sgoeschl staff 25756 Feb 22 16:43 transactions-fo.pdf
--rw-r--r-- 1 sgoeschl staff 66016 Feb 22 16:43 transactions-html.pdf
--rw-r--r-- 1 sgoeschl staff 330128 Feb 22 16:43 transactions.fo
--rw-r--r-- 1 sgoeschl staff 51008 Feb 22 16:43 transactions.html
+total 1440
+-rw-r--r-- 1 sgoeschl staff 646 Apr 4 13:03 combined-access.log.txt
+-rw-r--r-- 1 sgoeschl staff 22548 Apr 4 13:03 contract.html
+-rw-r--r-- 1 sgoeschl staff 7933 Apr 4 13:03 contract.md
+-rw-r--r-- 1 sgoeschl staff 784 Apr 4 13:03 curl.sh
+-rw-r--r-- 1 sgoeschl staff 232 Apr 4 13:03 customer.txt
+-rw-r--r-- 1 sgoeschl staff 15268 Apr 4 13:03 demo.txt
+-rw-r--r-- 1 sgoeschl staff 1310 Apr 4 13:03 dependencies.csv
+-rw-r--r-- 1 sgoeschl staff 2029 Apr 4 13:03 github-users-curl.md
+-rw-r--r-- 1 sgoeschl staff 2901 Apr 4 13:03 info.txt
+-rw-r--r-- 1 sgoeschl staff 66 Apr 4 13:03 interactive-html.txt
+-rw-r--r-- 1 sgoeschl staff 16 Apr 4 13:03 interactive-json.txt
+-rw-r--r-- 1 sgoeschl staff 10 Apr 4 13:03 interactive-xml.txt
+-rw-r--r-- 1 sgoeschl staff 285 Apr 4 13:03 locker-test-users.csv
+-rw-r--r-- 1 sgoeschl staff 6341 Apr 4 13:03 locker-test-users.fo
+-rw-r--r-- 1 sgoeschl staff 5526 Apr 4 13:03 locker-test-users.pdf
+-rw-r--r-- 1 sgoeschl staff 921 Apr 4 13:03 recipients.txt
+-rw-r--r-- 1 sgoeschl staff 910 Apr 4 13:03 sales-records.md
+-rw-r--r-- 1 sgoeschl staff 2453 Apr 4 13:03 swagger-spec.csv
+-rw-r--r-- 1 sgoeschl staff 25090 Apr 4 13:03 swagger-spec.json
+-rw-r--r-- 1 sgoeschl staff 16870 Apr 4 13:03 swagger-spec.yaml
+-rw-r--r-- 1 sgoeschl staff 156 Apr 4 13:03 test-multiple-sheets.xlsx.csv
+-rw-r--r-- 1 sgoeschl staff 1917 Apr 4 13:03 test-multiple-sheets.xlsx.html
+-rw-r--r-- 1 sgoeschl staff 389 Apr 4 13:03 test-multiple-sheets.xlsx.md
+-rw-r--r-- 1 sgoeschl staff 150 Apr 4 13:03 test-transform-xls.csv
+-rw-r--r-- 1 sgoeschl staff 1556 Apr 4 13:03 test.xls.html
+-rw-r--r-- 1 sgoeschl staff 1558 Apr 4 13:03 test.xslx.html
+-rw-r--r-- 1 sgoeschl staff 25756 Apr 4 13:03 transactions-fo.pdf
+-rw-r--r-- 1 sgoeschl staff 66016 Apr 4 13:03 transactions-html.pdf
+-rw-r--r-- 1 sgoeschl staff 330127 Apr 4 13:03 transactions.fo
+-rw-r--r-- 1 sgoeschl staff 51008 Apr 4 13:03 transactions.html
```
Please note that generated PDF files are very likely not found since they require `wkhtmltopdf` and `Apache FOP` installation.
@@ -1101,7 +1106,21 @@ yields
| Total Profit | 498855.44 |
```
-## 6.15 Using Advanced FreeMarker Features
+## 6.15 Converting Between JSON And YAML
+
+Sometimes we simply need to transform a JSON into an equivalent YAML or the other way around
+
+```
+> ./bin/freemarker-cli -t templates/yaml/json/transform.ftl site/sample/yaml/swagger-spec.yaml
+> ./bin/freemarker-cli -i '${GsonTool.toJson(YamlTool.parse(DataSources.get(0)))}' site/sample/yaml/swagger-spec.yaml
+
+> ./bin/freemarker-cli -t templates/json/yaml/transform.ftl site/sample/json/swagger-spec.json
+> ./bin/freemarker-cli -i '${YamlTool.toYaml(GsonTool.parse(DataSources.get(0)))}' site/sample/json/swagger-spec.json
+```
+
+
+
+## 6.16 Using Advanced FreeMarker Features
There is a `demo.ftl` which shows some advanced FreeMarker functionality
@@ -1206,6 +1225,7 @@ Get all data sources
- ExecTool : Execute command line tools using Apache Commons Exec (see https://commons.apache.org/proper/commons-exec/)
- FreeMarkerTool : Expose useful Apache FreeMarker classes
- GrokTool : Process text files using Grok expressions (see https://github.com/thekrakken/java-grok)
+- GsonTool : Process JSON files using GSON (see https://github.com/google/gson)
- JsonPathTool : Process JSON files using Java JSON Path (see https://github.com/json-path/JsonPath)
- JsoupTool : Process HTML files using Jsoup (see https://jsoup.org)
- PropertiesTool : Process JDK properties files
@@ -1222,6 +1242,7 @@ Get all data sources
- ExecTool
- FreeMarkerTool
- GrokTool
+- GsonTool
- JsonPathTool
- JsoupTool
- PropertiesTool
@@ -1269,9 +1290,10 @@ Within the script a FreeMarker data model is set up and passed to the template -
| CSVTool | Process CSV files using [Apache Commons CSV](https://commons.apache.org/proper/commons-csv/) |
| ExecTool | Execute command line tools using [Apache Commons Exec](https://commons.apache.org/proper/commons-exec/) |
| ExcelTool | Process Excels files (XLS, XLSX) using [Apache POI](https://poi.apache.org) |
-| DataSources | Helper class to find data sources, e.g. by name, extension or index |
+| DataSources | Helper class to find data sources, e.g. by name, extension or index |
| FreeMarkerTool | Expose useful FreeMarker classes |
| GrokTool | Process text files using [Grok](https://github.com/thekrakken/java-grok) instead of regular expressions |
+| GsonTool | Process JSON files using [GSON](https://github.com/google/gson) |
| JsonPathTool | Process JSON file using [Java JSON Path](https://github.com/json-path/JsonPath) |
| JsoupTool | Processing HTML files using [Jsoup](https://jsoup.org) |
| PropertiesTool | Process JDK properties files |
diff --git a/freemarker-generator-cli/run-samples.sh b/freemarker-generator-cli/run-samples.sh
index baaafab..01e5085 100755
--- a/freemarker-generator-cli/run-samples.sh
+++ b/freemarker-generator-cli/run-samples.sh
@@ -49,6 +49,8 @@ $FREEMARKER_CMD -t templates/demo.ftl README.md > target/out/demo.txt || { echo
$FREEMARKER_CMD -i '${JsonPathTool.parse(DataSources.first).read("$.info.title")}' site/sample/json/swagger-spec.json > target/out/interactive-json.txt || { echo >&2 "Test failed. Aborting."; exit 1; }
$FREEMARKER_CMD -i '${XmlTool.parse(DataSources.first)["recipients/person[1]/name"]}' site/sample/xml/recipients.xml > target/out/interactive-xml.txt || { echo >&2 "Test failed. Aborting."; exit 1; }
$FREEMARKER_CMD -i '${JsoupTool.parse(DataSources.first).select("a")[0]}' site/sample/html/dependencies.html > target/out/interactive-html.txt || { echo >&2 "Test failed. Aborting."; exit 1; }
+$FREEMARKER_CMD -i '${GsonTool.toJson(YamlTool.parse(DataSources.get(0)))}' site/sample/yaml/swagger-spec.yaml > target/out/interactive-swagger.json || { echo >&2 "Test failed. Aborting."; exit 1; }
+$FREEMARKER_CMD -i '${YamlTool.toYaml(GsonTool.parse(DataSources.get(0)))}' site/sample/json/swagger-spec.json > target/out/interactive-swagger.yaml || { echo >&2 "Test failed. Aborting."; exit 1; }
#############################################################################
# CSV
@@ -137,6 +139,9 @@ $FREEMARKER_CMD -t templates/html/csv/dependencies.ftl site/sample/html/dependen
echo "templates/json/csv/swagger-endpoints.ftl"
$FREEMARKER_CMD -t templates/json/csv/swagger-endpoints.ftl site/sample/json/swagger-spec.json > target/out/swagger-spec.csv || { echo >&2 "Test failed. Aborting."; exit 1; }
+echo "templates/json/yaml/transform.ftl"
+$FREEMARKER_CMD -t templates/json/yaml/transform.ftl site/sample/json/swagger-spec.json > target/out/swagger-spec.yaml || { echo >&2 "Test failed. Aborting."; exit 1; }
+
if hash curl 2>/dev/null; then
echo "templates/json/md/github-users.ftl"
$FREEMARKER_CMD -t templates/json/md/github-users.ftl site/sample/json/github-users.json > target/out/github-users-curl.md || { echo >&2 "Test failed. Aborting."; exit 1; }
@@ -154,7 +159,10 @@ $FREEMARKER_CMD -t templates/properties/csv/locker-test-users.ftl site/sample/pr
#############################################################################
echo "templates/yaml/txt/transform.ftl"
-$FREEMARKER_CMD -t ./templates/yaml/txt/transform.ftl ./site/sample/yaml/customer.yaml > target/out/customer.txt || { echo >&2 "Test failed. Aborting."; exit 1; }
+$FREEMARKER_CMD -t templates/yaml/txt/transform.ftl site/sample/yaml/customer.yaml > target/out/customer.txt || { echo >&2 "Test failed. Aborting."; exit 1; }
+
+echo "templates/yaml/json/transform.ftl"
+$FREEMARKER_CMD -t templates/yaml/json/transform.ftl site/sample/yaml/swagger-spec.yaml > target/out/swagger-spec.json || { echo >&2 "Test failed. Aborting."; exit 1; }
#############################################################################
# XML
@@ -164,5 +172,4 @@ echo "templates/xml/txt/recipients.ftl"
$FREEMARKER_CMD -t ./templates/xml/txt/recipients.ftl site/sample/xml/recipients.xml > target/out/recipients.txt || { echo >&2 "Test failed. Aborting."; exit 1; }
echo "Created the following sample files in ./target/out"
-ls -l ./target/out
-
+ls -l ./target/out
\ No newline at end of file
diff --git a/freemarker-generator-cli/site/sample/json/swagger-spec.json b/freemarker-generator-cli/site/sample/json/swagger-spec.json
index 35cf52c..1c5db9a 100644
--- a/freemarker-generator-cli/site/sample/json/swagger-spec.json
+++ b/freemarker-generator-cli/site/sample/json/swagger-spec.json
@@ -1,210 +1,1036 @@
{
- "swagger": "2.0",
- "info": {
- "version": "1.0.0",
- "title": "Swagger Petstore",
- "description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification",
- "termsOfService": "http://swagger.io/terms/",
- "contact": {
- "name": "Swagger API Team",
- "email": "apiteam@swagger.io",
- "url": "http://swagger.io"
- },
- "license": {
- "name": "Apache 2.0",
- "url": "https://www.apache.org/licenses/LICENSE-2.0.html"
- }
- },
- "host": "petstore.swagger.io",
- "basePath": "/api",
- "schemes": [
- "http"
- ],
- "consumes": [
- "application/json"
- ],
- "produces": [
- "application/json"
- ],
- "paths": {
- "/pets": {
- "get": {
- "description": "Returns all pets from the system that the user has access to",
- "operationId": "findPets",
- "parameters": [
- {
- "name": "tags",
- "in": "query",
- "description": "tags to filter by",
- "required": false,
- "type": "array",
- "collectionFormat": "csv",
- "items": {
- "type": "string"
- }
- },
- {
- "name": "limit",
- "in": "query",
- "description": "maximum number of results to return",
- "required": false,
- "type": "integer",
- "format": "int32"
- }
- ],
- "responses": {
- "200": {
- "description": "pet response",
- "schema": {
- "type": "array",
- "items": {
- "$ref": "#/definitions/Pet"
- }
- }
- },
- "default": {
- "description": "unexpected error",
- "schema": {
- "$ref": "#/definitions/Error"
- }
- }
- }
+ "swagger": "2.0",
+ "info": {
+ "description": "This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters.",
+ "version": "1.0.0",
+ "title": "Swagger Petstore",
+ "termsOfService": "http://swagger.io/terms/",
+ "contact": {
+ "email": "apiteam@swagger.io"
+ },
+ "license": {
+ "name": "Apache 2.0",
+ "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
+ }
+ },
+ "host": "petstore.swagger.io",
+ "basePath": "/v2",
+ "tags": [
+ {
+ "name": "pet",
+ "description": "Everything about your Pets",
+ "externalDocs": {
+ "description": "Find out more",
+ "url": "http://swagger.io"
+ }
+ },
+ {
+ "name": "store",
+ "description": "Access to Petstore orders"
+ },
+ {
+ "name": "user",
+ "description": "Operations about user",
+ "externalDocs": {
+ "description": "Find out more about our store",
+ "url": "http://swagger.io"
+ }
+ }
+ ],
+ "schemes": [
+ "https",
+ "http"
+ ],
+ "paths": {
+ "/pet": {
+ "post": {
+ "tags": [
+ "pet"
+ ],
+ "summary": "Add a new pet to the store",
+ "description": "",
+ "operationId": "addPet",
+ "consumes": [
+ "application/json",
+ "application/xml"
+ ],
+ "produces": [
+ "application/xml",
+ "application/json"
+ ],
+ "parameters": [
+ {
+ "in": "body",
+ "name": "body",
+ "description": "Pet object that needs to be added to the store",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/Pet"
+ }
+ }
+ ],
+ "responses": {
+ "405": {
+ "description": "Invalid input"
+ }
+ },
+ "security": [
+ {
+ "petstore_auth": [
+ "write:pets",
+ "read:pets"
+ ]
+ }
+ ]
+ },
+ "put": {
+ "tags": [
+ "pet"
+ ],
+ "summary": "Update an existing pet",
+ "description": "",
+ "operationId": "updatePet",
+ "consumes": [
+ "application/json",
+ "application/xml"
+ ],
+ "produces": [
+ "application/xml",
+ "application/json"
+ ],
+ "parameters": [
+ {
+ "in": "body",
+ "name": "body",
+ "description": "Pet object that needs to be added to the store",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/Pet"
+ }
+ }
+ ],
+ "responses": {
+ "400": {
+ "description": "Invalid ID supplied"
+ },
+ "404": {
+ "description": "Pet not found"
+ },
+ "405": {
+ "description": "Validation exception"
+ }
+ },
+ "security": [
+ {
+ "petstore_auth": [
+ "write:pets",
+ "read:pets"
+ ]
+ }
+ ]
+ }
+ },
+ "/pet/findByStatus": {
+ "get": {
+ "tags": [
+ "pet"
+ ],
+ "summary": "Finds Pets by status",
+ "description": "Multiple status values can be provided with comma separated strings",
+ "operationId": "findPetsByStatus",
+ "produces": [
+ "application/xml",
+ "application/json"
+ ],
+ "parameters": [
+ {
+ "name": "status",
+ "in": "query",
+ "description": "Status values that need to be considered for filter",
+ "required": true,
+ "type": "array",
+ "items": {
+ "type": "string",
+ "enum": [
+ "available",
+ "pending",
+ "sold"
+ ],
+ "default": "available"
},
- "post": {
- "description": "Creates a new pet in the store. Duplicates are allowed",
- "operationId": "addPet",
- "parameters": [
- {
- "name": "pet",
- "in": "body",
- "description": "Pet to add to the store",
- "required": true,
- "schema": {
- "$ref": "#/definitions/NewPet"
- }
- }
- ],
- "responses": {
- "200": {
- "description": "pet response",
- "schema": {
- "$ref": "#/definitions/Pet"
- }
- },
- "default": {
- "description": "unexpected error",
- "schema": {
- "$ref": "#/definitions/Error"
- }
- }
- }
- }
- },
- "/pets/{id}": {
- "get": {
- "description": "Returns a user based on a single ID, if the user does not have access to the pet",
- "operationId": "find pet by id",
- "parameters": [
- {
- "name": "id",
- "in": "path",
- "description": "ID of pet to fetch",
- "required": true,
- "type": "integer",
- "format": "int64"
- }
- ],
- "responses": {
- "200": {
- "description": "pet response",
- "schema": {
- "$ref": "#/definitions/Pet"
- }
- },
- "default": {
- "description": "unexpected error",
- "schema": {
- "$ref": "#/definitions/Error"
- }
- }
- }
+ "collectionFormat": "multi"
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "successful operation",
+ "schema": {
+ "type": "array",
+ "items": {
+ "$ref": "#/definitions/Pet"
+ }
+ }
+ },
+ "400": {
+ "description": "Invalid status value"
+ }
+ },
+ "security": [
+ {
+ "petstore_auth": [
+ "write:pets",
+ "read:pets"
+ ]
+ }
+ ]
+ }
+ },
+ "/pet/findByTags": {
+ "get": {
+ "tags": [
+ "pet"
+ ],
+ "summary": "Finds Pets by tags",
+ "description": "Muliple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.",
+ "operationId": "findPetsByTags",
+ "produces": [
+ "application/xml",
+ "application/json"
+ ],
+ "parameters": [
+ {
+ "name": "tags",
+ "in": "query",
+ "description": "Tags to filter by",
+ "required": true,
+ "type": "array",
+ "items": {
+ "type": "string"
},
- "delete": {
- "description": "Deletes a single pet based on the ID supplied",
- "operationId": "deletePet",
- "parameters": [
- {
- "name": "id",
- "in": "path",
- "description": "ID of pet to delete",
- "required": true,
- "type": "integer",
- "format": "int64"
- }
- ],
- "responses": {
- "204": {
- "description": "pet deleted"
- },
- "default": {
- "description": "unexpected error",
- "schema": {
- "$ref": "#/definitions/Error"
- }
- }
- }
- }
- }
- },
- "definitions": {
- "Pet": {
- "type": "object",
- "allOf": [
- {
- "$ref": "#/definitions/NewPet"
- },
- {
- "required": [
- "id"
- ],
- "properties": {
- "id": {
- "type": "integer",
- "format": "int64"
- }
- }
- }
+ "collectionFormat": "multi"
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "successful operation",
+ "schema": {
+ "type": "array",
+ "items": {
+ "$ref": "#/definitions/Pet"
+ }
+ }
+ },
+ "400": {
+ "description": "Invalid tag value"
+ }
+ },
+ "security": [
+ {
+ "petstore_auth": [
+ "write:pets",
+ "read:pets"
]
+ }
+ ],
+ "deprecated": true
+ }
+ },
+ "/pet/{petId}": {
+ "get": {
+ "tags": [
+ "pet"
+ ],
+ "summary": "Find pet by ID",
+ "description": "Returns a single pet",
+ "operationId": "getPetById",
+ "produces": [
+ "application/xml",
+ "application/json"
+ ],
+ "parameters": [
+ {
+ "name": "petId",
+ "in": "path",
+ "description": "ID of pet to return",
+ "required": true,
+ "type": "integer",
+ "format": "int64"
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "successful operation",
+ "schema": {
+ "$ref": "#/definitions/Pet"
+ }
+ },
+ "400": {
+ "description": "Invalid ID supplied"
+ },
+ "404": {
+ "description": "Pet not found"
+ }
+ },
+ "security": [
+ {
+ "api_key": []
+ }
+ ]
+ },
+ "post": {
+ "tags": [
+ "pet"
+ ],
+ "summary": "Updates a pet in the store with form data",
+ "description": "",
+ "operationId": "updatePetWithForm",
+ "consumes": [
+ "application/x-www-form-urlencoded"
+ ],
+ "produces": [
+ "application/xml",
+ "application/json"
+ ],
+ "parameters": [
+ {
+ "name": "petId",
+ "in": "path",
+ "description": "ID of pet that needs to be updated",
+ "required": true,
+ "type": "integer",
+ "format": "int64"
+ },
+ {
+ "name": "name",
+ "in": "formData",
+ "description": "Updated name of the pet",
+ "required": false,
+ "type": "string"
+ },
+ {
+ "name": "status",
+ "in": "formData",
+ "description": "Updated status of the pet",
+ "required": false,
+ "type": "string"
+ }
+ ],
+ "responses": {
+ "405": {
+ "description": "Invalid input"
+ }
},
- "NewPet": {
- "type": "object",
- "required": [
- "name"
- ],
- "properties": {
- "name": {
- "type": "string"
- },
- "tag": {
- "type": "string"
- }
- }
- },
- "Error": {
- "type": "object",
- "required": [
- "code",
- "message"
- ],
- "properties": {
- "code": {
- "type": "integer",
- "format": "int32"
- },
- "message": {
- "type": "string"
- }
+ "security": [
+ {
+ "petstore_auth": [
+ "write:pets",
+ "read:pets"
+ ]
+ }
+ ]
+ },
+ "delete": {
+ "tags": [
+ "pet"
+ ],
+ "summary": "Deletes a pet",
+ "description": "",
+ "operationId": "deletePet",
+ "produces": [
+ "application/xml",
+ "application/json"
+ ],
+ "parameters": [
+ {
+ "name": "api_key",
+ "in": "header",
+ "required": false,
+ "type": "string"
+ },
+ {
+ "name": "petId",
+ "in": "path",
+ "description": "Pet id to delete",
+ "required": true,
+ "type": "integer",
+ "format": "int64"
+ }
+ ],
+ "responses": {
+ "400": {
+ "description": "Invalid ID supplied"
+ },
+ "404": {
+ "description": "Pet not found"
+ }
+ },
+ "security": [
+ {
+ "petstore_auth": [
+ "write:pets",
+ "read:pets"
+ ]
+ }
+ ]
+ }
+ },
+ "/pet/{petId}/uploadImage": {
+ "post": {
+ "tags": [
+ "pet"
+ ],
+ "summary": "uploads an image",
+ "description": "",
+ "operationId": "uploadFile",
+ "consumes": [
+ "multipart/form-data"
+ ],
+ "produces": [
+ "application/json"
+ ],
+ "parameters": [
+ {
+ "name": "petId",
+ "in": "path",
+ "description": "ID of pet to update",
+ "required": true,
+ "type": "integer",
+ "format": "int64"
+ },
+ {
+ "name": "additionalMetadata",
+ "in": "formData",
+ "description": "Additional data to pass to server",
+ "required": false,
+ "type": "string"
+ },
+ {
+ "name": "file",
+ "in": "formData",
+ "description": "file to upload",
+ "required": false,
+ "type": "file"
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "successful operation",
+ "schema": {
+ "$ref": "#/definitions/ApiResponse"
+ }
+ }
+ },
+ "security": [
+ {
+ "petstore_auth": [
+ "write:pets",
+ "read:pets"
+ ]
+ }
+ ]
+ }
+ },
+ "/store/inventory": {
+ "get": {
+ "tags": [
+ "store"
+ ],
+ "summary": "Returns pet inventories by status",
+ "description": "Returns a map of status codes to quantities",
+ "operationId": "getInventory",
+ "produces": [
+ "application/json"
+ ],
+ "parameters": [],
+ "responses": {
+ "200": {
+ "description": "successful operation",
+ "schema": {
+ "type": "object",
+ "additionalProperties": {
+ "type": "integer",
+ "format": "int32"
+ }
+ }
+ }
+ },
+ "security": [
+ {
+ "api_key": []
+ }
+ ]
+ }
+ },
+ "/store/order": {
+ "post": {
+ "tags": [
+ "store"
+ ],
+ "summary": "Place an order for a pet",
+ "description": "",
+ "operationId": "placeOrder",
+ "produces": [
+ "application/xml",
+ "application/json"
+ ],
+ "parameters": [
+ {
+ "in": "body",
+ "name": "body",
+ "description": "order placed for purchasing the pet",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/Order"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "successful operation",
+ "schema": {
+ "$ref": "#/definitions/Order"
+ }
+ },
+ "400": {
+ "description": "Invalid Order"
+ }
+ }
+ }
+ },
+ "/store/order/{orderId}": {
+ "get": {
+ "tags": [
+ "store"
+ ],
+ "summary": "Find purchase order by ID",
+ "description": "For valid response try integer IDs with value >= 1 and <= 10. Other values will generated exceptions",
+ "operationId": "getOrderById",
+ "produces": [
+ "application/xml",
+ "application/json"
+ ],
+ "parameters": [
+ {
+ "name": "orderId",
+ "in": "path",
+ "description": "ID of pet that needs to be fetched",
+ "required": true,
+ "type": "integer",
+ "maximum": 10,
+ "minimum": 1,
+ "format": "int64"
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "successful operation",
+ "schema": {
+ "$ref": "#/definitions/Order"
}
+ },
+ "400": {
+ "description": "Invalid ID supplied"
+ },
+ "404": {
+ "description": "Order not found"
+ }
+ }
+ },
+ "delete": {
+ "tags": [
+ "store"
+ ],
+ "summary": "Delete purchase order by ID",
+ "description": "For valid response try integer IDs with positive integer value. Negative or non-integer values will generate API errors",
+ "operationId": "deleteOrder",
+ "produces": [
+ "application/xml",
+ "application/json"
+ ],
+ "parameters": [
+ {
+ "name": "orderId",
+ "in": "path",
+ "description": "ID of the order that needs to be deleted",
+ "required": true,
+ "type": "integer",
+ "minimum": 1,
+ "format": "int64"
+ }
+ ],
+ "responses": {
+ "400": {
+ "description": "Invalid ID supplied"
+ },
+ "404": {
+ "description": "Order not found"
+ }
+ }
+ }
+ },
+ "/user": {
+ "post": {
+ "tags": [
+ "user"
+ ],
+ "summary": "Create user",
+ "description": "This can only be done by the logged in user.",
+ "operationId": "createUser",
+ "produces": [
+ "application/xml",
+ "application/json"
+ ],
+ "parameters": [
+ {
+ "in": "body",
+ "name": "body",
+ "description": "Created user object",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/User"
+ }
+ }
+ ],
+ "responses": {
+ "default": {
+ "description": "successful operation"
+ }
+ }
+ }
+ },
+ "/user/createWithArray": {
+ "post": {
+ "tags": [
+ "user"
+ ],
+ "summary": "Creates list of users with given input array",
+ "description": "",
+ "operationId": "createUsersWithArrayInput",
+ "produces": [
+ "application/xml",
+ "application/json"
+ ],
+ "parameters": [
+ {
+ "in": "body",
+ "name": "body",
+ "description": "List of user object",
+ "required": true,
+ "schema": {
+ "type": "array",
+ "items": {
+ "$ref": "#/definitions/User"
+ }
+ }
+ }
+ ],
+ "responses": {
+ "default": {
+ "description": "successful operation"
+ }
+ }
+ }
+ },
+ "/user/createWithList": {
+ "post": {
+ "tags": [
+ "user"
+ ],
+ "summary": "Creates list of users with given input array",
+ "description": "",
+ "operationId": "createUsersWithListInput",
+ "produces": [
+ "application/xml",
+ "application/json"
+ ],
+ "parameters": [
+ {
+ "in": "body",
+ "name": "body",
+ "description": "List of user object",
+ "required": true,
+ "schema": {
+ "type": "array",
+ "items": {
+ "$ref": "#/definitions/User"
+ }
+ }
+ }
+ ],
+ "responses": {
+ "default": {
+ "description": "successful operation"
+ }
+ }
+ }
+ },
+ "/user/login": {
+ "get": {
+ "tags": [
+ "user"
+ ],
+ "summary": "Logs user into the system",
+ "description": "",
+ "operationId": "loginUser",
+ "produces": [
+ "application/xml",
+ "application/json"
+ ],
+ "parameters": [
+ {
+ "name": "username",
+ "in": "query",
+ "description": "The user name for login",
+ "required": true,
+ "type": "string"
+ },
+ {
+ "name": "password",
+ "in": "query",
+ "description": "The password for login in clear text",
+ "required": true,
+ "type": "string"
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "successful operation",
+ "schema": {
+ "type": "string"
+ },
+ "headers": {
+ "X-Rate-Limit": {
+ "type": "integer",
+ "format": "int32",
+ "description": "calls per hour allowed by the user"
+ },
+ "X-Expires-After": {
+ "type": "string",
+ "format": "date-time",
+ "description": "date in UTC when token expires"
+ }
+ }
+ },
+ "400": {
+ "description": "Invalid username/password supplied"
+ }
+ }
+ }
+ },
+ "/user/logout": {
+ "get": {
+ "tags": [
+ "user"
+ ],
+ "summary": "Logs out current logged in user session",
+ "description": "",
+ "operationId": "logoutUser",
+ "produces": [
+ "application/xml",
+ "application/json"
+ ],
+ "parameters": [],
+ "responses": {
+ "default": {
+ "description": "successful operation"
+ }
+ }
+ }
+ },
+ "/user/{username}": {
+ "get": {
+ "tags": [
+ "user"
+ ],
+ "summary": "Get user by user name",
+ "description": "",
+ "operationId": "getUserByName",
+ "produces": [
+ "application/xml",
+ "application/json"
+ ],
+ "parameters": [
+ {
+ "name": "username",
+ "in": "path",
+ "description": "The name that needs to be fetched. Use user1 for testing. ",
+ "required": true,
+ "type": "string"
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "successful operation",
+ "schema": {
+ "$ref": "#/definitions/User"
+ }
+ },
+ "400": {
+ "description": "Invalid username supplied"
+ },
+ "404": {
+ "description": "User not found"
+ }
+ }
+ },
+ "put": {
+ "tags": [
+ "user"
+ ],
+ "summary": "Updated user",
+ "description": "This can only be done by the logged in user.",
+ "operationId": "updateUser",
+ "produces": [
+ "application/xml",
+ "application/json"
+ ],
+ "parameters": [
+ {
+ "name": "username",
+ "in": "path",
+ "description": "name that need to be updated",
+ "required": true,
+ "type": "string"
+ },
+ {
+ "in": "body",
+ "name": "body",
+ "description": "Updated user object",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/User"
+ }
+ }
+ ],
+ "responses": {
+ "400": {
+ "description": "Invalid user supplied"
+ },
+ "404": {
+ "description": "User not found"
+ }
+ }
+ },
+ "delete": {
+ "tags": [
+ "user"
+ ],
+ "summary": "Delete user",
+ "description": "This can only be done by the logged in user.",
+ "operationId": "deleteUser",
+ "produces": [
+ "application/xml",
+ "application/json"
+ ],
+ "parameters": [
+ {
+ "name": "username",
+ "in": "path",
+ "description": "The name that needs to be deleted",
+ "required": true,
+ "type": "string"
+ }
+ ],
+ "responses": {
+ "400": {
+ "description": "Invalid username supplied"
+ },
+ "404": {
+ "description": "User not found"
+ }
+ }
+ }
+ }
+ },
+ "securityDefinitions": {
+ "petstore_auth": {
+ "type": "oauth2",
+ "authorizationUrl": "http://petstore.swagger.io/oauth/dialog",
+ "flow": "implicit",
+ "scopes": {
+ "write:pets": "modify pets in your account",
+ "read:pets": "read your pets"
+ }
+ },
+ "api_key": {
+ "type": "apiKey",
+ "name": "api_key",
+ "in": "header"
+ }
+ },
+ "definitions": {
+ "Order": {
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "petId": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "quantity": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "shipDate": {
+ "type": "string",
+ "format": "date-time"
+ },
+ "status": {
+ "type": "string",
+ "description": "Order Status",
+ "enum": [
+ "placed",
+ "approved",
+ "delivered"
+ ]
+ },
+ "complete": {
+ "type": "boolean",
+ "default": false
+ }
+ },
+ "xml": {
+ "name": "Order"
+ }
+ },
+ "Category": {
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ }
+ },
+ "xml": {
+ "name": "Category"
+ }
+ },
+ "User": {
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "username": {
+ "type": "string"
+ },
+ "firstName": {
+ "type": "string"
+ },
+ "lastName": {
+ "type": "string"
+ },
+ "email": {
+ "type": "string"
+ },
+ "password": {
+ "type": "string"
+ },
+ "phone": {
+ "type": "string"
+ },
+ "userStatus": {
+ "type": "integer",
+ "format": "int32",
+ "description": "User Status"
+ }
+ },
+ "xml": {
+ "name": "User"
+ }
+ },
+ "Tag": {
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ }
+ },
+ "xml": {
+ "name": "Tag"
+ }
+ },
+ "Pet": {
+ "type": "object",
+ "required": [
+ "name",
+ "photoUrls"
+ ],
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "category": {
+ "$ref": "#/definitions/Category"
+ },
+ "name": {
+ "type": "string",
+ "example": "doggie"
+ },
+ "photoUrls": {
+ "type": "array",
+ "xml": {
+ "name": "photoUrl",
+ "wrapped": true
+ },
+ "items": {
+ "type": "string"
+ }
+ },
+ "tags": {
+ "type": "array",
+ "xml": {
+ "name": "tag",
+ "wrapped": true
+ },
+ "items": {
+ "$ref": "#/definitions/Tag"
+ }
+ },
+ "status": {
+ "type": "string",
+ "description": "pet status in the store",
+ "enum": [
+ "available",
+ "pending",
+ "sold"
+ ]
+ }
+ },
+ "xml": {
+ "name": "Pet"
+ }
+ },
+ "ApiResponse": {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "type": {
+ "type": "string"
+ },
+ "message": {
+ "type": "string"
}
+ }
}
+ },
+ "externalDocs": {
+ "description": "Find out more about Swagger",
+ "url": "http://swagger.io"
+ }
}
\ No newline at end of file
diff --git a/freemarker-generator-cli/site/sample/yaml/swagger-spec.yaml b/freemarker-generator-cli/site/sample/yaml/swagger-spec.yaml
new file mode 100644
index 0000000..5ab693a
--- /dev/null
+++ b/freemarker-generator-cli/site/sample/yaml/swagger-spec.yaml
@@ -0,0 +1,700 @@
+swagger: "2.0"
+info:
+ description: "This is a sample server Petstore server. You can find out more about Swagger at <a href=\"http://swagger.io\">http://swagger.io</a> or on irc.freenode.net, #swagger. For this sample, you can use the api key \"special-key\" to test the authorization filters"
+ version: 1.0.0
+ title: Swagger Petstore YAML
+ termsOfService: "http://swagger.io/terms/"
+ contact:
+ email: "apiteam@swagger.io"
+ license:
+ name: Apache 2.0
+ url: "http://www.apache.org/licenses/LICENSE-2.0.html"
+basePath: /v2
+tags:
+ - name: pet
+ description: Everything about your Pets
+ externalDocs:
+ description: Find out more
+ url: "http://swagger.io"
+ - name: store
+ description: Operations about user
+ - name: user
+ description: Access to Petstore orders
+ externalDocs:
+ description: Find out more about our store
+ url: "http://swagger.io"
+schemes:
+ - http
+paths:
+ /pet:
+ post:
+ tags:
+ - pet
+ summary: Add a new pet to the store
+ x-swagger-router-controller: SampleController
+ description: ""
+ operationId: addPet
+ consumes:
+ - application/json
+ - application/xml
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - in: body
+ name: body
+ description: Pet object that needs to be added to the store
+ required: false
+ schema:
+ $ref: "#/definitions/Pet"
+ responses:
+ "405":
+ description: Invalid input
+ security:
+ - petstore_auth:
+ - "write:pets"
+ - "read:pets"
+ put:
+ tags:
+ - pet
+ summary: Update an existing pet
+ description: ""
+ operationId: updatePet
+ consumes:
+ - application/json
+ - application/xml
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - in: body
+ name: body
+ description: Pet object that needs to be added to the store
+ required: false
+ schema:
+ $ref: "#/definitions/Pet"
+ responses:
+ "400":
+ description: Invalid ID supplied
+ "404":
+ description: Pet not found
+ "405":
+ description: Validation exception
+ security:
+ - petstore_auth:
+ - "write:pets"
+ - "read:pets"
+ /pet/findByStatus:
+ get:
+ tags:
+ - pet
+ summary: Finds Pets by status
+ description: Multiple status values can be provided with comma separated strings
+ operationId: findPetsByStatus
+ consumes:
+ - application/xml
+ - application/json
+ - multipart/form-data
+ - application/x-www-form-urlencoded
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - name: status
+ in: query
+ description: Status values that need to be considered for filter
+ required: false
+ type: array
+ items:
+ type: string
+ collectionFormat: multi
+ default: available
+ enum:
+ - available
+ - pending
+ - sold
+ responses:
+ "200":
+ description: successful operation
+ schema:
+ type: array
+ items:
+ $ref: "#/definitions/Pet"
+ "400":
+ description: Invalid status value
+ security:
+ - petstore_auth:
+ - "write:pets"
+ - "read:pets"
+ /pet/findByTags:
+ get:
+ tags:
+ - pet
+ summary: Finds Pets by tags
+ description: "Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing."
+ operationId: findPetsByTags
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - name: tags
+ in: query
+ description: Tags to filter by
+ required: false
+ type: array
+ items:
+ type: string
+ collectionFormat: multi
+ responses:
+ "200":
+ description: successful operation
+ schema:
+ type: array
+ items:
+ $ref: "#/definitions/Pet"
+ "400":
+ description: Invalid tag value
+ security:
+ - petstore_auth:
+ - "write:pets"
+ - "read:pets"
+ "/pet/{petId}":
+ get:
+ tags:
+ - pet
+ summary: Find pet by ID
+ description: Returns a single pet
+ operationId: getPetById
+ consumes:
+ - application/x-www-form-urlencoded
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - name: petId
+ in: path
+ description: ID of pet to return
+ required: true
+ type: integer
+ format: int64
+ responses:
+ "200":
+ description: successful operation
+ schema:
+ $ref: "#/definitions/Pet"
+ "400":
+ description: Invalid ID supplied
+ "404":
+ description: Pet not found
+ security:
+ - api_key: []
+ - petstore_auth:
+ - "write:pets"
+ - "read:pets"
+ post:
+ tags:
+ - pet
+ summary: Updates a pet in the store with form data
+ description: ""
+ operationId: updatePetWithForm
+ consumes:
+ - application/x-www-form-urlencoded
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - name: petId
+ in: path
+ description: ID of pet that needs to be updated
+ required: true
+ type: string
+ - name: name
+ in: formData
+ description: Updated name of the pet
+ required: false
+ type: string
+ - name: status
+ in: formData
+ description: Updated status of the pet
+ required: false
+ type: string
+ responses:
+ "405":
+ description: Invalid input
+ security:
+ - petstore_auth:
+ - "write:pets"
+ - "read:pets"
+ delete:
+ tags:
+ - pet
+ summary: Deletes a pet
+ description: ""
+ operationId: deletePet
+ consumes:
+ - multipart/form-data
+ - application/x-www-form-urlencoded
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - name: api_key
+ in: header
+ description: ""
+ required: false
+ type: string
+ - name: petId
+ in: path
+ description: Pet id to delete
+ required: true
+ type: integer
+ format: int64
+ responses:
+ "400":
+ description: Invalid pet value
+ security:
+ - petstore_auth:
+ - "write:pets"
+ - "read:pets"
+ "/pet/{petId}/uploadImage":
+ post:
+ tags:
+ - pet
+ summary: uploads an image
+ x-swagger-router-controller: SampleController
+ description: ""
+ operationId: uploadFile
+ consumes:
+ - multipart/form-data
+ produces:
+ - application/json
+ parameters:
+ - name: petId
+ in: path
+ description: ID of pet to update
+ required: true
+ type: integer
+ format: int64
+ - name: additionalMetadata
+ in: formData
+ description: Additional data to pass to server
+ required: false
+ type: string
+ - name: file
+ in: formData
+ description: file to upload
+ required: false
+ type: file
+ responses:
+ "200":
+ description: successful operation
+ schema:
+ $ref: "#/definitions/ApiResponse"
+ security:
+ - petstore_auth:
+ - "write:pets"
+ - "read:pets"
+ /store/inventory:
+ get:
+ tags:
+ - store
+ summary: Returns pet inventories by status
+ description: Returns a map of status codes to quantities
+ operationId: getInventory
+ produces:
+ - application/json
+ parameters: []
+ responses:
+ "200":
+ description: successful operation
+ schema:
+ type: object
+ additionalProperties:
+ type: integer
+ format: int32
+ security:
+ - api_key: []
+ /store/order:
+ post:
+ tags:
+ - store
+ summary: Place an order for a pet
+ description: ""
+ operationId: placeOrder
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - in: body
+ name: body
+ description: order placed for purchasing the pet
+ required: false
+ schema:
+ $ref: "#/definitions/Order"
+ responses:
+ "200":
+ description: successful operation
+ schema:
+ $ref: "#/definitions/Order"
+ "400":
+ description: Invalid Order
+ "/store/order/{orderId}":
+ get:
+ tags:
+ - store
+ summary: Find purchase order by ID
+ description: "For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions"
+ operationId: getOrderById
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - name: orderId
+ in: path
+ description: ID of pet that needs to be fetched
+ required: true
+ type: string
+ responses:
+ "200":
+ description: successful operation
+ schema:
+ $ref: "#/definitions/Order"
+ "400":
+ description: Invalid ID supplied
+ "404":
+ description: Order not found
+ delete:
+ tags:
+ - store
+ summary: Delete purchase order by ID
+ description: For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
+ operationId: deleteOrder
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - name: orderId
+ in: path
+ description: ID of the order that needs to be deleted
+ required: true
+ type: string
+ responses:
+ "400":
+ description: Invalid ID supplied
+ "404":
+ description: Order not found
+ /user:
+ post:
+ tags:
+ - user
+ summary: Create user
+ description: This can only be done by the logged in user.
+ operationId: createUser
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - in: body
+ name: body
+ description: Created user object
+ required: false
+ schema:
+ $ref: "#/definitions/User"
+ responses:
+ default:
+ description: successful operation
+ /user/createWithArray:
+ post:
+ tags:
+ - user
+ summary: Creates list of users with given input array
+ description: ""
+ operationId: createUsersWithArrayInput
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - in: body
+ name: body
+ description: List of user object
+ required: false
+ schema:
+ type: array
+ items:
+ $ref: "#/definitions/User"
+ responses:
+ default:
+ description: successful operation
+ /user/createWithList:
+ post:
+ tags:
+ - user
+ summary: Creates list of users with given input array
+ description: ""
+ operationId: createUsersWithListInput
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - in: body
+ name: body
+ description: List of user object
+ required: false
+ schema:
+ type: array
+ items:
+ $ref: "#/definitions/User"
+ responses:
+ default:
+ description: successful operation
+ /user/login:
+ get:
+ tags:
+ - user
+ summary: Logs user into the system
+ description: ""
+ operationId: loginUser
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - name: username
+ in: query
+ description: The user name for login
+ required: false
+ type: string
+ - name: password
+ in: query
+ description: The password for login in clear text
+ required: false
+ type: string
+ responses:
+ "200":
+ description: successful operation
+ schema:
+ type: string
+ headers:
+ X-Rate-Limit:
+ type: integer
+ format: int32
+ description: calls per hour allowed by the user
+ X-Expires-After:
+ type: string
+ format: date-time
+ description: date in UTC when toekn expires
+ "400":
+ description: Invalid username/password supplied
+ /user/logout:
+ get:
+ tags:
+ - user
+ summary: Logs out current logged in user session
+ description: ""
+ operationId: logoutUser
+ produces:
+ - application/xml
+ - application/json
+ parameters: []
+ responses:
+ default:
+ description: successful operation
+ "/user/{username}":
+ get:
+ tags:
+ - user
+ summary: Get user by user name
+ description: ""
+ operationId: getUserByName
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - name: username
+ in: path
+ description: "The name that needs to be fetched. Use user1 for testing. "
+ required: true
+ type: string
+ responses:
+ "200":
+ description: successful operation
+ schema:
+ $ref: "#/definitions/User"
+ "400":
+ description: Invalid username supplied
+ "404":
+ description: User not found
+ put:
+ tags:
+ - user
+ summary: Updated user
+ description: This can only be done by the logged in user.
+ operationId: updateUser
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - name: username
+ in: path
+ description: name that need to be deleted
+ required: true
+ type: string
+ - in: body
+ name: body
+ description: Updated user object
+ required: false
+ schema:
+ $ref: "#/definitions/User"
+ responses:
+ "400":
+ description: Invalid user supplied
+ "404":
+ description: User not found
+ delete:
+ tags:
+ - user
+ summary: Delete user
+ description: This can only be done by the logged in user.
+ operationId: deleteUser
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - name: username
+ in: path
+ description: The name that needs to be deleted
+ required: true
+ type: string
+ responses:
+ "400":
+ description: Invalid username supplied
+ "404":
+ description: User not found
+securityDefinitions:
+ petstore_auth:
+ type: oauth2
+ authorizationUrl: "http://petstore.swagger.io/api/oauth/dialog"
+ flow: implicit
+ scopes:
+ "write:pets": modify pets in your account
+ "read:pets": read your pets
+ api_key:
+ type: apiKey
+ name: api_key
+ in: header
+definitions:
+ Order:
+ properties:
+ id:
+ type: integer
+ format: int64
+ petId:
+ type: integer
+ format: int64
+ quantity:
+ type: integer
+ format: int32
+ shipDate:
+ type: string
+ format: date-time
+ status:
+ type: string
+ description: Order Status
+ enum:
+ - placed
+ - approved
+ - delivered
+ complete:
+ type: boolean
+ xml:
+ name: Order
+ Category:
+ properties:
+ id:
+ type: integer
+ format: int64
+ name:
+ type: string
+ xml:
+ name: Category
+ User:
+ properties:
+ id:
+ type: integer
+ format: int64
+ username:
+ type: string
+ firstName:
+ type: string
+ lastName:
+ type: string
+ email:
+ type: string
+ password:
+ type: string
+ phone:
+ type: string
+ userStatus:
+ type: integer
+ format: int32
+ description: User Status
+ xml:
+ name: User
+ Tag:
+ properties:
+ id:
+ type: integer
+ format: int64
+ name:
+ type: string
+ xml:
+ name: Tag
+ Pet:
+ required:
+ - name
+ - photoUrls
+ properties:
+ id:
+ type: integer
+ format: int64
+ category:
+ $ref: "#/definitions/Category"
+ name:
+ type: string
+ example: doggie
+ photoUrls:
+ type: array
+ xml:
+ name: photoUrl
+ wrapped: true
+ items:
+ type: string
+ tags:
+ type: array
+ xml:
+ name: tag
+ wrapped: true
+ items:
+ $ref: "#/definitions/Tag"
+ status:
+ type: string
+ description: pet status in the store
+ enum:
+ - available
+ - pending
+ - sold
+ xml:
+ name: Pet
+ ApiResponse:
+ properties:
+ code:
+ type: integer
+ format: int32
+ type:
+ type: string
+ message:
+ type: string
+ xml:
+ name: "##default"
+externalDocs:
+ description: Find out more about Swagger
+ url: "http://swagger.io"
\ No newline at end of file
diff --git a/freemarker-generator-cli/src/main/config/freemarker-cli.properties b/freemarker-generator-cli/src/main/config/freemarker-cli.properties
index af23b05..fa08255 100644
--- a/freemarker-generator-cli/src/main/config/freemarker-cli.properties
+++ b/freemarker-generator-cli/src/main/config/freemarker-cli.properties
@@ -29,6 +29,7 @@ freemarker.tools.ExecTool=org.apache.freemarker.generator.tools.commonsexec.Comm
freemarker.tools.ExcelTool=org.apache.freemarker.generator.tools.excel.ExcelTool
freemarker.tools.FreeMarkerTool=org.apache.freemarker.generator.tools.freemarker.FreeMarkerTool
freemarker.tools.GrokTool=org.apache.freemarker.generator.tools.grok.GrokTool
+freemarker.tools.GsonTool=org.apache.freemarker.generator.tools.gson.GsonTool
freemarker.tools.JsonPathTool=org.apache.freemarker.generator.tools.jsonpath.JsonPathTool
freemarker.tools.JsoupTool=org.apache.freemarker.generator.tools.jsoup.JsoupTool
freemarker.tools.PropertiesTool=org.apache.freemarker.generator.tools.properties.PropertiesTool
diff --git a/freemarker-generator-cli/src/main/resources/freemarker-cli.properties b/freemarker-generator-cli/src/main/resources/freemarker-cli.properties
index 8dd5f59..0a0542a 100644
--- a/freemarker-generator-cli/src/main/resources/freemarker-cli.properties
+++ b/freemarker-generator-cli/src/main/resources/freemarker-cli.properties
@@ -29,6 +29,7 @@ freemarker.tools.ExcelTool=org.apache.freemarker.generator.tools.excel.ExcelTool
freemarker.tools.ExecTool=org.apache.freemarker.generator.tools.commonsexec.CommonsExecTool
freemarker.tools.FreeMarkerTool=org.apache.freemarker.generator.tools.freemarker.FreeMarkerTool
freemarker.tools.GrokTool=org.apache.freemarker.generator.tools.grok.GrokTool
+freemarker.tools.GsonTool=org.apache.freemarker.generator.tools.gson.GsonTool
freemarker.tools.JsonPathTool=org.apache.freemarker.generator.tools.jsonpath.JsonPathTool
freemarker.tools.JsoupTool=org.apache.freemarker.generator.tools.jsoup.JsoupTool
freemarker.tools.PropertiesTool=org.apache.freemarker.generator.tools.properties.PropertiesTool
diff --git a/freemarker-generator-cli/src/main/scripts/run-samples.sh b/freemarker-generator-cli/src/main/scripts/run-samples.sh
index 0366d5e..c9f335e 100755
--- a/freemarker-generator-cli/src/main/scripts/run-samples.sh
+++ b/freemarker-generator-cli/src/main/scripts/run-samples.sh
@@ -49,6 +49,8 @@ $FREEMARKER_CMD -t templates/demo.ftl README.md > target/out/demo.txt || { echo
$FREEMARKER_CMD -i '${JsonPathTool.parse(DataSources.first).read("$.info.title")}' site/sample/json/swagger-spec.json > target/out/interactive-json.txt || { echo >&2 "Test failed. Aborting."; exit 1; }
$FREEMARKER_CMD -i '${XmlTool.parse(DataSources.first)["recipients/person[1]/name"]}' site/sample/xml/recipients.xml > target/out/interactive-xml.txt || { echo >&2 "Test failed. Aborting."; exit 1; }
$FREEMARKER_CMD -i '${JsoupTool.parse(DataSources.first).select("a")[0]}' site/sample/html/dependencies.html > target/out/interactive-html.txt || { echo >&2 "Test failed. Aborting."; exit 1; }
+$FREEMARKER_CMD -i '${GsonTool.toJson(YamlTool.parse(DataSources.get(0)))}' site/sample/yaml/swagger-spec.yaml > target/out/interactive-swagger.json || { echo >&2 "Test failed. Aborting."; exit 1; }
+$FREEMARKER_CMD -i '${YamlTool.toYaml(GsonTool.parse(DataSources.get(0)))}' site/sample/json/swagger-spec.json > target/out/interactive-swagger.yaml || { echo >&2 "Test failed. Aborting."; exit 1; }
#############################################################################
# CSV
@@ -137,6 +139,9 @@ $FREEMARKER_CMD -t templates/html/csv/dependencies.ftl site/sample/html/dependen
echo "templates/json/csv/swagger-endpoints.ftl"
$FREEMARKER_CMD -t templates/json/csv/swagger-endpoints.ftl site/sample/json/swagger-spec.json > target/out/swagger-spec.csv || { echo >&2 "Test failed. Aborting."; exit 1; }
+echo "templates/json/yaml/transform.ftl"
+$FREEMARKER_CMD -t templates/json/yaml/transform.ftl site/sample/json/swagger-spec.json > target/out/swagger-spec.yaml || { echo >&2 "Test failed. Aborting."; exit 1; }
+
if hash curl 2>/dev/null; then
echo "templates/json/md/github-users.ftl"
$FREEMARKER_CMD -t templates/json/md/github-users.ftl site/sample/json/github-users.json > target/out/github-users-curl.md || { echo >&2 "Test failed. Aborting."; exit 1; }
@@ -154,7 +159,10 @@ $FREEMARKER_CMD -t templates/properties/csv/locker-test-users.ftl site/sample/pr
#############################################################################
echo "templates/yaml/txt/transform.ftl"
-$FREEMARKER_CMD -t ./templates/yaml/txt/transform.ftl ./site/sample/yaml/customer.yaml > target/out/customer.txt || { echo >&2 "Test failed. Aborting."; exit 1; }
+$FREEMARKER_CMD -t templates/yaml/txt/transform.ftl site/sample/yaml/customer.yaml > target/out/customer.txt || { echo >&2 "Test failed. Aborting."; exit 1; }
+
+echo "templates/yaml/json/transform.ftl"
+$FREEMARKER_CMD -t templates/yaml/json/transform.ftl site/sample/yaml/swagger-spec.yaml > target/out/swagger-spec.json || { echo >&2 "Test failed. Aborting."; exit 1; }
#############################################################################
# XML
@@ -164,4 +172,4 @@ echo "templates/xml/txt/recipients.ftl"
$FREEMARKER_CMD -t ./templates/xml/txt/recipients.ftl site/sample/xml/recipients.xml > target/out/recipients.txt || { echo >&2 "Test failed. Aborting."; exit 1; }
echo "Created the following sample files in ./target/out"
-ls -l ./target/out
+ls -l ./target/out
\ No newline at end of file
diff --git a/freemarker-generator-cli/src/test/java/org/apache/freemarker/generator/cli/ExamplesTest.java b/freemarker-generator-cli/src/test/java/org/apache/freemarker/generator/cli/ExamplesTest.java
index 7039efb..295b2e7 100644
--- a/freemarker-generator-cli/src/test/java/org/apache/freemarker/generator/cli/ExamplesTest.java
+++ b/freemarker-generator-cli/src/test/java/org/apache/freemarker/generator/cli/ExamplesTest.java
@@ -73,6 +73,7 @@ public class ExamplesTest extends AbstractMainTest {
public void shouldRunJsonExamples() throws IOException {
assertValid(execute("-t templates/json/csv/swagger-endpoints.ftl site/sample/json/swagger-spec.json"));
assertValid(execute("-t templates/json/md/github-users.ftl site/sample/json/github-users.json"));
+ assertValid(execute("-t templates/json/yaml/transform.ftl site/sample/json/swagger-spec.json"));
}
@Test
@@ -83,6 +84,7 @@ public class ExamplesTest extends AbstractMainTest {
@Test
public void shouldRunYamlExamples() throws IOException {
assertValid(execute("-t templates/yaml/txt/transform.ftl site/sample/yaml/customer.yaml"));
+ assertValid(execute("-t templates/yaml/json/transform.ftl site/sample/yaml/swagger-spec.yaml"));
}
@Test
@@ -102,10 +104,11 @@ public class ExamplesTest extends AbstractMainTest {
@Test
public void shouldRunInteractiveTemplateExamples() throws IOException {
- // @TODO We should check the generated output directly
assertValid(execute("-i ${JsonPathTool.parse(DataSources.first).read(\"$.info.title\")} site/sample/json/swagger-spec.json"));
assertValid(execute("-i ${XmlTool.parse(DataSources.first)[\"recipients/person[1]/name\"]} site/sample/xml/recipients.xml"));
assertValid(execute("-i ${JsoupTool.parse(DataSources.first).select(\"a\")[0]} site/sample/html/dependencies.html"));
+ assertValid(execute("-i ${GsonTool.toJson(YamlTool.parse(DataSources.get(0)))} site/sample/yaml/swagger-spec.yaml"));
+ assertValid(execute("-i ${YamlTool.toYaml(GsonTool.parse(DataSources.get(0)))} site/sample/json/swagger-spec.json"));
}
@Test
@@ -130,6 +133,7 @@ public class ExamplesTest extends AbstractMainTest {
private static void assertValid(String output) {
assertTrue(output.length() > MIN_OUTPUT_SIZE);
assertFalse(output.contains("Exception"));
- assertFalse(output.contains("Error"));
+ assertFalse(output.contains("FreeMarker template error"));
+ assertFalse(output.contains("FTL stack trace"));
}
}
diff --git a/freemarker-generator-cli/src/test/java/org/apache/freemarker/generator/cli/ManualTest.java b/freemarker-generator-cli/src/test/java/org/apache/freemarker/generator/cli/ManualTest.java
index 8eb44cf..0d8ca31 100644
--- a/freemarker-generator-cli/src/test/java/org/apache/freemarker/generator/cli/ManualTest.java
+++ b/freemarker-generator-cli/src/test/java/org/apache/freemarker/generator/cli/ManualTest.java
@@ -39,7 +39,9 @@ public class ManualTest {
// private static final String CMD = "-b ./src/test -l de_AT -DFOO=foo -DBAR=bar -t templates/info.ftl -d user:admin=site/sample/csv/contract.csv#charset=UTF-16 google:www=https://www.google.com?foo=bar#contenttype=application/json";
// private static final String CMD = "-b ./src/test -t templates/info.ftl -d :user=site/sample/properties -d contract=site/sample/csv/contract.csv";
// private static final String CMD = "-b ./src/test -t site/sample/ftl/nginx/nginx.conf.ftl -d env=site/sample/ftl/nginx/nginx.env";
- private static final String CMD = "-b ./src/test -t templates/info.ftl -d env=site/sample/ftl/nginx/nginx.env";
+ // private static final String CMD = "-b ./src/test -t templates/info.ftl -d env=site/sample/ftl/nginx/nginx.env";
+ private static final String CMD = "-b ./src/test -t templates/json/yaml/transform.ftl site/sample/json/swagger-spec.json";
+ // private static final String CMD = "-b ./src/test -t templates/yaml/json/transform.ftl site/sample/yaml/swagger-spec.yaml";
public static void main(String[] args) {
Main.execute(toArgs(CMD));
diff --git a/freemarker-generator-cli/templates/json/csv/swagger-endpoints.ftl b/freemarker-generator-cli/templates/json/csv/swagger-endpoints.ftl
index 7fab37f..5f9d6a0 100644
--- a/freemarker-generator-cli/templates/json/csv/swagger-endpoints.ftl
+++ b/freemarker-generator-cli/templates/json/csv/swagger-endpoints.ftl
@@ -15,21 +15,23 @@
specific language governing permissions and limitations
under the License.
-->
-<#assign json = JsonPathTool.parse(DataSources.get(0))>
-<#assign basePath = json.read("$.basePath")>
-<#assign paths = json.read("$.paths")>
+<#assign map = GsonTool.parse(DataSources.get(0))>
+<#assign basePath = map.basePath!"/">
+<#assign paths = map.paths!{}>
<#compress>
- ENDPOINT;METHOD;CONSUMES;PRODUCES;SUMMARY;DESCRIPTION
+ ENDPOINT;METHOD;CONSUMES;PRODUCES;PARAMETERS;SUMMARY;DESCRIPTION
<#list paths as endpoint,metadata>
- <#assign relative_url = basePath + endpoint>
- <#assign methods = metadata?keys>
- <#list methods as method>
- <#assign summary = sanitize(paths[endpoint][method]["summary"]!"")>
- <#assign description = sanitize(paths[endpoint][method]["description"]!"")>
- <#assign consumes = join(paths[endpoint][method]["consumes"]![])>
- <#assign produces = join(paths[endpoint][method]["produces"]![])>
- ${relative_url};${method?upper_case};${consumes};${produces};${summary};${description}
+ <#assign url = basePath + endpoint>
+ <#assign names = metadata?keys?sort>
+ <#list names as name>
+ <#assign method = paths[endpoint][name]>
+ <#assign summary = sanitize(method["summary"]!"")>
+ <#assign description = sanitize(method["description"]!"")>
+ <#assign consumes = join(method["consumes"]![])>
+ <#assign produces = join(method["produces"]![])>
+ <#assign parameters = method["parameters"]>
+ ${url};${name?upper_case};${consumes};${produces};${parameters?size};${summary};${description}
</#list>
</#list>
</#compress>
diff --git a/freemarker-generator-cli/templates/json/yaml/transform.ftl b/freemarker-generator-cli/templates/json/yaml/transform.ftl
new file mode 100644
index 0000000..80c8d2f
--- /dev/null
+++ b/freemarker-generator-cli/templates/json/yaml/transform.ftl
@@ -0,0 +1,17 @@
+<#--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+ http://www.apache.org/licenses/LICENSE-2.0
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-->
+${YamlTool.toYaml(GsonTool.parse(DataSources.get(0)))}
\ No newline at end of file
diff --git a/freemarker-generator-cli/templates/yaml/json/transform.ftl b/freemarker-generator-cli/templates/yaml/json/transform.ftl
new file mode 100644
index 0000000..e5e4d90
--- /dev/null
+++ b/freemarker-generator-cli/templates/yaml/json/transform.ftl
@@ -0,0 +1,17 @@
+<#--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+ http://www.apache.org/licenses/LICENSE-2.0
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-->
+${GsonTool.toJson(YamlTool.parse(DataSources.get(0)))}
\ No newline at end of file
diff --git a/freemarker-generator-tools/pom.xml b/freemarker-generator-tools/pom.xml
index 80815d9..95e7d4c 100644
--- a/freemarker-generator-tools/pom.xml
+++ b/freemarker-generator-tools/pom.xml
@@ -42,55 +42,19 @@
<artifactId>freemarker-generator-base</artifactId>
<version>${project.version}</version>
</dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- </dependency>
- <dependency>
- <groupId>io.krakens</groupId>
- <artifactId>java-grok</artifactId>
- <version>0.1.9</version>
- </dependency>
- <dependency>
- <groupId>com.jayway.jsonpath</groupId>
- <artifactId>json-path</artifactId>
- <version>2.4.0</version>
- </dependency>
- <dependency>
- <groupId>info.picocli</groupId>
- <artifactId>picocli</artifactId>
- <version>4.1.4</version>
- </dependency>
- <dependency>
- <groupId>javax.activation</groupId>
- <artifactId>activation</artifactId>
- </dependency>
- <dependency>
- <groupId>org.jsoup</groupId>
- <artifactId>jsoup</artifactId>
- <version>1.12.1</version>
- </dependency>
- <dependency>
- <groupId>org.yaml</groupId>
- <artifactId>snakeyaml</artifactId>
- <version>1.25</version>
- </dependency>
- <!-- Apache libraries -->
+ <!-- CommonsCSVTool -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-exec</artifactId>
<version>1.3</version>
</dependency>
+ <!-- CommonsExecTool -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.8</version>
</dependency>
- <dependency>
- <groupId>org.freemarker</groupId>
- <artifactId>freemarker</artifactId>
- </dependency>
- <!-- Apache POI -->
+ <!-- ExcelTool -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
@@ -106,6 +70,41 @@
<artifactId>poi-ooxml-schemas</artifactId>
<version>${apache.poi.version}</version>
</dependency>
+ <!-- FreeMarkerTool -->
+ <dependency>
+ <groupId>org.freemarker</groupId>
+ <artifactId>freemarker</artifactId>
+ </dependency>
+ <!-- GrokTool -->
+ <dependency>
+ <groupId>io.krakens</groupId>
+ <artifactId>java-grok</artifactId>
+ <version>0.1.9</version>
+ </dependency>
+ <!-- GsonTool -->
+ <dependency>
+ <groupId>com.google.code.gson</groupId>
+ <artifactId>gson</artifactId>
+ <version>2.8.6</version>
+ </dependency>
+ <!-- JsonPath Tool -->
+ <dependency>
+ <groupId>com.jayway.jsonpath</groupId>
+ <artifactId>json-path</artifactId>
+ <version>2.4.0</version>
+ </dependency>
+ <!-- JsoupTool -->
+ <dependency>
+ <groupId>org.jsoup</groupId>
+ <artifactId>jsoup</artifactId>
+ <version>1.12.1</version>
+ </dependency>
+ <!-- SnakeYamlTool -->
+ <dependency>
+ <groupId>org.yaml</groupId>
+ <artifactId>snakeyaml</artifactId>
+ <version>1.25</version>
+ </dependency>
<!-- Testing -->
<dependency>
<groupId>junit</groupId>
diff --git a/freemarker-generator-tools/src/main/java/org/apache/freemarker/generator/tools/gson/GsonTool.java b/freemarker-generator-tools/src/main/java/org/apache/freemarker/generator/tools/gson/GsonTool.java
new file mode 100644
index 0000000..9805afd
--- /dev/null
+++ b/freemarker-generator-tools/src/main/java/org/apache/freemarker/generator/tools/gson/GsonTool.java
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.freemarker.generator.tools.gson;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.reflect.TypeToken;
+import com.google.gson.stream.JsonReader;
+import org.apache.freemarker.generator.base.datasource.DataSource;
+
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.lang.reflect.Type;
+import java.util.Map;
+
+public class GsonTool {
+
+ private Gson gson;
+ private Type type;
+
+ public Map<String, Object> parse(DataSource dataSource) throws IOException {
+ try (JsonReader reader = new JsonReader(new InputStreamReader(dataSource.getUnsafeInputStream()))) {
+ return gson().fromJson(reader, type());
+ }
+ }
+
+ public Map<String, Object> parse(String json) {
+ return gson().fromJson(json, type());
+ }
+
+ public String toJson(Object src) {
+ return gson().toJson(src);
+ }
+
+ @Override
+ public String toString() {
+ return "Process JSON files using GSON (see https://github.com/google/gson)";
+ }
+
+ private synchronized Gson gson() {
+ if (gson == null) {
+ gson = new GsonBuilder().setLenient().setPrettyPrinting().disableHtmlEscaping().create();
+ }
+ return gson;
+ }
+
+ private synchronized Type type() {
+ if (type == null) {
+ type = new TypeToken<Map<String, Object>>() {}.getType();
+ }
+ return type;
+ }
+}
diff --git a/freemarker-generator-tools/src/main/java/org/apache/freemarker/generator/tools/snakeyaml/SnakeYamlTool.java b/freemarker-generator-tools/src/main/java/org/apache/freemarker/generator/tools/snakeyaml/SnakeYamlTool.java
index 6d1b56a..1058d47 100644
--- a/freemarker-generator-tools/src/main/java/org/apache/freemarker/generator/tools/snakeyaml/SnakeYamlTool.java
+++ b/freemarker-generator-tools/src/main/java/org/apache/freemarker/generator/tools/snakeyaml/SnakeYamlTool.java
@@ -17,28 +17,53 @@
package org.apache.freemarker.generator.tools.snakeyaml;
import org.apache.freemarker.generator.base.datasource.DataSource;
+import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
+import static org.yaml.snakeyaml.DumperOptions.FlowStyle.BLOCK;
+
public class SnakeYamlTool {
+ private Yaml yaml;
+
public Map<String, Object> parse(DataSource dataSource) {
try (InputStream is = dataSource.getUnsafeInputStream()) {
- return new Yaml().load(is);
+ return yaml().load(is);
} catch (IOException e) {
throw new RuntimeException("Failed to load YAML data source: " + dataSource, e);
}
}
public Map<String, Object> parse(String value) {
- return new Yaml().load(value);
+ return yaml().load(value);
+ }
+
+ public String toYaml(Object data) {
+ return yaml().dump(data);
}
@Override
public String toString() {
return "Process YAML files using SnakeYAML(see https://bitbucket.org/asomov/snakeyaml/wiki/Home)";
}
+
+ private synchronized Yaml yaml() {
+ if (yaml == null) {
+ yaml = new Yaml(options());
+ }
+ return yaml;
+ }
+
+ private static DumperOptions options() {
+ final DumperOptions options = new DumperOptions();
+ options.setDefaultFlowStyle(BLOCK);
+ options.setPrettyFlow(true);
+ options.setSplitLines(false);
+ options.setIndent(2);
+ return options;
+ }
}
diff --git a/freemarker-generator-tools/src/test/data/json/swagger.json b/freemarker-generator-tools/src/test/data/json/swagger.json
new file mode 100644
index 0000000..fd958bd
--- /dev/null
+++ b/freemarker-generator-tools/src/test/data/json/swagger.json
@@ -0,0 +1,1054 @@
+{
+ "swagger": "2.0",
+ "info": {
+ "description": "This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters.",
+ "version": "1.0.5",
+ "title": "Swagger Petstore",
+ "termsOfService": "http://swagger.io/terms/",
+ "contact": {
+ "email": "apiteam@swagger.io"
+ },
+ "license": {
+ "name": "Apache 2.0",
+ "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
+ }
+ },
+ "host": "petstore.swagger.io",
+ "basePath": "/v2",
+ "tags": [
+ {
+ "name": "pet",
+ "description": "Everything about your Pets",
+ "externalDocs": {
+ "description": "Find out more",
+ "url": "http://swagger.io"
+ }
+ },
+ {
+ "name": "store",
+ "description": "Access to Petstore orders"
+ },
+ {
+ "name": "user",
+ "description": "Operations about user",
+ "externalDocs": {
+ "description": "Find out more about our store",
+ "url": "http://swagger.io"
+ }
+ }
+ ],
+ "schemes": [
+ "https",
+ "http"
+ ],
+ "paths": {
+ "/pet/{petId}/uploadImage": {
+ "post": {
+ "tags": [
+ "pet"
+ ],
+ "summary": "uploads an image",
+ "description": "",
+ "operationId": "uploadFile",
+ "consumes": [
+ "multipart/form-data"
+ ],
+ "produces": [
+ "application/json"
+ ],
+ "parameters": [
+ {
+ "name": "petId",
+ "in": "path",
+ "description": "ID of pet to update",
+ "required": true,
+ "type": "integer",
+ "format": "int64"
+ },
+ {
+ "name": "additionalMetadata",
+ "in": "formData",
+ "description": "Additional data to pass to server",
+ "required": false,
+ "type": "string"
+ },
+ {
+ "name": "file",
+ "in": "formData",
+ "description": "file to upload",
+ "required": false,
+ "type": "file"
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "successful operation",
+ "schema": {
+ "$ref": "#/definitions/ApiResponse"
+ }
+ }
+ },
+ "security": [
+ {
+ "petstore_auth": [
+ "write:pets",
+ "read:pets"
+ ]
+ }
+ ]
+ }
+ },
+ "/pet": {
+ "post": {
+ "tags": [
+ "pet"
+ ],
+ "summary": "Add a new pet to the store",
+ "description": "",
+ "operationId": "addPet",
+ "consumes": [
+ "application/json",
+ "application/xml"
+ ],
+ "produces": [
+ "application/json",
+ "application/xml"
+ ],
+ "parameters": [
+ {
+ "in": "body",
+ "name": "body",
+ "description": "Pet object that needs to be added to the store",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/Pet"
+ }
+ }
+ ],
+ "responses": {
+ "405": {
+ "description": "Invalid input"
+ }
+ },
+ "security": [
+ {
+ "petstore_auth": [
+ "write:pets",
+ "read:pets"
+ ]
+ }
+ ]
+ },
+ "put": {
+ "tags": [
+ "pet"
+ ],
+ "summary": "Update an existing pet",
+ "description": "",
+ "operationId": "updatePet",
+ "consumes": [
+ "application/json",
+ "application/xml"
+ ],
+ "produces": [
+ "application/json",
+ "application/xml"
+ ],
+ "parameters": [
+ {
+ "in": "body",
+ "name": "body",
+ "description": "Pet object that needs to be added to the store",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/Pet"
+ }
+ }
+ ],
+ "responses": {
+ "400": {
+ "description": "Invalid ID supplied"
+ },
+ "404": {
+ "description": "Pet not found"
+ },
+ "405": {
+ "description": "Validation exception"
+ }
+ },
+ "security": [
+ {
+ "petstore_auth": [
+ "write:pets",
+ "read:pets"
+ ]
+ }
+ ]
+ }
+ },
+ "/pet/findByStatus": {
+ "get": {
+ "tags": [
+ "pet"
+ ],
+ "summary": "Finds Pets by status",
+ "description": "Multiple status values can be provided with comma separated strings",
+ "operationId": "findPetsByStatus",
+ "produces": [
+ "application/json",
+ "application/xml"
+ ],
+ "parameters": [
+ {
+ "name": "status",
+ "in": "query",
+ "description": "Status values that need to be considered for filter",
+ "required": true,
+ "type": "array",
+ "items": {
+ "type": "string",
+ "enum": [
+ "available",
+ "pending",
+ "sold"
+ ],
+ "default": "available"
+ },
+ "collectionFormat": "multi"
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "successful operation",
+ "schema": {
+ "type": "array",
+ "items": {
+ "$ref": "#/definitions/Pet"
+ }
+ }
+ },
+ "400": {
+ "description": "Invalid status value"
+ }
+ },
+ "security": [
+ {
+ "petstore_auth": [
+ "write:pets",
+ "read:pets"
+ ]
+ }
+ ]
+ }
+ },
+ "/pet/findByTags": {
+ "get": {
+ "tags": [
+ "pet"
+ ],
+ "summary": "Finds Pets by tags",
+ "description": "Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.",
+ "operationId": "findPetsByTags",
+ "produces": [
+ "application/json",
+ "application/xml"
+ ],
+ "parameters": [
+ {
+ "name": "tags",
+ "in": "query",
+ "description": "Tags to filter by",
+ "required": true,
+ "type": "array",
+ "items": {
+ "type": "string"
+ },
+ "collectionFormat": "multi"
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "successful operation",
+ "schema": {
+ "type": "array",
+ "items": {
+ "$ref": "#/definitions/Pet"
+ }
+ }
+ },
+ "400": {
+ "description": "Invalid tag value"
+ }
+ },
+ "security": [
+ {
+ "petstore_auth": [
+ "write:pets",
+ "read:pets"
+ ]
+ }
+ ],
+ "deprecated": true
+ }
+ },
+ "/pet/{petId}": {
+ "get": {
+ "tags": [
+ "pet"
+ ],
+ "summary": "Find pet by ID",
+ "description": "Returns a single pet",
+ "operationId": "getPetById",
+ "produces": [
+ "application/json",
+ "application/xml"
+ ],
+ "parameters": [
+ {
+ "name": "petId",
+ "in": "path",
+ "description": "ID of pet to return",
+ "required": true,
+ "type": "integer",
+ "format": "int64"
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "successful operation",
+ "schema": {
+ "$ref": "#/definitions/Pet"
+ }
+ },
+ "400": {
+ "description": "Invalid ID supplied"
+ },
+ "404": {
+ "description": "Pet not found"
+ }
+ },
+ "security": [
+ {
+ "api_key": []
+ }
+ ]
+ },
+ "post": {
+ "tags": [
+ "pet"
+ ],
+ "summary": "Updates a pet in the store with form data",
+ "description": "",
+ "operationId": "updatePetWithForm",
+ "consumes": [
+ "application/x-www-form-urlencoded"
+ ],
+ "produces": [
+ "application/json",
+ "application/xml"
+ ],
+ "parameters": [
+ {
+ "name": "petId",
+ "in": "path",
+ "description": "ID of pet that needs to be updated",
+ "required": true,
+ "type": "integer",
+ "format": "int64"
+ },
+ {
+ "name": "name",
+ "in": "formData",
+ "description": "Updated name of the pet",
+ "required": false,
+ "type": "string"
+ },
+ {
+ "name": "status",
+ "in": "formData",
+ "description": "Updated status of the pet",
+ "required": false,
+ "type": "string"
+ }
+ ],
+ "responses": {
+ "405": {
+ "description": "Invalid input"
+ }
+ },
+ "security": [
+ {
+ "petstore_auth": [
+ "write:pets",
+ "read:pets"
+ ]
+ }
+ ]
+ },
+ "delete": {
+ "tags": [
+ "pet"
+ ],
+ "summary": "Deletes a pet",
+ "description": "",
+ "operationId": "deletePet",
+ "produces": [
+ "application/json",
+ "application/xml"
+ ],
+ "parameters": [
+ {
+ "name": "api_key",
+ "in": "header",
+ "required": false,
+ "type": "string"
+ },
+ {
+ "name": "petId",
+ "in": "path",
+ "description": "Pet id to delete",
+ "required": true,
+ "type": "integer",
+ "format": "int64"
+ }
+ ],
+ "responses": {
+ "400": {
+ "description": "Invalid ID supplied"
+ },
+ "404": {
+ "description": "Pet not found"
+ }
+ },
+ "security": [
+ {
+ "petstore_auth": [
+ "write:pets",
+ "read:pets"
+ ]
+ }
+ ]
+ }
+ },
+ "/store/order": {
+ "post": {
+ "tags": [
+ "store"
+ ],
+ "summary": "Place an order for a pet",
+ "description": "",
+ "operationId": "placeOrder",
+ "consumes": [
+ "application/json"
+ ],
+ "produces": [
+ "application/json",
+ "application/xml"
+ ],
+ "parameters": [
+ {
+ "in": "body",
+ "name": "body",
+ "description": "order placed for purchasing the pet",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/Order"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "successful operation",
+ "schema": {
+ "$ref": "#/definitions/Order"
+ }
+ },
+ "400": {
+ "description": "Invalid Order"
+ }
+ }
+ }
+ },
+ "/store/order/{orderId}": {
+ "get": {
+ "tags": [
+ "store"
+ ],
+ "summary": "Find purchase order by ID",
+ "description": "For valid response try integer IDs with value >= 1 and <= 10. Other values will generated exceptions",
+ "operationId": "getOrderById",
+ "produces": [
+ "application/json",
+ "application/xml"
+ ],
+ "parameters": [
+ {
+ "name": "orderId",
+ "in": "path",
+ "description": "ID of pet that needs to be fetched",
+ "required": true,
+ "type": "integer",
+ "maximum": 10.0,
+ "minimum": 1.0,
+ "format": "int64"
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "successful operation",
+ "schema": {
+ "$ref": "#/definitions/Order"
+ }
+ },
+ "400": {
+ "description": "Invalid ID supplied"
+ },
+ "404": {
+ "description": "Order not found"
+ }
+ }
+ },
+ "delete": {
+ "tags": [
+ "store"
+ ],
+ "summary": "Delete purchase order by ID",
+ "description": "For valid response try integer IDs with positive integer value. Negative or non-integer values will generate API errors",
+ "operationId": "deleteOrder",
+ "produces": [
+ "application/json",
+ "application/xml"
+ ],
+ "parameters": [
+ {
+ "name": "orderId",
+ "in": "path",
+ "description": "ID of the order that needs to be deleted",
+ "required": true,
+ "type": "integer",
+ "minimum": 1.0,
+ "format": "int64"
+ }
+ ],
+ "responses": {
+ "400": {
+ "description": "Invalid ID supplied"
+ },
+ "404": {
+ "description": "Order not found"
+ }
+ }
+ }
+ },
+ "/store/inventory": {
+ "get": {
+ "tags": [
+ "store"
+ ],
+ "summary": "Returns pet inventories by status",
+ "description": "Returns a map of status codes to quantities",
+ "operationId": "getInventory",
+ "produces": [
+ "application/json"
+ ],
+ "parameters": [],
+ "responses": {
+ "200": {
+ "description": "successful operation",
+ "schema": {
+ "type": "object",
+ "additionalProperties": {
+ "type": "integer",
+ "format": "int32"
+ }
+ }
+ }
+ },
+ "security": [
+ {
+ "api_key": []
+ }
+ ]
+ }
+ },
+ "/user/createWithArray": {
+ "post": {
+ "tags": [
+ "user"
+ ],
+ "summary": "Creates list of users with given input array",
+ "description": "",
+ "operationId": "createUsersWithArrayInput",
+ "consumes": [
+ "application/json"
+ ],
+ "produces": [
+ "application/json",
+ "application/xml"
+ ],
+ "parameters": [
+ {
+ "in": "body",
+ "name": "body",
+ "description": "List of user object",
+ "required": true,
+ "schema": {
+ "type": "array",
+ "items": {
+ "$ref": "#/definitions/User"
+ }
+ }
+ }
+ ],
+ "responses": {
+ "default": {
+ "description": "successful operation"
+ }
+ }
+ }
+ },
+ "/user/createWithList": {
+ "post": {
+ "tags": [
+ "user"
+ ],
+ "summary": "Creates list of users with given input array",
+ "description": "",
+ "operationId": "createUsersWithListInput",
+ "consumes": [
+ "application/json"
+ ],
+ "produces": [
+ "application/json",
+ "application/xml"
+ ],
+ "parameters": [
+ {
+ "in": "body",
+ "name": "body",
+ "description": "List of user object",
+ "required": true,
+ "schema": {
+ "type": "array",
+ "items": {
+ "$ref": "#/definitions/User"
+ }
+ }
+ }
+ ],
+ "responses": {
+ "default": {
+ "description": "successful operation"
+ }
+ }
+ }
+ },
+ "/user/{username}": {
+ "get": {
+ "tags": [
+ "user"
+ ],
+ "summary": "Get user by user name",
+ "description": "",
+ "operationId": "getUserByName",
+ "produces": [
+ "application/json",
+ "application/xml"
+ ],
+ "parameters": [
+ {
+ "name": "username",
+ "in": "path",
+ "description": "The name that needs to be fetched. Use user1 for testing. ",
+ "required": true,
+ "type": "string"
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "successful operation",
+ "schema": {
+ "$ref": "#/definitions/User"
+ }
+ },
+ "400": {
+ "description": "Invalid username supplied"
+ },
+ "404": {
+ "description": "User not found"
+ }
+ }
+ },
+ "put": {
+ "tags": [
+ "user"
+ ],
+ "summary": "Updated user",
+ "description": "This can only be done by the logged in user.",
+ "operationId": "updateUser",
+ "consumes": [
+ "application/json"
+ ],
+ "produces": [
+ "application/json",
+ "application/xml"
+ ],
+ "parameters": [
+ {
+ "name": "username",
+ "in": "path",
+ "description": "name that need to be updated",
+ "required": true,
+ "type": "string"
+ },
+ {
+ "in": "body",
+ "name": "body",
+ "description": "Updated user object",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/User"
+ }
+ }
+ ],
+ "responses": {
+ "400": {
+ "description": "Invalid user supplied"
+ },
+ "404": {
+ "description": "User not found"
+ }
+ }
+ },
+ "delete": {
+ "tags": [
+ "user"
+ ],
+ "summary": "Delete user",
+ "description": "This can only be done by the logged in user.",
+ "operationId": "deleteUser",
+ "produces": [
+ "application/json",
+ "application/xml"
+ ],
+ "parameters": [
+ {
+ "name": "username",
+ "in": "path",
+ "description": "The name that needs to be deleted",
+ "required": true,
+ "type": "string"
+ }
+ ],
+ "responses": {
+ "400": {
+ "description": "Invalid username supplied"
+ },
+ "404": {
+ "description": "User not found"
+ }
+ }
+ }
+ },
+ "/user/login": {
+ "get": {
+ "tags": [
+ "user"
+ ],
+ "summary": "Logs user into the system",
+ "description": "",
+ "operationId": "loginUser",
+ "produces": [
+ "application/json",
+ "application/xml"
+ ],
+ "parameters": [
+ {
+ "name": "username",
+ "in": "query",
+ "description": "The user name for login",
+ "required": true,
+ "type": "string"
+ },
+ {
+ "name": "password",
+ "in": "query",
+ "description": "The password for login in clear text",
+ "required": true,
+ "type": "string"
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "successful operation",
+ "headers": {
+ "X-Expires-After": {
+ "type": "string",
+ "format": "date-time",
+ "description": "date in UTC when token expires"
+ },
+ "X-Rate-Limit": {
+ "type": "integer",
+ "format": "int32",
+ "description": "calls per hour allowed by the user"
+ }
+ },
+ "schema": {
+ "type": "string"
+ }
+ },
+ "400": {
+ "description": "Invalid username/password supplied"
+ }
+ }
+ }
+ },
+ "/user/logout": {
+ "get": {
+ "tags": [
+ "user"
+ ],
+ "summary": "Logs out current logged in user session",
+ "description": "",
+ "operationId": "logoutUser",
+ "produces": [
+ "application/json",
+ "application/xml"
+ ],
+ "parameters": [],
+ "responses": {
+ "default": {
+ "description": "successful operation"
+ }
+ }
+ }
+ },
+ "/user": {
+ "post": {
+ "tags": [
+ "user"
+ ],
+ "summary": "Create user",
+ "description": "This can only be done by the logged in user.",
+ "operationId": "createUser",
+ "consumes": [
+ "application/json"
+ ],
+ "produces": [
+ "application/json",
+ "application/xml"
+ ],
+ "parameters": [
+ {
+ "in": "body",
+ "name": "body",
+ "description": "Created user object",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/User"
+ }
+ }
+ ],
+ "responses": {
+ "default": {
+ "description": "successful operation"
+ }
+ }
+ }
+ }
+ },
+ "securityDefinitions": {
+ "api_key": {
+ "type": "apiKey",
+ "name": "api_key",
+ "in": "header"
+ },
+ "petstore_auth": {
+ "type": "oauth2",
+ "authorizationUrl": "https://petstore.swagger.io/oauth/authorize",
+ "flow": "implicit",
+ "scopes": {
+ "read:pets": "read your pets",
+ "write:pets": "modify pets in your account"
+ }
+ }
+ },
+ "definitions": {
+ "ApiResponse": {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "type": {
+ "type": "string"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ },
+ "Category": {
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ }
+ },
+ "xml": {
+ "name": "Category"
+ }
+ },
+ "Pet": {
+ "type": "object",
+ "required": [
+ "name",
+ "photoUrls"
+ ],
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "category": {
+ "$ref": "#/definitions/Category"
+ },
+ "name": {
+ "type": "string",
+ "example": "doggie"
+ },
+ "photoUrls": {
+ "type": "array",
+ "xml": {
+ "wrapped": true
+ },
+ "items": {
+ "type": "string",
+ "xml": {
+ "name": "photoUrl"
+ }
+ }
+ },
+ "tags": {
+ "type": "array",
+ "xml": {
+ "wrapped": true
+ },
+ "items": {
+ "xml": {
+ "name": "tag"
+ },
+ "$ref": "#/definitions/Tag"
+ }
+ },
+ "status": {
+ "type": "string",
+ "description": "pet status in the store",
+ "enum": [
+ "available",
+ "pending",
+ "sold"
+ ]
+ }
+ },
+ "xml": {
+ "name": "Pet"
+ }
+ },
+ "Tag": {
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ }
+ },
+ "xml": {
+ "name": "Tag"
+ }
+ },
+ "Order": {
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "petId": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "quantity": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "shipDate": {
+ "type": "string",
+ "format": "date-time"
+ },
+ "status": {
+ "type": "string",
+ "description": "Order Status",
+ "enum": [
+ "placed",
+ "approved",
+ "delivered"
+ ]
+ },
+ "complete": {
+ "type": "boolean"
+ }
+ },
+ "xml": {
+ "name": "Order"
+ }
+ },
+ "User": {
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "username": {
+ "type": "string"
+ },
+ "firstName": {
+ "type": "string"
+ },
+ "lastName": {
+ "type": "string"
+ },
+ "email": {
+ "type": "string"
+ },
+ "password": {
+ "type": "string"
+ },
+ "phone": {
+ "type": "string"
+ },
+ "userStatus": {
+ "type": "integer",
+ "format": "int32",
+ "description": "User Status"
+ }
+ },
+ "xml": {
+ "name": "User"
+ }
+ }
+ },
+ "externalDocs": {
+ "description": "Find out more about Swagger",
+ "url": "http://swagger.io"
+ }
+}
\ No newline at end of file
diff --git a/freemarker-generator-tools/src/test/data/yaml/swagger.yaml b/freemarker-generator-tools/src/test/data/yaml/swagger.yaml
new file mode 100644
index 0000000..5ab693a
--- /dev/null
+++ b/freemarker-generator-tools/src/test/data/yaml/swagger.yaml
@@ -0,0 +1,700 @@
+swagger: "2.0"
+info:
+ description: "This is a sample server Petstore server. You can find out more about Swagger at <a href=\"http://swagger.io\">http://swagger.io</a> or on irc.freenode.net, #swagger. For this sample, you can use the api key \"special-key\" to test the authorization filters"
+ version: 1.0.0
+ title: Swagger Petstore YAML
+ termsOfService: "http://swagger.io/terms/"
+ contact:
+ email: "apiteam@swagger.io"
+ license:
+ name: Apache 2.0
+ url: "http://www.apache.org/licenses/LICENSE-2.0.html"
+basePath: /v2
+tags:
+ - name: pet
+ description: Everything about your Pets
+ externalDocs:
+ description: Find out more
+ url: "http://swagger.io"
+ - name: store
+ description: Operations about user
+ - name: user
+ description: Access to Petstore orders
+ externalDocs:
+ description: Find out more about our store
+ url: "http://swagger.io"
+schemes:
+ - http
+paths:
+ /pet:
+ post:
+ tags:
+ - pet
+ summary: Add a new pet to the store
+ x-swagger-router-controller: SampleController
+ description: ""
+ operationId: addPet
+ consumes:
+ - application/json
+ - application/xml
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - in: body
+ name: body
+ description: Pet object that needs to be added to the store
+ required: false
+ schema:
+ $ref: "#/definitions/Pet"
+ responses:
+ "405":
+ description: Invalid input
+ security:
+ - petstore_auth:
+ - "write:pets"
+ - "read:pets"
+ put:
+ tags:
+ - pet
+ summary: Update an existing pet
+ description: ""
+ operationId: updatePet
+ consumes:
+ - application/json
+ - application/xml
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - in: body
+ name: body
+ description: Pet object that needs to be added to the store
+ required: false
+ schema:
+ $ref: "#/definitions/Pet"
+ responses:
+ "400":
+ description: Invalid ID supplied
+ "404":
+ description: Pet not found
+ "405":
+ description: Validation exception
+ security:
+ - petstore_auth:
+ - "write:pets"
+ - "read:pets"
+ /pet/findByStatus:
+ get:
+ tags:
+ - pet
+ summary: Finds Pets by status
+ description: Multiple status values can be provided with comma separated strings
+ operationId: findPetsByStatus
+ consumes:
+ - application/xml
+ - application/json
+ - multipart/form-data
+ - application/x-www-form-urlencoded
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - name: status
+ in: query
+ description: Status values that need to be considered for filter
+ required: false
+ type: array
+ items:
+ type: string
+ collectionFormat: multi
+ default: available
+ enum:
+ - available
+ - pending
+ - sold
+ responses:
+ "200":
+ description: successful operation
+ schema:
+ type: array
+ items:
+ $ref: "#/definitions/Pet"
+ "400":
+ description: Invalid status value
+ security:
+ - petstore_auth:
+ - "write:pets"
+ - "read:pets"
+ /pet/findByTags:
+ get:
+ tags:
+ - pet
+ summary: Finds Pets by tags
+ description: "Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing."
+ operationId: findPetsByTags
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - name: tags
+ in: query
+ description: Tags to filter by
+ required: false
+ type: array
+ items:
+ type: string
+ collectionFormat: multi
+ responses:
+ "200":
+ description: successful operation
+ schema:
+ type: array
+ items:
+ $ref: "#/definitions/Pet"
+ "400":
+ description: Invalid tag value
+ security:
+ - petstore_auth:
+ - "write:pets"
+ - "read:pets"
+ "/pet/{petId}":
+ get:
+ tags:
+ - pet
+ summary: Find pet by ID
+ description: Returns a single pet
+ operationId: getPetById
+ consumes:
+ - application/x-www-form-urlencoded
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - name: petId
+ in: path
+ description: ID of pet to return
+ required: true
+ type: integer
+ format: int64
+ responses:
+ "200":
+ description: successful operation
+ schema:
+ $ref: "#/definitions/Pet"
+ "400":
+ description: Invalid ID supplied
+ "404":
+ description: Pet not found
+ security:
+ - api_key: []
+ - petstore_auth:
+ - "write:pets"
+ - "read:pets"
+ post:
+ tags:
+ - pet
+ summary: Updates a pet in the store with form data
+ description: ""
+ operationId: updatePetWithForm
+ consumes:
+ - application/x-www-form-urlencoded
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - name: petId
+ in: path
+ description: ID of pet that needs to be updated
+ required: true
+ type: string
+ - name: name
+ in: formData
+ description: Updated name of the pet
+ required: false
+ type: string
+ - name: status
+ in: formData
+ description: Updated status of the pet
+ required: false
+ type: string
+ responses:
+ "405":
+ description: Invalid input
+ security:
+ - petstore_auth:
+ - "write:pets"
+ - "read:pets"
+ delete:
+ tags:
+ - pet
+ summary: Deletes a pet
+ description: ""
+ operationId: deletePet
+ consumes:
+ - multipart/form-data
+ - application/x-www-form-urlencoded
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - name: api_key
+ in: header
+ description: ""
+ required: false
+ type: string
+ - name: petId
+ in: path
+ description: Pet id to delete
+ required: true
+ type: integer
+ format: int64
+ responses:
+ "400":
+ description: Invalid pet value
+ security:
+ - petstore_auth:
+ - "write:pets"
+ - "read:pets"
+ "/pet/{petId}/uploadImage":
+ post:
+ tags:
+ - pet
+ summary: uploads an image
+ x-swagger-router-controller: SampleController
+ description: ""
+ operationId: uploadFile
+ consumes:
+ - multipart/form-data
+ produces:
+ - application/json
+ parameters:
+ - name: petId
+ in: path
+ description: ID of pet to update
+ required: true
+ type: integer
+ format: int64
+ - name: additionalMetadata
+ in: formData
+ description: Additional data to pass to server
+ required: false
+ type: string
+ - name: file
+ in: formData
+ description: file to upload
+ required: false
+ type: file
+ responses:
+ "200":
+ description: successful operation
+ schema:
+ $ref: "#/definitions/ApiResponse"
+ security:
+ - petstore_auth:
+ - "write:pets"
+ - "read:pets"
+ /store/inventory:
+ get:
+ tags:
+ - store
+ summary: Returns pet inventories by status
+ description: Returns a map of status codes to quantities
+ operationId: getInventory
+ produces:
+ - application/json
+ parameters: []
+ responses:
+ "200":
+ description: successful operation
+ schema:
+ type: object
+ additionalProperties:
+ type: integer
+ format: int32
+ security:
+ - api_key: []
+ /store/order:
+ post:
+ tags:
+ - store
+ summary: Place an order for a pet
+ description: ""
+ operationId: placeOrder
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - in: body
+ name: body
+ description: order placed for purchasing the pet
+ required: false
+ schema:
+ $ref: "#/definitions/Order"
+ responses:
+ "200":
+ description: successful operation
+ schema:
+ $ref: "#/definitions/Order"
+ "400":
+ description: Invalid Order
+ "/store/order/{orderId}":
+ get:
+ tags:
+ - store
+ summary: Find purchase order by ID
+ description: "For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions"
+ operationId: getOrderById
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - name: orderId
+ in: path
+ description: ID of pet that needs to be fetched
+ required: true
+ type: string
+ responses:
+ "200":
+ description: successful operation
+ schema:
+ $ref: "#/definitions/Order"
+ "400":
+ description: Invalid ID supplied
+ "404":
+ description: Order not found
+ delete:
+ tags:
+ - store
+ summary: Delete purchase order by ID
+ description: For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
+ operationId: deleteOrder
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - name: orderId
+ in: path
+ description: ID of the order that needs to be deleted
+ required: true
+ type: string
+ responses:
+ "400":
+ description: Invalid ID supplied
+ "404":
+ description: Order not found
+ /user:
+ post:
+ tags:
+ - user
+ summary: Create user
+ description: This can only be done by the logged in user.
+ operationId: createUser
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - in: body
+ name: body
+ description: Created user object
+ required: false
+ schema:
+ $ref: "#/definitions/User"
+ responses:
+ default:
+ description: successful operation
+ /user/createWithArray:
+ post:
+ tags:
+ - user
+ summary: Creates list of users with given input array
+ description: ""
+ operationId: createUsersWithArrayInput
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - in: body
+ name: body
+ description: List of user object
+ required: false
+ schema:
+ type: array
+ items:
+ $ref: "#/definitions/User"
+ responses:
+ default:
+ description: successful operation
+ /user/createWithList:
+ post:
+ tags:
+ - user
+ summary: Creates list of users with given input array
+ description: ""
+ operationId: createUsersWithListInput
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - in: body
+ name: body
+ description: List of user object
+ required: false
+ schema:
+ type: array
+ items:
+ $ref: "#/definitions/User"
+ responses:
+ default:
+ description: successful operation
+ /user/login:
+ get:
+ tags:
+ - user
+ summary: Logs user into the system
+ description: ""
+ operationId: loginUser
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - name: username
+ in: query
+ description: The user name for login
+ required: false
+ type: string
+ - name: password
+ in: query
+ description: The password for login in clear text
+ required: false
+ type: string
+ responses:
+ "200":
+ description: successful operation
+ schema:
+ type: string
+ headers:
+ X-Rate-Limit:
+ type: integer
+ format: int32
+ description: calls per hour allowed by the user
+ X-Expires-After:
+ type: string
+ format: date-time
+ description: date in UTC when toekn expires
+ "400":
+ description: Invalid username/password supplied
+ /user/logout:
+ get:
+ tags:
+ - user
+ summary: Logs out current logged in user session
+ description: ""
+ operationId: logoutUser
+ produces:
+ - application/xml
+ - application/json
+ parameters: []
+ responses:
+ default:
+ description: successful operation
+ "/user/{username}":
+ get:
+ tags:
+ - user
+ summary: Get user by user name
+ description: ""
+ operationId: getUserByName
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - name: username
+ in: path
+ description: "The name that needs to be fetched. Use user1 for testing. "
+ required: true
+ type: string
+ responses:
+ "200":
+ description: successful operation
+ schema:
+ $ref: "#/definitions/User"
+ "400":
+ description: Invalid username supplied
+ "404":
+ description: User not found
+ put:
+ tags:
+ - user
+ summary: Updated user
+ description: This can only be done by the logged in user.
+ operationId: updateUser
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - name: username
+ in: path
+ description: name that need to be deleted
+ required: true
+ type: string
+ - in: body
+ name: body
+ description: Updated user object
+ required: false
+ schema:
+ $ref: "#/definitions/User"
+ responses:
+ "400":
+ description: Invalid user supplied
+ "404":
+ description: User not found
+ delete:
+ tags:
+ - user
+ summary: Delete user
+ description: This can only be done by the logged in user.
+ operationId: deleteUser
+ produces:
+ - application/xml
+ - application/json
+ parameters:
+ - name: username
+ in: path
+ description: The name that needs to be deleted
+ required: true
+ type: string
+ responses:
+ "400":
+ description: Invalid username supplied
+ "404":
+ description: User not found
+securityDefinitions:
+ petstore_auth:
+ type: oauth2
+ authorizationUrl: "http://petstore.swagger.io/api/oauth/dialog"
+ flow: implicit
+ scopes:
+ "write:pets": modify pets in your account
+ "read:pets": read your pets
+ api_key:
+ type: apiKey
+ name: api_key
+ in: header
+definitions:
+ Order:
+ properties:
+ id:
+ type: integer
+ format: int64
+ petId:
+ type: integer
+ format: int64
+ quantity:
+ type: integer
+ format: int32
+ shipDate:
+ type: string
+ format: date-time
+ status:
+ type: string
+ description: Order Status
+ enum:
+ - placed
+ - approved
+ - delivered
+ complete:
+ type: boolean
+ xml:
+ name: Order
+ Category:
+ properties:
+ id:
+ type: integer
+ format: int64
+ name:
+ type: string
+ xml:
+ name: Category
+ User:
+ properties:
+ id:
+ type: integer
+ format: int64
+ username:
+ type: string
+ firstName:
+ type: string
+ lastName:
+ type: string
+ email:
+ type: string
+ password:
+ type: string
+ phone:
+ type: string
+ userStatus:
+ type: integer
+ format: int32
+ description: User Status
+ xml:
+ name: User
+ Tag:
+ properties:
+ id:
+ type: integer
+ format: int64
+ name:
+ type: string
+ xml:
+ name: Tag
+ Pet:
+ required:
+ - name
+ - photoUrls
+ properties:
+ id:
+ type: integer
+ format: int64
+ category:
+ $ref: "#/definitions/Category"
+ name:
+ type: string
+ example: doggie
+ photoUrls:
+ type: array
+ xml:
+ name: photoUrl
+ wrapped: true
+ items:
+ type: string
+ tags:
+ type: array
+ xml:
+ name: tag
+ wrapped: true
+ items:
+ $ref: "#/definitions/Tag"
+ status:
+ type: string
+ description: pet status in the store
+ enum:
+ - available
+ - pending
+ - sold
+ xml:
+ name: Pet
+ ApiResponse:
+ properties:
+ code:
+ type: integer
+ format: int32
+ type:
+ type: string
+ message:
+ type: string
+ xml:
+ name: "##default"
+externalDocs:
+ description: Find out more about Swagger
+ url: "http://swagger.io"
\ No newline at end of file
diff --git a/freemarker-generator-tools/src/test/java/org/apache/freemarker/generator/tools/gson/GsonToolTest.java b/freemarker-generator-tools/src/test/java/org/apache/freemarker/generator/tools/gson/GsonToolTest.java
new file mode 100644
index 0000000..7a5f5cf
--- /dev/null
+++ b/freemarker-generator-tools/src/test/java/org/apache/freemarker/generator/tools/gson/GsonToolTest.java
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.freemarker.generator.tools.gson;
+
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static junit.framework.TestCase.assertEquals;
+import static org.apache.commons.io.FileUtils.readFileToString;
+
+public class GsonToolTest {
+
+ private static final String JSON_OBJECT_STRING = "{\n" +
+ " \"id\": 110.0,\n" +
+ " \"language\": \"Python\",\n" +
+ " \"price\": 1900.0\n" +
+ "}";
+
+ private static final String JSON_ARRAY_STRING = "{\n" +
+ " \"eBooks\": [\n" +
+ " {\n" +
+ " \"language\": \"Pascal\",\n" +
+ " \"edition\": \"third\"\n" +
+ " },\n" +
+ " {\n" +
+ " \"language\": \"Python\",\n" +
+ " \"edition\": \"four\"\n" +
+ " },\n" +
+ " {\n" +
+ " \"language\": \"SQL\",\n" +
+ " \"edition\": \"second\"\n" +
+ " }\n" +
+ " ]\n" +
+ "}";
+
+ private static final String JSON_WITH_COMMENTS = "{\n" +
+ " // Single line comment\n" +
+ " \"fruit\": \"Apple\",\n" +
+ " \"size\": \"Large\",\n" +
+ " \"color\": \"Red\"\n" +
+ "}";
+
+ private final GsonTool gsonTool = gsonTool();
+
+ @Test
+ public void shouldParseJsonObject() {
+ final Map<String, Object> map = parse(JSON_OBJECT_STRING);
+
+ assertEquals(3, map.size());
+ assertEquals("110.0", map.get("id").toString());
+ assertEquals("Python", map.get("language"));
+ assertEquals("1900.0", map.get("price").toString());
+ }
+
+ @Test
+ public void shouldParseJsonArray() {
+ final Map<String, Object> map = parse(JSON_ARRAY_STRING);
+
+ assertEquals(1, map.size());
+ assertEquals(3, ((List) map.get("eBooks")).size());
+
+ return;
+ }
+
+ @Test
+ public void shouldParseJsonWithComemnts() {
+ final Map<String, Object> map = parse(JSON_WITH_COMMENTS);
+
+ assertEquals("Apple", map.get("fruit"));
+ }
+
+ @Test
+ public void shouldConvertToJson() {
+ assertEquals(JSON_OBJECT_STRING, gsonTool.toJson(parse(JSON_OBJECT_STRING)));
+ assertEquals(JSON_ARRAY_STRING, gsonTool.toJson(parse(JSON_ARRAY_STRING)));
+ }
+
+ @Test
+ public void shouldParseComplexJson() throws IOException {
+ final String json = readFileToString(new File("./src/test/data/json/swagger.json"), UTF_8);
+ final Map<String, Object> map = parse(json);
+
+ assertEquals("petstore.swagger.io", map.get("host"));
+ assertEquals(json, gsonTool.toJson(parse(json)));
+ }
+
+ private Map<String, Object> parse(String json) {
+ return gsonTool.parse(json);
+ }
+
+ private GsonTool gsonTool() {
+ return new GsonTool();
+ }
+}
diff --git a/freemarker-generator-tools/src/test/java/org/apache/freemarker/generator/tools/jsonpath/JsonPathToolTest.java b/freemarker-generator-tools/src/test/java/org/apache/freemarker/generator/tools/jsonpath/JsonPathToolTest.java
index aaed1b5..dd21f67 100644
--- a/freemarker-generator-tools/src/test/java/org/apache/freemarker/generator/tools/jsonpath/JsonPathToolTest.java
+++ b/freemarker-generator-tools/src/test/java/org/apache/freemarker/generator/tools/jsonpath/JsonPathToolTest.java
@@ -27,7 +27,7 @@ public class JsonPathToolTest {
private static final String JSON_OBJECT_STRING = "{\n" +
"\"id\": 110,\n" +
"\"language\": \"Python\",\n" +
- "\"price\": 1900,\n" +
+ "\"price\": 1900\n" +
"}";
private static final String JSON_ARRAY_STRING = "{\n" +
diff --git a/freemarker-generator-tools/src/test/java/org/apache/freemarker/generator/tools/snakeyaml/SnakeYamlToolTest.java b/freemarker-generator-tools/src/test/java/org/apache/freemarker/generator/tools/snakeyaml/SnakeYamlToolTest.java
index f379995..6850b25 100644
--- a/freemarker-generator-tools/src/test/java/org/apache/freemarker/generator/tools/snakeyaml/SnakeYamlToolTest.java
+++ b/freemarker-generator-tools/src/test/java/org/apache/freemarker/generator/tools/snakeyaml/SnakeYamlToolTest.java
@@ -21,10 +21,15 @@ import org.apache.freemarker.generator.base.datasource.DataSource;
import org.apache.freemarker.generator.base.datasource.DataSourceFactory;
import org.junit.Test;
+import java.io.File;
+import java.io.IOException;
import java.util.List;
import java.util.Map;
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static junit.framework.Assert.assertTrue;
import static junit.framework.TestCase.assertEquals;
+import static org.apache.commons.io.FileUtils.readFileToString;
public class SnakeYamlToolTest {
@@ -54,6 +59,22 @@ public class SnakeYamlToolTest {
assertEquals(3, ((List<?>) map.get("docker")).size());
}
+ @Test
+ public void shallConvertToYamlString() {
+ final Map<String, Object> map = snakeYamlTool().parse(ANY_YAML_STRING);
+
+ assertEquals(114, snakeYamlTool().toYaml(map).length());
+ }
+
+ @Test
+ public void shouldParseComplexYaml() throws IOException {
+ final String yaml = readFileToString(new File("./src/test/data/yaml/swagger.yaml"), UTF_8);
+ final Map<String, Object> map = snakeYamlTool().parse(yaml);
+
+ assertEquals("2.0", map.get("swagger"));
+ assertEquals(16956, snakeYamlTool().toYaml(map).length());
+ }
+
private SnakeYamlTool snakeYamlTool() {
return new SnakeYamlTool();
}