From 900da44d246e68e9a404125e51417fe02c839c87 Mon Sep 17 00:00:00 2001 From: Ivan Garcia Sainz-Aja Date: Thu, 11 May 2023 17:49:11 +0200 Subject: [PATCH 1/2] Adds RuleFactory logger to avoid nullpointer in upstream library. fixes #1 --- .../AsyncApiJsonSchema2PojoGenerator.java | 70 ++++++++++++++++--- .../AsyncApiJsonSchema2PojoGeneratorTest.java | 13 ++++ .../src/test/resources/asyncapi.yml | 32 +++++++++ 3 files changed, 106 insertions(+), 9 deletions(-) create mode 100644 plugins/asyncapi-jsonschema2pojo/src/test/resources/asyncapi.yml diff --git a/plugins/asyncapi-jsonschema2pojo/src/main/java/io/zenwave360/sdk/plugins/AsyncApiJsonSchema2PojoGenerator.java b/plugins/asyncapi-jsonschema2pojo/src/main/java/io/zenwave360/sdk/plugins/AsyncApiJsonSchema2PojoGenerator.java index c63745a6..90a817dd 100644 --- a/plugins/asyncapi-jsonschema2pojo/src/main/java/io/zenwave360/sdk/plugins/AsyncApiJsonSchema2PojoGenerator.java +++ b/plugins/asyncapi-jsonschema2pojo/src/main/java/io/zenwave360/sdk/plugins/AsyncApiJsonSchema2PojoGenerator.java @@ -17,13 +17,8 @@ import org.apache.commons.lang3.RegExUtils; import org.apache.commons.lang3.StringUtils; -import org.jsonschema2pojo.FileCodeWriterWithEncoding; -import org.jsonschema2pojo.Jackson2Annotator; -import org.jsonschema2pojo.Jsonschema2Pojo; -import org.jsonschema2pojo.SchemaGenerator; -import org.jsonschema2pojo.SchemaMapper; -import org.jsonschema2pojo.SchemaStore; -import org.jsonschema2pojo.SourceType; +import org.jsonschema2pojo.*; +import org.jsonschema2pojo.exception.ClassAlreadyExistsException; import org.jsonschema2pojo.rules.RuleFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -148,13 +143,15 @@ public void generateFromJsonSchemaFile(JsonSchema2PojoConfiguration config, URL SourceType sourceType = url.getFile().endsWith(".yml") || url.getFile().endsWith(".yaml") ? YAMLSCHEMA : JSONSCHEMA; config.setSourceType(sourceType); } - Jsonschema2Pojo.generate(config, null); + Jsonschema2Pojo.generate(config, ruleLogger); } public void generateFromNativeFormat(JsonSchema2PojoConfiguration config, Map payload, String packageName, String className) throws IOException { var json = this.convertToJson(payload, packageName); - SchemaMapper mapper = new SchemaMapper(new RuleFactory(config, new Jackson2Annotator(config), new SchemaStore()), new SchemaGenerator()); + var ruleFactory = new RuleFactory(config, new Jackson2Annotator(config), new SchemaStore()); + ruleFactory.setLogger(ruleLogger); + SchemaMapper mapper = new SchemaMapper(ruleFactory, new SchemaGenerator()); var sourcesWriter = new FileCodeWriterWithEncoding(targetSourceFolder, config.getOutputEncoding()); var resourcesWriter = new FileCodeWriterWithEncoding(targetSourceFolder, config.getOutputEncoding()); var codeModel = new JCodeModel(); @@ -174,4 +171,59 @@ protected String convertToJson(final Map payload, final String p return this.jsonMapper.writeValueAsString(jsonObject); } + private RuleLogger ruleLogger = new AbstractRuleLogger() { + @Override + protected void doDebug(String msg) { + log.debug(msg); + } + + @Override + protected void doError(String msg, Throwable e) { + if (e instanceof ClassAlreadyExistsException) { + log.debug("Class already exists: {}", ((ClassAlreadyExistsException)e).getExistingClass()); + } else { + log.debug(msg, e); + } + } + + @Override + protected void doInfo(String msg) { + log.debug(msg); + } + + @Override + protected void doTrace(String msg) { + log.trace(msg); + } + + @Override + protected void doWarn(String msg, Throwable e) { + + } + + @Override + public boolean isDebugEnabled() { + return false; + } + + @Override + public boolean isErrorEnabled() { + return false; + } + + @Override + public boolean isInfoEnabled() { + return false; + } + + @Override + public boolean isTraceEnabled() { + return false; + } + + @Override + public boolean isWarnEnabled() { + return false; + } + }; } diff --git a/plugins/asyncapi-jsonschema2pojo/src/test/java/io/zenwave360/sdk/plugins/AsyncApiJsonSchema2PojoGeneratorTest.java b/plugins/asyncapi-jsonschema2pojo/src/test/java/io/zenwave360/sdk/plugins/AsyncApiJsonSchema2PojoGeneratorTest.java index 9c850d49..f26f996a 100644 --- a/plugins/asyncapi-jsonschema2pojo/src/test/java/io/zenwave360/sdk/plugins/AsyncApiJsonSchema2PojoGeneratorTest.java +++ b/plugins/asyncapi-jsonschema2pojo/src/test/java/io/zenwave360/sdk/plugins/AsyncApiJsonSchema2PojoGeneratorTest.java @@ -18,6 +18,19 @@ public void setup() throws IOException { FileUtils.deleteDirectory(new File("target/zenwave630/out")); } + @Test + public void test_generator_for_asyncapi_repeated_enum() throws Exception { + Plugin plugin = new AsyncApiJsonSchema2PojoPlugin() + .withSpecFile("classpath:asyncapi.yml") + .withTargetFolder("target/zenwave630") + .withOption("modelPackage", "io.example.integration.test.with_schemas.model"); + + new MainGenerator().generate(plugin); + + Assertions.assertTrue(new File("target/zenwave630/src/main/java/io/example/integration/test/with_schemas/model/Email.java").exists()); + Assertions.assertTrue(new File("target/zenwave630/src/main/java/io/example/integration/test/with_schemas/model/UserSignedUp.java").exists()); + } + @Test public void test_generator_for_asyncapi_schemas() throws Exception { Plugin plugin = new AsyncApiJsonSchema2PojoPlugin() diff --git a/plugins/asyncapi-jsonschema2pojo/src/test/resources/asyncapi.yml b/plugins/asyncapi-jsonschema2pojo/src/test/resources/asyncapi.yml new file mode 100644 index 00000000..cec76fe8 --- /dev/null +++ b/plugins/asyncapi-jsonschema2pojo/src/test/resources/asyncapi.yml @@ -0,0 +1,32 @@ +asyncapi: '2.6.0' +info: + title: Account Service + version: 1.0.0 + description: This service is in charge of processing user signups +channels: + user/signedup: + subscribe: + operationId: user-signedup + message: + $ref: '#/components/messages/UserSignedUp' +components: + messages: + UserSignedUp: + payload: + type: object + properties: + displayName: + type: string + description: Name of the user + email: + $ref: "#/components/schemas/Email" + description: Email of the user + backupEmail: + description: something else + $ref: "#/components/schemas/Email" + schemas: + Email: + type: string + enum: + - Google + - Yahoo From 8ffafd3c915be1567f1b64947dd00971cca9fae4 Mon Sep 17 00:00:00 2001 From: Ivan Garcia Sainz-Aja Date: Thu, 11 May 2023 17:50:13 +0200 Subject: [PATCH 2/2] GitHub action: checkout with token --- .github/workflows/prepare-maven-release.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/prepare-maven-release.yml b/.github/workflows/prepare-maven-release.yml index 5dce6f27..25b1932a 100644 --- a/.github/workflows/prepare-maven-release.yml +++ b/.github/workflows/prepare-maven-release.yml @@ -14,8 +14,8 @@ jobs: steps: - uses: actions/checkout@v2 - with: - token: ${{ github.token }} +# with: +# token: ${{ github.token }} - name: Set up Java for publishing to Maven Central Repository uses: actions/setup-java@v2 with: