Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/post2 #22

Merged
merged 15 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
## OpenAPI Forge - C#
## OpenAPI Forge - Java

This repository is the C# generator for the [OpenAPI Forge](https://github.com/ScottLogic/openapi-forge), see that repository for usage instructions:

https://github.com/ScottLogic/openapi-forge

## Configuration

OpenAPI Forge is opinionated in its approach, we don't provide a vast range of configuration options, just the essentials! You can list the generator-specific configuration options by running the `generate-options` command as follows:

```
% openapi-forge generator-options openapi-forge-java
This generator has a number of additional options which can be supplied when executing the 'forge' command.

Options:
--generator.package <value> The package for the generated classes.
```

## Development

### Running
Expand Down
6 changes: 6 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"package": {
"default": "com.openapi.forge",
"description": "The package for the generated classes."
}
}
27 changes: 16 additions & 11 deletions formatter.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
const prettier_cli = require("prettier/cli.js");

const logLevels = [
/* quiet */
"silent",
/* standard */
"warn",
/* verbose */
"debug",
];

module.exports = (folder, logLevel) => {
return prettier_cli.run(["--write", folder, "--loglevel", logLevels[logLevel]]);
};
/* quiet */
"silent",
/* standard */
"warn",
/* verbose */
"debug",
];

module.exports = (folder, logLevel) => {
return prettier_cli.run([
"--write",
folder,
"--loglevel",
logLevels[logLevel],
]);
};
32 changes: 16 additions & 16 deletions helpers/complexReturnType.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
const typeConvert = require("./typeConvert");

const complexReturnType = (responseSchema) => {
const responseType = typeConvert(responseSchema);
switch (responseType) {
case "boolean":
case "int":
case "long":
case "float":
case "double":
case "String":
return false;
case "Date":
default:
return true;
}
}
const complexReturnType = (responseSchema) => {
const responseType = typeConvert(responseSchema);
switch (responseType) {
case "boolean":
case "int":
case "long":
case "float":
case "double":
case "String":
return false;
case "Date":
default:
return true;
}
};

module.exports = complexReturnType;
module.exports = complexReturnType;
8 changes: 6 additions & 2 deletions helpers/createReturnStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const mapperListHandle = (responseType) => {
} else {
return `${responseType}.class`;
}
}
};

const createReturnStatement = (responseSchema) => {
const responseType = typeConvert(responseSchema);
Expand All @@ -28,7 +28,11 @@ const createReturnStatement = (responseSchema) => {
break;
default:
mapperStatements = `ObjectMapper deserMapper = new ObjectMapper();\n`;
mapperStatements += `${responseType} ${toParamName(responseType)} = deserMapper.readValue(responseBodyString, ${mapperListHandle(responseType)});\n`
mapperStatements += `${responseType} ${toParamName(
responseType
)} = deserMapper.readValue(responseBodyString, ${mapperListHandle(
responseType
)});\n`;
returnStatement = toParamName(responseType);
}

Expand Down
11 changes: 5 additions & 6 deletions helpers/toParamNameCapital.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const toParamNameCapital = (name) => {
name = name.replace(/[^a-z0-9_]/gi, "");
return name.charAt(0).toUpperCase() + name.substr(1);
};

module.exports = toParamNameCapital;

name = name.replace(/[^a-z0-9_]/gi, "");
return name.charAt(0).toUpperCase() + name.substr(1);
};

module.exports = toParamNameCapital;
67 changes: 66 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"version": "0.1.0",
"description": "OpenAPI-Forge Java template",
"apiTemplates": [
"src/main/java/com/example/springboot/ApiClient.java.handlebars",
"src/main/java/com/example/springboot/IApiClient.java.handlebars"
"ApiClient.java.handlebars",
"IApiClient.java.handlebars"
],
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -44,7 +44,8 @@
"path": "^0.12.7",
"prettier": "^2.7.1",
"prettier-plugin-java": "^2.0.0",
"semantic-release": "^19.0.5"
"semantic-release": "^19.0.5",
"shelljs": "^0.8.5"
},
"engines": {
"node": ">=16.0.0"
Expand Down
5 changes: 5 additions & 0 deletions postProcess.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const move_files = require("./postProcessMoveFiles");

module.exports = (folder, _, options) => {
move_files(folder, options);
};
15 changes: 15 additions & 0 deletions postProcessMoveFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const shell = require("shelljs");

// To match the java package/folder conventions, the java files that are generated from handlebars templates
// should be moved to src/main/java/<package_folder>.
// The package folder name comes from the generator options and that can given as a command line argument
// by --generator.package

module.exports = (folder, options) => {
var package_name = options["generator.package"];
var package_folder = package_name.replaceAll(".", "/");
java_path = "src/main/java/" + package_folder;
shell.cd(folder);
shell.mkdir("-p", java_path);
shell.mv("-f", "*.java", java_path);
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.springboot;
package {{_options.[generator.package]}};

import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.springboot;
package {{_options.[generator.package]}};

import java.util.Date;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.springboot;
package {{_options.[generator.package]}};

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.springboot;
package {{_options.[generator.package]}};

import java.util.Arrays;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.springboot;
package {{_options.[generator.package]}};

import okhttp3.Headers;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.springboot;
package {{_options.[generator.package]}};

import java.util.List;

Expand Down