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

Package config with dynamic package name processing + more #14

Closed
wants to merge 38 commits into from
Closed
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
c8f6731
feat: dynamic package name via post-processing
gkocak-scottlogic Mar 1, 2023
fc8b016
fix: apiTemplates change
gkocak-scottlogic Mar 1, 2023
2b8839b
fix: return body string
gkocak-scottlogic Mar 1, 2023
c70fd5a
feat: model split hack
gkocak-scottlogic Mar 1, 2023
0fae95c
chore: run prettier
gkocak-scottlogic Mar 1, 2023
065a600
fix: shelljs dev dep
gkocak-scottlogic Mar 2, 2023
9ba381f
fix: change model fix from class regex to java parser
gkocak-scottlogic Mar 2, 2023
b539222
chore: error propagation in model fix
gkocak-scottlogic Mar 2, 2023
4e1e0cf
fix: make classes public on model split
gkocak-scottlogic Mar 2, 2023
aad08db
chore: split move file logic
gkocak-scottlogic Mar 9, 2023
5e3b702
chore: change default package name to com.openapi.forge
gkocak-scottlogic Mar 9, 2023
f1b4c09
fix: remove model split logic from branch
gkocak-scottlogic Mar 9, 2023
36cba53
fix: remove java-parser from deps
gkocak-scottlogic Mar 9, 2023
443f636
chore: add description to post process move
gkocak-scottlogic Mar 14, 2023
cd3775e
fix: cookie headers in requests
ms14981 Mar 7, 2023
94aed9f
feat: change adding headers to requests.
gkocak-scottlogic Mar 9, 2023
f288e24
fix: cookie headers when more than one cookie
ms14981 Mar 8, 2023
85e46fa
fix: cookie headers when more than one cookie
ms14981 Mar 8, 2023
bd9f349
feat: add httpResponse wrapping
gkocak-scottlogic Mar 9, 2023
4d3f6de
chore: simplify response object creation in handlebar
gkocak-scottlogic Mar 9, 2023
8436a0a
fix: safeTypeConvert void wrapping
gkocak-scottlogic Mar 9, 2023
d20674e
chore: tidy http response
gkocak-scottlogic Mar 9, 2023
effac7d
chore: change var names in constructor and order
gkocak-scottlogic Mar 10, 2023
eb48380
feat: change adding headers to requests.
gkocak-scottlogic Mar 9, 2023
c0ef017
feat: add httpResponse wrapping
gkocak-scottlogic Mar 9, 2023
eebe839
chore: simplify response object creation in handlebar
gkocak-scottlogic Mar 9, 2023
496f245
fix: safeTypeConvert void wrapping
gkocak-scottlogic Mar 9, 2023
c13a7a3
feat: add httpResponse wrapping
gkocak-scottlogic Mar 9, 2023
8a3b7c2
fix: safeTypeConvert void wrapping
gkocak-scottlogic Mar 9, 2023
343d112
feat: add httpResponse wrapping
gkocak-scottlogic Mar 9, 2023
c27e399
fix: safeTypeConvert void wrapping
gkocak-scottlogic Mar 9, 2023
d218f18
feat: add httpResponse wrapping
gkocak-scottlogic Mar 9, 2023
4319447
fix: safeTypeConvert void wrapping
gkocak-scottlogic Mar 9, 2023
f84de8c
feat: change adding headers to requests.
gkocak-scottlogic Mar 9, 2023
82ececb
fix: cookie headers when more than one cookie
ms14981 Mar 8, 2023
e370518
feat: add httpResponse wrapping
gkocak-scottlogic Mar 9, 2023
bd1ca3f
chore: simplify response object creation in handlebar
gkocak-scottlogic Mar 9, 2023
849e73f
feat: change adding headers to requests.
gkocak-scottlogic Mar 9, 2023
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;
14 changes: 10 additions & 4 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 @@ -24,15 +24,21 @@ const createReturnStatement = (responseSchema) => {
returnStatement = `${responseType}.Parse(responseBody)`;
break;
case "String":
returnStatement = "responseBody";
returnStatement = "responseBodyString";
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);
}

return new Handlebars.SafeString(mapperStatements + `return ${returnStatement};`);
return new Handlebars.SafeString(
mapperStatements + `return ${returnStatement};`
);
};

module.exports = createReturnStatement;
18 changes: 9 additions & 9 deletions helpers/queryType.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const queryType = (key) => {
if (key == 'delete') {
return '.delete()';
} else if (key == "put") {
return '.put(RequestBody.create("", null))'
} else if (key == "post") {
return '.post(RequestBody.create("", null))'
} else {
return '';
}
if (key == "delete") {
return ".delete()";
} else if (key == "put") {
return '.put(RequestBody.create("", null))';
} else if (key == "post") {
return '.post(RequestBody.create("", null))';
} else {
return "";
}
};

module.exports = queryType;
18 changes: 9 additions & 9 deletions helpers/queryTypeWithBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// This is why this function exists seperately.

const queryTypeWithBody = (key) => {
if (key == "delete") {
return ".delete()";
} else if (key == "put") {
return ".put(httpBody)"
} else if (key == "post") {
return ".post(httpBody)"
} else {
return "";
}
if (key == "delete") {
return ".delete()";
} else if (key == "put") {
return ".put(httpBody)";
} else if (key == "post") {
return ".post(httpBody)";
} else {
return "";
}
};

module.exports = queryTypeWithBody;
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) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were we going to put formatting under the banner of post-processing? This would have the benefit of the main forge only needing to know about post-processing in general, not formatting. Although this would need changes to the other generators, I don't think those changes will be very significant.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that formatting is actually a post-processing stage and it would make sense every language to do their formating in this stage. And I also agree that it's not a big change either. Maybe we can discuss this change over the demo meeting.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realise that we didn't end up chatting about this. Did you want to message Colin about this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, we forgot to chat about this. And since the post-processing is already merged at the main repo alongside with formatting, we can keep the logic as is in this PR at least. I can open a ticket/issue to track the merge of formatting into post-processing.

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) => {
gkocak-scottlogic marked this conversation as resolved.
Show resolved Hide resolved
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 java.util.List;

Expand Down