From 3838a7c34cec8d62de0a41f6528132249b28284c Mon Sep 17 00:00:00 2001 From: Justin Black Date: Tue, 23 Jan 2024 14:28:49 -0800 Subject: [PATCH 1/6] Makes servers publics, adds imports in apiconfiguration --- .../petstore/java/.openapi-generator/FILES | 1 + .../configurations/ApiConfiguration.java | 12 +++++++++ .../paths/foo/get/servers/FooGetServer0.java | 2 +- .../paths/foo/get/servers/FooGetServer1.java | 2 +- .../servers/PetfindbystatusServer0.java | 2 +- .../servers/PetfindbystatusServer1.java | 2 +- .../client/servers/Server0.java | 2 +- .../client/servers/Server1.java | 2 +- .../client/servers/Server2.java | 2 +- .../codegen/generators/DefaultGenerator.java | 2 +- .../generators/JavaClientGenerator.java | 4 +++ .../configurations/ApiConfiguration.hbs | 26 +++++++++++++++++++ .../main/java/packagename/servers/Server.hbs | 4 +-- 13 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/configurations/ApiConfiguration.java create mode 100644 src/main/resources/java/src/main/java/packagename/configurations/ApiConfiguration.hbs diff --git a/samples/client/petstore/java/.openapi-generator/FILES b/samples/client/petstore/java/.openapi-generator/FILES index b92667a4474..5854479fe40 100644 --- a/samples/client/petstore/java/.openapi-generator/FILES +++ b/samples/client/petstore/java/.openapi-generator/FILES @@ -465,6 +465,7 @@ src/main/java/org/openapijsonschematools/client/components/schemas/UUIDString.ja src/main/java/org/openapijsonschematools/client/components/schemas/User.java src/main/java/org/openapijsonschematools/client/components/schemas/Whale.java src/main/java/org/openapijsonschematools/client/components/schemas/Zebra.java +src/main/java/org/openapijsonschematools/client/configurations/ApiConfiguration.java src/main/java/org/openapijsonschematools/client/configurations/JsonSchemaKeywordFlags.java src/main/java/org/openapijsonschematools/client/configurations/SchemaConfiguration.java src/main/java/org/openapijsonschematools/client/exceptions/BaseException.java diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/configurations/ApiConfiguration.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/configurations/ApiConfiguration.java new file mode 100644 index 00000000000..d1d7dbdda67 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/configurations/ApiConfiguration.java @@ -0,0 +1,12 @@ +package org.openapijsonschematools.client.configurations; + +import org.openapijsonschematools.client.servers.Server0; +import org.openapijsonschematools.client.servers.Server1; +import org.openapijsonschematools.client.servers.Server2; +import org.openapijsonschematools.client.paths.foo.get.servers.FooGetServer0; +import org.openapijsonschematools.client.paths.foo.get.servers.FooGetServer1; +import org.openapijsonschematools.client.paths.petfindbystatus.servers.PetfindbystatusServer0; +import org.openapijsonschematools.client.paths.petfindbystatus.servers.PetfindbystatusServer1; + +public record ApiConfiguration() { +} \ No newline at end of file diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/FooGetServer0.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/FooGetServer0.java index 515598c4bc9..1ea6b69b607 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/FooGetServer0.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/FooGetServer0.java @@ -2,7 +2,7 @@ import org.openapijsonschematools.client.servers.ServerWithoutVariables; -class FooGetServer0 extends ServerWithoutVariables { +public class FooGetServer0 extends ServerWithoutVariables { public FooGetServer0() { super("https://path-server-test.petstore.local/v2"); } diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/FooGetServer1.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/FooGetServer1.java index 3f43ac01d6e..4c00686226e 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/FooGetServer1.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/FooGetServer1.java @@ -8,7 +8,7 @@ import java.util.AbstractMap; -class FooGetServer1 extends ServerWithVariables { +public class FooGetServer1 extends ServerWithVariables { public FooGetServer1() { super( diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/servers/PetfindbystatusServer0.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/servers/PetfindbystatusServer0.java index 52486b0960f..d548b1da5db 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/servers/PetfindbystatusServer0.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/servers/PetfindbystatusServer0.java @@ -2,7 +2,7 @@ import org.openapijsonschematools.client.servers.ServerWithoutVariables; -class PetfindbystatusServer0 extends ServerWithoutVariables { +public class PetfindbystatusServer0 extends ServerWithoutVariables { public PetfindbystatusServer0() { super("https://path-server-test.petstore.local/v2"); } diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/servers/PetfindbystatusServer1.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/servers/PetfindbystatusServer1.java index 01449e311af..365a0597ded 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/servers/PetfindbystatusServer1.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/servers/PetfindbystatusServer1.java @@ -8,7 +8,7 @@ import java.util.AbstractMap; -class PetfindbystatusServer1 extends ServerWithVariables { +public class PetfindbystatusServer1 extends ServerWithVariables { public PetfindbystatusServer1() { super( diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server0.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server0.java index d4d772a235f..6327eea827e 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server0.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server0.java @@ -7,7 +7,7 @@ import java.util.AbstractMap; -class Server0 extends ServerWithVariables { +public class Server0 extends ServerWithVariables { /* petstore server */ diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server1.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server1.java index 40d76ee508d..12d78391b64 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server1.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server1.java @@ -7,7 +7,7 @@ import java.util.AbstractMap; -class Server1 extends ServerWithVariables { +public class Server1 extends ServerWithVariables { /* The local server */ diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server2.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server2.java index cd8cf496918..9d7cb0e0bfb 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server2.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server2.java @@ -2,7 +2,7 @@ import org.openapijsonschematools.client.servers.ServerWithoutVariables; -class Server2 extends ServerWithoutVariables { +public class Server2 extends ServerWithoutVariables { public Server2() { super("https://localhost:8080"); } diff --git a/src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java b/src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java index 652a4a068d4..537c4fda31f 100644 --- a/src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java +++ b/src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java @@ -4806,7 +4806,7 @@ public CodegenKey getKey(String key, String keyType, String sourceJsonPath) { case "paths": usedKey = escapeUnsafeCharacters(key); isValid = isValid(usedKey); - snakeCaseName = toModelFilename(usedKey, sourceJsonPath); + snakeCaseName = toPathFilename(usedKey, sourceJsonPath); pascalCaseName = camelize(toPathFilename(usedKey, null)); break; case "misc": diff --git a/src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java b/src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java index 4a526c26d05..d51c134fd57 100644 --- a/src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java +++ b/src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java @@ -682,6 +682,10 @@ public void processOpts() { "src/main/java/packagename/configurations/SchemaConfiguration.hbs", packagePath() + File.separatorChar + "configurations", "SchemaConfiguration.java")); + supportingFiles.add(new SupportingFile( + "src/main/java/packagename/configurations/ApiConfiguration.hbs", + packagePath() + File.separatorChar + "configurations", + "ApiConfiguration.java")); HashMap schemaTemplates = new HashMap<>(); schemaTemplates.put("src/main/java/packagename/components/schemas/Schema.hbs", ".java"); diff --git a/src/main/resources/java/src/main/java/packagename/configurations/ApiConfiguration.hbs b/src/main/resources/java/src/main/java/packagename/configurations/ApiConfiguration.hbs new file mode 100644 index 00000000000..2b503dac9e2 --- /dev/null +++ b/src/main/resources/java/src/main/java/packagename/configurations/ApiConfiguration.hbs @@ -0,0 +1,26 @@ +package {{{packageName}}}.configurations; + +{{#if hasServers}} + {{#each servers}} +import {{packageName}}.servers.{{jsonPathPiece.pascalCase}}; + {{/each}} + {{#each paths}} + {{#if servers}} + {{#each servers}} +import {{packageName}}.paths.{{../@key.snakeCase}}.servers.{{jsonPathPiece.pascalCase}}; + {{/each}} + {{/if}} + {{#if operations}} + {{#each operations}} + {{#if servers}} + {{#each servers}} +import {{packageName}}.paths.{{../../@key.snakeCase}}.{{../@key.original}}.servers.{{jsonPathPiece.pascalCase}}; + {{/each}} + {{/if}} + {{/each}} + {{/if}} + {{/each}} +{{/if}} + +public record ApiConfiguration() { +} \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/servers/Server.hbs b/src/main/resources/java/src/main/java/packagename/servers/Server.hbs index 91006af68e1..931bccba6b9 100644 --- a/src/main/resources/java/src/main/java/packagename/servers/Server.hbs +++ b/src/main/resources/java/src/main/java/packagename/servers/Server.hbs @@ -12,7 +12,7 @@ import {{{packageName}}}.{{subpackage}}.{{containerJsonPathPiece.pascalCase}}; import java.util.AbstractMap; -class {{../jsonPathPiece.pascalCase}} extends ServerWithVariables<{{containerJsonPathPiece.pascalCase}}.{{mapOutputJsonPathPiece.pascalCase}}> { +public class {{../jsonPathPiece.pascalCase}} extends ServerWithVariables<{{containerJsonPathPiece.pascalCase}}.{{mapOutputJsonPathPiece.pascalCase}}> { {{#if ../description}} /* {{../description.original}} @@ -39,7 +39,7 @@ class {{../jsonPathPiece.pascalCase}} extends ServerWithVariables<{{containerJso {{else}} import {{{packageName}}}.servers.ServerWithoutVariables; -class {{jsonPathPiece.pascalCase}} extends ServerWithoutVariables { +public class {{jsonPathPiece.pascalCase}} extends ServerWithoutVariables { {{#if ../description}} /* {{../description.original}} From 5afc752facb9dbaecba457e20ee0a4ecfd419478 Mon Sep 17 00:00:00 2001 From: Justin Black Date: Tue, 23 Jan 2024 16:31:02 -0800 Subject: [PATCH 2/6] Adds Servers files --- .../petstore/java/.openapi-generator/FILES | 3 + .../client/Servers.java | 13 + .../schema.java | 459 ++++++ .../schema.java | 1353 +++++++++++++++++ .../content/application~1json/schema.java | 19 + .../content/application~1json/schema.java | 19 + .../content/application~1json/schema.java | 19 + .../content/application~1json/schema.java | 161 ++ .../content/application~1json/schema.java | 280 ++++ .../content/multipart~1form-data/schema.java | 460 ++++++ .../schema.java | 252 +++ .../application~1json-patch+json/schema.java | 19 + .../schema.java | 19 + .../content/application~1json/schema.java | 178 +++ .../content/multipart~1form-data/schema.java | 178 +++ .../content/application~1json/schema.java | 19 + .../application~1x-pem-file/schema.java | 19 + .../content/multipart~1form-data/schema.java | 229 +++ .../content/application~1json/schema.java | 19 + .../content/application~1json/schema.java | 19 + .../content/application~1json/schema.java | 19 + .../content/application~1json/schema.java | 19 + .../content/application~1json/schema.java | 19 + .../content/application~1json/schema.java | 19 + .../content/application~1json/schema.java | 19 + .../content/application~1json/schema.java | 19 + .../content/application~1json/schema.java | 19 + .../application~1octet-stream/schema.java | 20 + .../content/multipart~1form-data/schema.java | 229 +++ .../content/multipart~1form-data/schema.java | 279 ++++ .../paths/foo/get/FooServerservers.java | 11 + .../PetfindbystatusServers.java | 11 + .../schema.java | 216 +++ .../content/multipart~1form-data/schema.java | 217 +++ .../content/application~1json/schema.java | 19 + .../content/application~1json/schema.java | 19 + .../content/application~1json/schema.java | 19 + .../DefaultGeneratorRunner.java | 5 +- .../codegen/generators/DefaultGenerator.java | 44 +- .../codegen/generators/Generator.java | 3 +- .../generators/JavaClientGenerator.java | 34 +- .../main/java/packagename/servers/Servers.hbs | 15 + 42 files changed, 4987 insertions(+), 24 deletions(-) create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/Servers.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/requestbody/content/application~1x-www-form-urlencoded/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/post/requestbody/content/application~1x-www-form-urlencoded/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeadditionalpropertieswitharrayofenums/get/requestbody/content/application~1json/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakebodywithfileschema/put/requestbody/content/application~1json/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakebodywithqueryparams/put/requestbody/content/application~1json/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeinlineadditionalproperties/post/requestbody/content/application~1json/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeinlinecomposition/post/requestbody/content/application~1json/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeinlinecomposition/post/requestbody/content/multipart~1form-data/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakejsonformdata/get/requestbody/content/application~1x-www-form-urlencoded/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakejsonpatch/patch/requestbody/content/application~1json-patch+json/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakejsonwithcharset/post/requestbody/content/application~1json; charset=utf-8/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakemultiplerequestbodycontenttypes/post/requestbody/content/application~1json/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakemultiplerequestbodycontenttypes/post/requestbody/content/multipart~1form-data/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeparametercollisions1ababselfab/post/requestbody/content/application~1json/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakepemcontenttype/get/requestbody/content/application~1x-pem-file/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakepetiduploadimagewithrequiredfile/post/requestbody/content/multipart~1form-data/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsarraymodel/post/requestbody/content/application~1json/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsarrayofenums/post/requestbody/content/application~1json/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsboolean/post/requestbody/content/application~1json/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefscomposedoneofnumberwithvalidations/post/requestbody/content/application~1json/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsenum/post/requestbody/content/application~1json/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsmammal/post/requestbody/content/application~1json/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsnumber/post/requestbody/content/application~1json/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsobjectmodelwithrefprops/post/requestbody/content/application~1json/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsstring/post/requestbody/content/application~1json/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeuploaddownloadfile/post/requestbody/content/application~1octet-stream/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeuploadfile/post/requestbody/content/multipart~1form-data/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeuploadfiles/post/requestbody/content/multipart~1form-data/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/FooServerservers.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/PetfindbystatusServers.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petpetid/post/requestbody/content/application~1x-www-form-urlencoded/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petpetiduploadimage/post/requestbody/content/multipart~1form-data/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/storeorder/post/requestbody/content/application~1json/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/user/post/requestbody/content/application~1json/schema.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/userusername/put/requestbody/content/application~1json/schema.java create mode 100644 src/main/resources/java/src/main/java/packagename/servers/Servers.hbs diff --git a/samples/client/petstore/java/.openapi-generator/FILES b/samples/client/petstore/java/.openapi-generator/FILES index 5854479fe40..111d171a1f4 100644 --- a/samples/client/petstore/java/.openapi-generator/FILES +++ b/samples/client/petstore/java/.openapi-generator/FILES @@ -305,6 +305,7 @@ docs/servers/Server0.md docs/servers/Server1.md docs/servers/Server2.md pom.xml +src/main/java/org/openapijsonschematools/client/Servers.java src/main/java/org/openapijsonschematools/client/components/headers/int32jsoncontenttypeheader/content/applicationjson/Int32JsonContentTypeHeaderSchema.java src/main/java/org/openapijsonschematools/client/components/headers/numberheader/NumberHeaderSchema.java src/main/java/org/openapijsonschematools/client/components/headers/refcontentschemaheader/content/applicationjson/RefContentSchemaHeaderSchema.java @@ -612,10 +613,12 @@ src/main/java/org/openapijsonschematools/client/paths/fakewildcardresponses/get/ src/main/java/org/openapijsonschematools/client/paths/fakewildcardresponses/get/responses/response3xx/content/applicationjson/Schema.java src/main/java/org/openapijsonschematools/client/paths/fakewildcardresponses/get/responses/response4xx/content/applicationjson/Schema.java src/main/java/org/openapijsonschematools/client/paths/fakewildcardresponses/get/responses/response5xx/content/applicationjson/Schema.java +src/main/java/org/openapijsonschematools/client/paths/foo/get/FooServerservers.java src/main/java/org/openapijsonschematools/client/paths/foo/get/responses/responsedefault/content/applicationjson/Schema.java src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/FooGetServer0.java src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/FooGetServer1.java src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/server1/Variables.java +src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/PetfindbystatusServers.java src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/get/QueryParameters.java src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/get/parameters/parameter0/Schema0.java src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/servers/PetfindbystatusServer0.java diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/Servers.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/Servers.java new file mode 100644 index 00000000000..53a165c353f --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/Servers.java @@ -0,0 +1,13 @@ +package org.openapijsonschematools.client.servers; + +public class Servers { + private Server0 server0; + private Server1 server1; + private Server2 server2; + + public Servers() { + server0 = new Server0(); + server1 = new Server1(); + server2 = new Server2(); + } +} \ No newline at end of file diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/requestbody/content/application~1x-www-form-urlencoded/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/requestbody/content/application~1x-www-form-urlencoded/schema.java new file mode 100644 index 00000000000..1ccdb1ed22e --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/requestbody/content/application~1x-www-form-urlencoded/schema.java @@ -0,0 +1,459 @@ +package org.openapijsonschematools.client.paths.fake.get.requestbody.content.application~1x-www-form-urlencoded; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; +import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; +import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.BaseBuilder; +import org.openapijsonschematools.client.schemas.SetMaker; +import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; +import org.openapijsonschematools.client.schemas.validation.DefaultValueMethod; +import org.openapijsonschematools.client.schemas.validation.FrozenList; +import org.openapijsonschematools.client.schemas.validation.FrozenMap; +import org.openapijsonschematools.client.schemas.validation.JsonSchema; +import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; +import org.openapijsonschematools.client.schemas.validation.ListSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; +import org.openapijsonschematools.client.schemas.validation.PropertyEntry; +import org.openapijsonschematools.client.schemas.validation.StringEnumValidator; +import org.openapijsonschematools.client.schemas.validation.StringSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.StringValueMethod; +import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; + +public class Schema { + // nest classes so all schemas and input/output classes can be public + + public enum StringItemsEnums implements StringValueMethod { + GREATER_THAN_SIGN(">"), + DOLLAR_SIGN("$"); + private final String value; + + StringItemsEnums(String value) { + this.value = value; + } + public String value() { + return this.value; + } + } + + + public static class Items extends JsonSchema implements StringSchemaValidator, StringEnumValidator, DefaultValueMethod { + private static @Nullable Items instance = null; + + protected Items() { + super(new JsonSchemaInfo() + .type(Set.of( + String.class + )) + .enumValues(SetMaker.makeSet( + ">", + "$" + )) + .defaultValue("$") + ); + } + + public static Items getInstance() { + if (instance == null) { + instance = new Items(); + } + return instance; + } + + @Override + public String validate(String arg, SchemaConfiguration configuration) throws ValidationException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + String castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + + @Override + public String validate(StringItemsEnums arg,SchemaConfiguration configuration) throws ValidationException { + return validate(arg.value(), configuration); + } + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof String) { + return validate((String) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof String) { + return getNewInstance((String) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + public String defaultValue() { + if (defaultValue instanceof String) { + return (String) defaultValue; + } + throw new InvalidTypeException("Invalid type stored in defaultValue"); + } + } + + public static class EnumFormStringArrayList extends FrozenList { + protected EnumFormStringArrayList(FrozenList m) { + super(m); + } + public static EnumFormStringArrayList of(List arg, SchemaConfiguration configuration) throws ValidationException { + return EnumFormStringArray.getInstance().validate(arg, configuration); + } + } + + public static class EnumFormStringArrayListBuilder { + // class to build List + private final List list; + + public EnumFormStringArrayListBuilder() { + list = new ArrayList<>(); + } + + public EnumFormStringArrayListBuilder(List list) { + this.list = list; + } + + public EnumFormStringArrayListBuilder add(String item) { + list.add(item); + return this; + } + + public EnumFormStringArrayListBuilder add(StringItemsEnums item) { + list.add(item.value()); + return this; + } + + public List build() { + return list; + } + } + + + public static class EnumFormStringArray extends JsonSchema implements ListSchemaValidator { + private static @Nullable EnumFormStringArray instance = null; + + protected EnumFormStringArray() { + super(new JsonSchemaInfo() + .type(Set.of(List.class)) + .items(Items.class) + ); + } + + public static EnumFormStringArray getInstance() { + if (instance == null) { + instance = new EnumFormStringArray(); + } + return instance; + } + + @Override + public EnumFormStringArrayList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List items = new ArrayList<>(); + int i = 0; + for (Object item: arg) { + List itemPathToItem = new ArrayList<>(pathToItem); + itemPathToItem.add(i); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof String)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((String) itemInstance); + i += 1; + } + FrozenList newInstanceItems = new FrozenList<>(items); + return new EnumFormStringArrayList(newInstanceItems); + } + + public EnumFormStringArrayList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + List castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); + } + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof List) { + return validate((List) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof List) { + return getNewInstance((List) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + public enum StringEnumFormStringEnums implements StringValueMethod { + _ABC("_abc"), + HYPHEN_MINUS_EFG("-efg"), + LEFT_PARENTHESIS_XYZ_RIGHT_PARENTHESIS("(xyz)"); + private final String value; + + StringEnumFormStringEnums(String value) { + this.value = value; + } + public String value() { + return this.value; + } + } + + + public static class EnumFormString extends JsonSchema implements StringSchemaValidator, StringEnumValidator, DefaultValueMethod { + private static @Nullable EnumFormString instance = null; + + protected EnumFormString() { + super(new JsonSchemaInfo() + .type(Set.of( + String.class + )) + .enumValues(SetMaker.makeSet( + "_abc", + "-efg", + "(xyz)" + )) + .defaultValue("-efg") + ); + } + + public static EnumFormString getInstance() { + if (instance == null) { + instance = new EnumFormString(); + } + return instance; + } + + @Override + public String validate(String arg, SchemaConfiguration configuration) throws ValidationException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + String castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + + @Override + public String validate(StringEnumFormStringEnums arg,SchemaConfiguration configuration) throws ValidationException { + return validate(arg.value(), configuration); + } + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof String) { + return validate((String) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof String) { + return getNewInstance((String) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + public String defaultValue() { + if (defaultValue instanceof String) { + return (String) defaultValue; + } + throw new InvalidTypeException("Invalid type stored in defaultValue"); + } + } + + public static class SchemaMap extends FrozenMap<@Nullable Object> { + protected SchemaMap(FrozenMap<@Nullable Object> m) { + super(m); + } + public static final Set requiredKeys = Set.of(); + public static final Set optionalKeys = Set.of( + "enum_form_string_array", + "enum_form_string" + ); + public static SchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + return Schema1.getInstance().validate(arg, configuration); + } + + public EnumFormStringArrayList enum_form_string_array() throws UnsetPropertyException { + String key = "enum_form_string_array"; + throwIfKeyNotPresent(key); + @Nullable Object value = get(key); + if (!(value instanceof EnumFormStringArrayList)) { + throw new InvalidTypeException("Invalid value stored for enum_form_string_array"); + } + return (EnumFormStringArrayList) value; + } + + public String enum_form_string() throws UnsetPropertyException { + String key = "enum_form_string"; + throwIfKeyNotPresent(key); + @Nullable Object value = get(key); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for enum_form_string"); + } + return (String) value; + } + + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { + throwIfKeyKnown(name, requiredKeys, optionalKeys); + throwIfKeyNotPresent(name); + return get(name); + } + } + + public interface SetterForEnumFormStringArray { + Map getInstance(); + T getBuilderAfterEnumFormStringArray(Map instance); + + default T enum_form_string_array(List value) { + var instance = getInstance(); + instance.put("enum_form_string_array", value); + return getBuilderAfterEnumFormStringArray(instance); + } + } + + public interface SetterForEnumFormString { + Map getInstance(); + T getBuilderAfterEnumFormString(Map instance); + + default T enum_form_string(String value) { + var instance = getInstance(); + instance.put("enum_form_string", value); + return getBuilderAfterEnumFormString(instance); + } + + default T enum_form_string(StringEnumFormStringEnums value) { + var instance = getInstance(); + instance.put("enum_form_string", value.value()); + return getBuilderAfterEnumFormString(instance); + } + } + + public static class SchemaMapBuilder extends UnsetAddPropsSetter implements BaseBuilder<@Nullable Object>, SetterForEnumFormStringArray, SetterForEnumFormString { + private final Map instance; + private static final Set knownKeys = Set.of( + "enum_form_string_array", + "enum_form_string" + ); + public Set getKnownKeys() { + return knownKeys; + } + public SchemaMapBuilder() { + this.instance = new LinkedHashMap<>(); + } + public Map build() { + return instance; + } + public Map getInstance() { + return instance; + } + public SchemaMapBuilder getBuilderAfterEnumFormStringArray(Map instance) { + return this; + } + public SchemaMapBuilder getBuilderAfterEnumFormString(Map instance) { + return this; + } + public SchemaMapBuilder getBuilderAfterAdditionalProperty(Map instance) { + return this; + } + } + + + public static class Schema1 extends JsonSchema implements MapSchemaValidator { + private static @Nullable Schema1 instance = null; + + protected Schema1() { + super(new JsonSchemaInfo() + .type(Set.of(Map.class)) + .properties(Map.ofEntries( + new PropertyEntry("enum_form_string_array", EnumFormStringArray.class), + new PropertyEntry("enum_form_string", EnumFormString.class) + )) + ); + } + + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + + public SchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); + for(Map.Entry entry: arg.entrySet()) { + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; + List propertyPathToItem = new ArrayList<>(pathToItem); + propertyPathToItem.add(propertyName); + Object value = entry.getValue(); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + properties.put(propertyName, propertyInstance); + } + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); + return new SchemaMap(castProperties); + } + + public SchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); + } + + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof Map) { + return validate((Map) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof Map) { + return getNewInstance((Map) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/post/requestbody/content/application~1x-www-form-urlencoded/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/post/requestbody/content/application~1x-www-form-urlencoded/schema.java new file mode 100644 index 00000000000..61d313e6029 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/post/requestbody/content/application~1x-www-form-urlencoded/schema.java @@ -0,0 +1,1353 @@ +package org.openapijsonschematools.client.paths.fake.post.requestbody.content.application~1x-www-form-urlencoded; +import java.time.ZonedDateTime; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.regex.Pattern; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; +import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; +import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.BaseBuilder; +import org.openapijsonschematools.client.schemas.DateJsonSchema; +import org.openapijsonschematools.client.schemas.Int64JsonSchema; +import org.openapijsonschematools.client.schemas.StringJsonSchema; +import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; +import org.openapijsonschematools.client.schemas.validation.DefaultValueMethod; +import org.openapijsonschematools.client.schemas.validation.FrozenMap; +import org.openapijsonschematools.client.schemas.validation.JsonSchema; +import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; +import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.NumberSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; +import org.openapijsonschematools.client.schemas.validation.PropertyEntry; +import org.openapijsonschematools.client.schemas.validation.StringSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; + +public class Schema { + // nest classes so all schemas and input/output classes can be public + + + public static class IntegerSchema extends JsonSchema implements NumberSchemaValidator { + private static @Nullable IntegerSchema instance = null; + + protected IntegerSchema() { + super(new JsonSchemaInfo() + .type(Set.of( + Integer.class, + Long.class, + Float.class, + Double.class + )) + .format("int") + .maximum(100) + .minimum(10) + ); + } + + public static IntegerSchema getInstance() { + if (instance == null) { + instance = new IntegerSchema(); + } + return instance; + } + + @Override + public Number validate(Number arg, SchemaConfiguration configuration) throws ValidationException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + Number castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + + public int validate(int arg, SchemaConfiguration configuration) throws ValidationException { + return (int) validate((Number) arg, configuration); + } + + public float validate(float arg, SchemaConfiguration configuration) throws ValidationException { + return (float) validate((Number) arg, configuration); + } + + public long validate(long arg, SchemaConfiguration configuration) throws ValidationException { + return (long) validate((Number) arg, configuration); + } + + public double validate(double arg, SchemaConfiguration configuration) throws ValidationException { + return (double) validate((Number) arg, configuration); + } + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof Number) { + return validate((Number) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof Number) { + return getNewInstance((Number) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + + public static class Int32 extends JsonSchema implements NumberSchemaValidator { + private static @Nullable Int32 instance = null; + + protected Int32() { + super(new JsonSchemaInfo() + .type(Set.of( + Integer.class, + Long.class, + Float.class, + Double.class + )) + .format("int32") + .maximum(200) + .minimum(20) + ); + } + + public static Int32 getInstance() { + if (instance == null) { + instance = new Int32(); + } + return instance; + } + + @Override + public Number validate(Number arg, SchemaConfiguration configuration) throws ValidationException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + Number castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + + public int validate(int arg, SchemaConfiguration configuration) throws ValidationException { + return (int) validate((Number) arg, configuration); + } + + public float validate(float arg, SchemaConfiguration configuration) throws ValidationException { + return (float) validate((Number) arg, configuration); + } + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof Number) { + return validate((Number) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof Number) { + return getNewInstance((Number) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + + public static class Int64 extends Int64JsonSchema { + private static @Nullable Int64 instance = null; + public static Int64 getInstance() { + if (instance == null) { + instance = new Int64(); + } + return instance; + } + } + + + public static class NumberSchema extends JsonSchema implements NumberSchemaValidator { + private static @Nullable NumberSchema instance = null; + + protected NumberSchema() { + super(new JsonSchemaInfo() + .type(Set.of( + Integer.class, + Long.class, + Float.class, + Double.class + )) + .maximum(543.2) + .minimum(32.1) + ); + } + + public static NumberSchema getInstance() { + if (instance == null) { + instance = new NumberSchema(); + } + return instance; + } + + @Override + public Number validate(Number arg, SchemaConfiguration configuration) throws ValidationException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + Number castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + + public int validate(int arg, SchemaConfiguration configuration) throws ValidationException { + return (int) validate((Number) arg, configuration); + } + + public long validate(long arg, SchemaConfiguration configuration) throws ValidationException { + return (long) validate((Number) arg, configuration); + } + + public float validate(float arg, SchemaConfiguration configuration) throws ValidationException { + return (float) validate((Number) arg, configuration); + } + + public double validate(double arg, SchemaConfiguration configuration) throws ValidationException { + return (double) validate((Number) arg, configuration); + } + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof Number) { + return validate((Number) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof Number) { + return getNewInstance((Number) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + + public static class FloatSchema extends JsonSchema implements NumberSchemaValidator { + private static @Nullable FloatSchema instance = null; + + protected FloatSchema() { + super(new JsonSchemaInfo() + .type(Set.of( + Integer.class, + Long.class, + Float.class, + Double.class + )) + .format("float") + .maximum(987.6) + ); + } + + public static FloatSchema getInstance() { + if (instance == null) { + instance = new FloatSchema(); + } + return instance; + } + + @Override + public Number validate(Number arg, SchemaConfiguration configuration) throws ValidationException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + Number castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + public float validate(float arg, SchemaConfiguration configuration) throws ValidationException { + return (float) validate((Number) arg, configuration); + } + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof Number) { + return validate((Number) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof Number) { + return getNewInstance((Number) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + + public static class DoubleSchema extends JsonSchema implements NumberSchemaValidator { + private static @Nullable DoubleSchema instance = null; + + protected DoubleSchema() { + super(new JsonSchemaInfo() + .type(Set.of( + Integer.class, + Long.class, + Float.class, + Double.class + )) + .format("double") + .maximum(123.4) + .minimum(67.8) + ); + } + + public static DoubleSchema getInstance() { + if (instance == null) { + instance = new DoubleSchema(); + } + return instance; + } + + @Override + public Number validate(Number arg, SchemaConfiguration configuration) throws ValidationException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + Number castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + public double validate(double arg, SchemaConfiguration configuration) throws ValidationException { + return (double) validate((Number) arg, configuration); + } + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof Number) { + return validate((Number) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof Number) { + return getNewInstance((Number) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + + public static class StringSchema extends JsonSchema implements StringSchemaValidator { + private static @Nullable StringSchema instance = null; + + protected StringSchema() { + super(new JsonSchemaInfo() + .type(Set.of( + String.class + )) + .pattern(Pattern.compile( + "[a-z]", + Pattern.CASE_INSENSITIVE + )) + ); + } + + public static StringSchema getInstance() { + if (instance == null) { + instance = new StringSchema(); + } + return instance; + } + + @Override + public String validate(String arg, SchemaConfiguration configuration) throws ValidationException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + String castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof String) { + return validate((String) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof String) { + return getNewInstance((String) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + + public static class PatternWithoutDelimiter extends JsonSchema implements StringSchemaValidator { + private static @Nullable PatternWithoutDelimiter instance = null; + + protected PatternWithoutDelimiter() { + super(new JsonSchemaInfo() + .type(Set.of( + String.class + )) + .pattern(Pattern.compile( + "^[A-Z].*" + )) + ); + } + + public static PatternWithoutDelimiter getInstance() { + if (instance == null) { + instance = new PatternWithoutDelimiter(); + } + return instance; + } + + @Override + public String validate(String arg, SchemaConfiguration configuration) throws ValidationException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + String castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof String) { + return validate((String) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof String) { + return getNewInstance((String) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + + public static class ByteSchema extends StringJsonSchema { + private static @Nullable ByteSchema instance = null; + public static ByteSchema getInstance() { + if (instance == null) { + instance = new ByteSchema(); + } + return instance; + } + } + + + public static class Binary extends StringJsonSchema { + // BinarySchema + private static @Nullable Binary instance = null; + public static Binary getInstance() { + if (instance == null) { + instance = new Binary(); + } + return instance; + } + } + + + public static class Date extends DateJsonSchema { + private static @Nullable Date instance = null; + public static Date getInstance() { + if (instance == null) { + instance = new Date(); + } + return instance; + } + } + + + public static class DateTime extends JsonSchema implements StringSchemaValidator, DefaultValueMethod { + private static @Nullable DateTime instance = null; + + protected DateTime() { + super(new JsonSchemaInfo() + .type(Set.of( + String.class + )) + .format("date-time") + .defaultValue("2010-02-01T10:20:10.111110+01:00") + ); + } + + public static DateTime getInstance() { + if (instance == null) { + instance = new DateTime(); + } + return instance; + } + + @Override + public String validate(String arg, SchemaConfiguration configuration) throws ValidationException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + String castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof String) { + return validate((String) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof String) { + return getNewInstance((String) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + public String defaultValue() { + if (defaultValue instanceof String) { + return (String) defaultValue; + } + throw new InvalidTypeException("Invalid type stored in defaultValue"); + } + } + + public static class Password extends JsonSchema implements StringSchemaValidator { + private static @Nullable Password instance = null; + + protected Password() { + super(new JsonSchemaInfo() + .type(Set.of( + String.class + )) + .format("password") + .maxLength(64) + .minLength(10) + ); + } + + public static Password getInstance() { + if (instance == null) { + instance = new Password(); + } + return instance; + } + + @Override + public String validate(String arg, SchemaConfiguration configuration) throws ValidationException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + String castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof String) { + return validate((String) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof String) { + return getNewInstance((String) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + + public static class Callback extends StringJsonSchema { + private static @Nullable Callback instance = null; + public static Callback getInstance() { + if (instance == null) { + instance = new Callback(); + } + return instance; + } + } + + + public static class SchemaMap extends FrozenMap<@Nullable Object> { + protected SchemaMap(FrozenMap<@Nullable Object> m) { + super(m); + } + public static final Set requiredKeys = Set.of( + "byte", + "double", + "number", + "pattern_without_delimiter" + ); + public static final Set optionalKeys = Set.of( + "integer", + "int32", + "int64", + "float", + "string", + "binary", + "date", + "dateTime", + "password", + "callback" + ); + public static SchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + return Schema1.getInstance().validate(arg, configuration); + } + + public String pattern_without_delimiter() { + @Nullable Object value = get("pattern_without_delimiter"); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for pattern_without_delimiter"); + } + return (String) value; + } + + public Number int32() throws UnsetPropertyException { + String key = "int32"; + throwIfKeyNotPresent(key); + @Nullable Object value = get(key); + if (!(value instanceof Number)) { + throw new InvalidTypeException("Invalid value stored for int32"); + } + return (Number) value; + } + + public Number int64() throws UnsetPropertyException { + String key = "int64"; + throwIfKeyNotPresent(key); + @Nullable Object value = get(key); + if (!(value instanceof Number)) { + throw new InvalidTypeException("Invalid value stored for int64"); + } + return (Number) value; + } + + public String binary() throws UnsetPropertyException { + String key = "binary"; + throwIfKeyNotPresent(key); + @Nullable Object value = get(key); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for binary"); + } + return (String) value; + } + + public String date() throws UnsetPropertyException { + String key = "date"; + throwIfKeyNotPresent(key); + @Nullable Object value = get(key); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for date"); + } + return (String) value; + } + + public String dateTime() throws UnsetPropertyException { + String key = "dateTime"; + throwIfKeyNotPresent(key); + @Nullable Object value = get(key); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for dateTime"); + } + return (String) value; + } + + public String password() throws UnsetPropertyException { + String key = "password"; + throwIfKeyNotPresent(key); + @Nullable Object value = get(key); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for password"); + } + return (String) value; + } + + public String callback() throws UnsetPropertyException { + String key = "callback"; + throwIfKeyNotPresent(key); + @Nullable Object value = get(key); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for callback"); + } + return (String) value; + } + + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { + throwIfKeyKnown(name, requiredKeys, optionalKeys); + throwIfKeyNotPresent(name); + return get(name); + } + } + + public interface SetterForByteSchema { + Map getInstance(); + T getBuilderAfterByteSchema(Map instance); + + default T setByte(String value) { + var instance = getInstance(); + instance.put("byte", value); + return getBuilderAfterByteSchema(instance); + } + } + + public interface SetterForDoubleSchema { + Map getInstance(); + T getBuilderAfterDoubleSchema(Map instance); + + default T setDouble(int value) { + var instance = getInstance(); + instance.put("double", value); + return getBuilderAfterDoubleSchema(instance); + } + + default T setDouble(float value) { + var instance = getInstance(); + instance.put("double", value); + return getBuilderAfterDoubleSchema(instance); + } + + default T setDouble(long value) { + var instance = getInstance(); + instance.put("double", value); + return getBuilderAfterDoubleSchema(instance); + } + + default T setDouble(double value) { + var instance = getInstance(); + instance.put("double", value); + return getBuilderAfterDoubleSchema(instance); + } + } + + public interface SetterForNumberSchema { + Map getInstance(); + T getBuilderAfterNumberSchema(Map instance); + + default T setNumber(int value) { + var instance = getInstance(); + instance.put("number", value); + return getBuilderAfterNumberSchema(instance); + } + + default T setNumber(float value) { + var instance = getInstance(); + instance.put("number", value); + return getBuilderAfterNumberSchema(instance); + } + + default T setNumber(long value) { + var instance = getInstance(); + instance.put("number", value); + return getBuilderAfterNumberSchema(instance); + } + + default T setNumber(double value) { + var instance = getInstance(); + instance.put("number", value); + return getBuilderAfterNumberSchema(instance); + } + } + + public interface SetterForPatternWithoutDelimiter { + Map getInstance(); + T getBuilderAfterPatternWithoutDelimiter(Map instance); + + default T pattern_without_delimiter(String value) { + var instance = getInstance(); + instance.put("pattern_without_delimiter", value); + return getBuilderAfterPatternWithoutDelimiter(instance); + } + } + + public interface SetterForIntegerSchema { + Map getInstance(); + T getBuilderAfterIntegerSchema(Map instance); + + default T setInteger(int value) { + var instance = getInstance(); + instance.put("integer", value); + return getBuilderAfterIntegerSchema(instance); + } + + default T setInteger(float value) { + var instance = getInstance(); + instance.put("integer", value); + return getBuilderAfterIntegerSchema(instance); + } + + default T setInteger(long value) { + var instance = getInstance(); + instance.put("integer", value); + return getBuilderAfterIntegerSchema(instance); + } + + default T setInteger(double value) { + var instance = getInstance(); + instance.put("integer", value); + return getBuilderAfterIntegerSchema(instance); + } + } + + public interface SetterForInt32 { + Map getInstance(); + T getBuilderAfterInt32(Map instance); + + default T int32(int value) { + var instance = getInstance(); + instance.put("int32", value); + return getBuilderAfterInt32(instance); + } + + default T int32(float value) { + var instance = getInstance(); + instance.put("int32", value); + return getBuilderAfterInt32(instance); + } + } + + public interface SetterForInt64 { + Map getInstance(); + T getBuilderAfterInt64(Map instance); + + default T int64(int value) { + var instance = getInstance(); + instance.put("int64", value); + return getBuilderAfterInt64(instance); + } + + default T int64(float value) { + var instance = getInstance(); + instance.put("int64", value); + return getBuilderAfterInt64(instance); + } + + default T int64(long value) { + var instance = getInstance(); + instance.put("int64", value); + return getBuilderAfterInt64(instance); + } + + default T int64(double value) { + var instance = getInstance(); + instance.put("int64", value); + return getBuilderAfterInt64(instance); + } + } + + public interface SetterForFloatSchema { + Map getInstance(); + T getBuilderAfterFloatSchema(Map instance); + + default T setFloat(int value) { + var instance = getInstance(); + instance.put("float", value); + return getBuilderAfterFloatSchema(instance); + } + + default T setFloat(float value) { + var instance = getInstance(); + instance.put("float", value); + return getBuilderAfterFloatSchema(instance); + } + + default T setFloat(long value) { + var instance = getInstance(); + instance.put("float", value); + return getBuilderAfterFloatSchema(instance); + } + + default T setFloat(double value) { + var instance = getInstance(); + instance.put("float", value); + return getBuilderAfterFloatSchema(instance); + } + } + + public interface SetterForStringSchema { + Map getInstance(); + T getBuilderAfterStringSchema(Map instance); + + default T setString(String value) { + var instance = getInstance(); + instance.put("string", value); + return getBuilderAfterStringSchema(instance); + } + } + + public interface SetterForBinary { + Map getInstance(); + T getBuilderAfterBinary(Map instance); + + default T binary(String value) { + var instance = getInstance(); + instance.put("binary", value); + return getBuilderAfterBinary(instance); + } + } + + public interface SetterForDate { + Map getInstance(); + T getBuilderAfterDate(Map instance); + + default T date(String value) { + var instance = getInstance(); + instance.put("date", value); + return getBuilderAfterDate(instance); + } + } + + public interface SetterForDateTime { + Map getInstance(); + T getBuilderAfterDateTime(Map instance); + + default T dateTime(String value) { + var instance = getInstance(); + instance.put("dateTime", value); + return getBuilderAfterDateTime(instance); + } + } + + public interface SetterForPassword { + Map getInstance(); + T getBuilderAfterPassword(Map instance); + + default T password(String value) { + var instance = getInstance(); + instance.put("password", value); + return getBuilderAfterPassword(instance); + } + } + + public interface SetterForCallback { + Map getInstance(); + T getBuilderAfterCallback(Map instance); + + default T callback(String value) { + var instance = getInstance(); + instance.put("callback", value); + return getBuilderAfterCallback(instance); + } + } + + public static class SchemaMap0000Builder extends UnsetAddPropsSetter implements BaseBuilder<@Nullable Object>, SetterForIntegerSchema, SetterForInt32, SetterForInt64, SetterForFloatSchema, SetterForStringSchema, SetterForBinary, SetterForDate, SetterForDateTime, SetterForPassword, SetterForCallback { + private final Map instance; + private static final Set knownKeys = Set.of( + "byte", + "double", + "number", + "pattern_without_delimiter", + "integer", + "int32", + "int64", + "float", + "string", + "binary", + "date", + "dateTime", + "password", + "callback" + ); + public Set getKnownKeys() { + return knownKeys; + } + public SchemaMap0000Builder(Map instance) { + this.instance = instance; + } + public Map build() { + return instance; + } + public Map getInstance() { + return instance; + } + public SchemaMap0000Builder getBuilderAfterIntegerSchema(Map instance) { + return this; + } + public SchemaMap0000Builder getBuilderAfterInt32(Map instance) { + return this; + } + public SchemaMap0000Builder getBuilderAfterInt64(Map instance) { + return this; + } + public SchemaMap0000Builder getBuilderAfterFloatSchema(Map instance) { + return this; + } + public SchemaMap0000Builder getBuilderAfterStringSchema(Map instance) { + return this; + } + public SchemaMap0000Builder getBuilderAfterBinary(Map instance) { + return this; + } + public SchemaMap0000Builder getBuilderAfterDate(Map instance) { + return this; + } + public SchemaMap0000Builder getBuilderAfterDateTime(Map instance) { + return this; + } + public SchemaMap0000Builder getBuilderAfterPassword(Map instance) { + return this; + } + public SchemaMap0000Builder getBuilderAfterCallback(Map instance) { + return this; + } + public SchemaMap0000Builder getBuilderAfterAdditionalProperty(Map instance) { + return this; + } + } + + public static class SchemaMap0001Builder implements SetterForPatternWithoutDelimiter { + private final Map instance; + public SchemaMap0001Builder(Map instance) { + this.instance = instance; + } + public Map getInstance() { + return instance; + } + public SchemaMap0000Builder getBuilderAfterPatternWithoutDelimiter(Map instance) { + return new SchemaMap0000Builder(instance); + } + } + + public static class SchemaMap0010Builder implements SetterForNumberSchema { + private final Map instance; + public SchemaMap0010Builder(Map instance) { + this.instance = instance; + } + public Map getInstance() { + return instance; + } + public SchemaMap0000Builder getBuilderAfterNumberSchema(Map instance) { + return new SchemaMap0000Builder(instance); + } + } + + public static class SchemaMap0011Builder implements SetterForNumberSchema, SetterForPatternWithoutDelimiter { + private final Map instance; + public SchemaMap0011Builder(Map instance) { + this.instance = instance; + } + public Map getInstance() { + return instance; + } + public SchemaMap0001Builder getBuilderAfterNumberSchema(Map instance) { + return new SchemaMap0001Builder(instance); + } + public SchemaMap0010Builder getBuilderAfterPatternWithoutDelimiter(Map instance) { + return new SchemaMap0010Builder(instance); + } + } + + public static class SchemaMap0100Builder implements SetterForDoubleSchema { + private final Map instance; + public SchemaMap0100Builder(Map instance) { + this.instance = instance; + } + public Map getInstance() { + return instance; + } + public SchemaMap0000Builder getBuilderAfterDoubleSchema(Map instance) { + return new SchemaMap0000Builder(instance); + } + } + + public static class SchemaMap0101Builder implements SetterForDoubleSchema, SetterForPatternWithoutDelimiter { + private final Map instance; + public SchemaMap0101Builder(Map instance) { + this.instance = instance; + } + public Map getInstance() { + return instance; + } + public SchemaMap0001Builder getBuilderAfterDoubleSchema(Map instance) { + return new SchemaMap0001Builder(instance); + } + public SchemaMap0100Builder getBuilderAfterPatternWithoutDelimiter(Map instance) { + return new SchemaMap0100Builder(instance); + } + } + + public static class SchemaMap0110Builder implements SetterForDoubleSchema, SetterForNumberSchema { + private final Map instance; + public SchemaMap0110Builder(Map instance) { + this.instance = instance; + } + public Map getInstance() { + return instance; + } + public SchemaMap0010Builder getBuilderAfterDoubleSchema(Map instance) { + return new SchemaMap0010Builder(instance); + } + public SchemaMap0100Builder getBuilderAfterNumberSchema(Map instance) { + return new SchemaMap0100Builder(instance); + } + } + + public static class SchemaMap0111Builder implements SetterForDoubleSchema, SetterForNumberSchema, SetterForPatternWithoutDelimiter { + private final Map instance; + public SchemaMap0111Builder(Map instance) { + this.instance = instance; + } + public Map getInstance() { + return instance; + } + public SchemaMap0011Builder getBuilderAfterDoubleSchema(Map instance) { + return new SchemaMap0011Builder(instance); + } + public SchemaMap0101Builder getBuilderAfterNumberSchema(Map instance) { + return new SchemaMap0101Builder(instance); + } + public SchemaMap0110Builder getBuilderAfterPatternWithoutDelimiter(Map instance) { + return new SchemaMap0110Builder(instance); + } + } + + public static class SchemaMap1000Builder implements SetterForByteSchema { + private final Map instance; + public SchemaMap1000Builder(Map instance) { + this.instance = instance; + } + public Map getInstance() { + return instance; + } + public SchemaMap0000Builder getBuilderAfterByteSchema(Map instance) { + return new SchemaMap0000Builder(instance); + } + } + + public static class SchemaMap1001Builder implements SetterForByteSchema, SetterForPatternWithoutDelimiter { + private final Map instance; + public SchemaMap1001Builder(Map instance) { + this.instance = instance; + } + public Map getInstance() { + return instance; + } + public SchemaMap0001Builder getBuilderAfterByteSchema(Map instance) { + return new SchemaMap0001Builder(instance); + } + public SchemaMap1000Builder getBuilderAfterPatternWithoutDelimiter(Map instance) { + return new SchemaMap1000Builder(instance); + } + } + + public static class SchemaMap1010Builder implements SetterForByteSchema, SetterForNumberSchema { + private final Map instance; + public SchemaMap1010Builder(Map instance) { + this.instance = instance; + } + public Map getInstance() { + return instance; + } + public SchemaMap0010Builder getBuilderAfterByteSchema(Map instance) { + return new SchemaMap0010Builder(instance); + } + public SchemaMap1000Builder getBuilderAfterNumberSchema(Map instance) { + return new SchemaMap1000Builder(instance); + } + } + + public static class SchemaMap1011Builder implements SetterForByteSchema, SetterForNumberSchema, SetterForPatternWithoutDelimiter { + private final Map instance; + public SchemaMap1011Builder(Map instance) { + this.instance = instance; + } + public Map getInstance() { + return instance; + } + public SchemaMap0011Builder getBuilderAfterByteSchema(Map instance) { + return new SchemaMap0011Builder(instance); + } + public SchemaMap1001Builder getBuilderAfterNumberSchema(Map instance) { + return new SchemaMap1001Builder(instance); + } + public SchemaMap1010Builder getBuilderAfterPatternWithoutDelimiter(Map instance) { + return new SchemaMap1010Builder(instance); + } + } + + public static class SchemaMap1100Builder implements SetterForByteSchema, SetterForDoubleSchema { + private final Map instance; + public SchemaMap1100Builder(Map instance) { + this.instance = instance; + } + public Map getInstance() { + return instance; + } + public SchemaMap0100Builder getBuilderAfterByteSchema(Map instance) { + return new SchemaMap0100Builder(instance); + } + public SchemaMap1000Builder getBuilderAfterDoubleSchema(Map instance) { + return new SchemaMap1000Builder(instance); + } + } + + public static class SchemaMap1101Builder implements SetterForByteSchema, SetterForDoubleSchema, SetterForPatternWithoutDelimiter { + private final Map instance; + public SchemaMap1101Builder(Map instance) { + this.instance = instance; + } + public Map getInstance() { + return instance; + } + public SchemaMap0101Builder getBuilderAfterByteSchema(Map instance) { + return new SchemaMap0101Builder(instance); + } + public SchemaMap1001Builder getBuilderAfterDoubleSchema(Map instance) { + return new SchemaMap1001Builder(instance); + } + public SchemaMap1100Builder getBuilderAfterPatternWithoutDelimiter(Map instance) { + return new SchemaMap1100Builder(instance); + } + } + + public static class SchemaMap1110Builder implements SetterForByteSchema, SetterForDoubleSchema, SetterForNumberSchema { + private final Map instance; + public SchemaMap1110Builder(Map instance) { + this.instance = instance; + } + public Map getInstance() { + return instance; + } + public SchemaMap0110Builder getBuilderAfterByteSchema(Map instance) { + return new SchemaMap0110Builder(instance); + } + public SchemaMap1010Builder getBuilderAfterDoubleSchema(Map instance) { + return new SchemaMap1010Builder(instance); + } + public SchemaMap1100Builder getBuilderAfterNumberSchema(Map instance) { + return new SchemaMap1100Builder(instance); + } + } + + public static class SchemaMapBuilder implements SetterForByteSchema, SetterForDoubleSchema, SetterForNumberSchema, SetterForPatternWithoutDelimiter { + private final Map instance; + public SchemaMapBuilder() { + this.instance = new LinkedHashMap<>(); + } + public Map getInstance() { + return instance; + } + public SchemaMap0111Builder getBuilderAfterByteSchema(Map instance) { + return new SchemaMap0111Builder(instance); + } + public SchemaMap1011Builder getBuilderAfterDoubleSchema(Map instance) { + return new SchemaMap1011Builder(instance); + } + public SchemaMap1101Builder getBuilderAfterNumberSchema(Map instance) { + return new SchemaMap1101Builder(instance); + } + public SchemaMap1110Builder getBuilderAfterPatternWithoutDelimiter(Map instance) { + return new SchemaMap1110Builder(instance); + } + } + + + public static class Schema1 extends JsonSchema implements MapSchemaValidator { + private static @Nullable Schema1 instance = null; + + protected Schema1() { + super(new JsonSchemaInfo() + .type(Set.of(Map.class)) + .properties(Map.ofEntries( + new PropertyEntry("integer", IntegerSchema.class), + new PropertyEntry("int32", Int32.class), + new PropertyEntry("int64", Int64.class), + new PropertyEntry("number", NumberSchema.class), + new PropertyEntry("float", FloatSchema.class), + new PropertyEntry("double", DoubleSchema.class), + new PropertyEntry("string", StringSchema.class), + new PropertyEntry("pattern_without_delimiter", PatternWithoutDelimiter.class), + new PropertyEntry("byte", ByteSchema.class), + new PropertyEntry("binary", Binary.class), + new PropertyEntry("date", Date.class), + new PropertyEntry("dateTime", DateTime.class), + new PropertyEntry("password", Password.class), + new PropertyEntry("callback", Callback.class) + )) + .required(Set.of( + "byte", + "double", + "number", + "pattern_without_delimiter" + )) + ); + } + + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + + public SchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); + for(Map.Entry entry: arg.entrySet()) { + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; + List propertyPathToItem = new ArrayList<>(pathToItem); + propertyPathToItem.add(propertyName); + Object value = entry.getValue(); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + properties.put(propertyName, propertyInstance); + } + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); + return new SchemaMap(castProperties); + } + + public SchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); + } + + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof Map) { + return validate((Map) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof Map) { + return getNewInstance((Map) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeadditionalpropertieswitharrayofenums/get/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeadditionalpropertieswitharrayofenums/get/requestbody/content/application~1json/schema.java new file mode 100644 index 00000000000..011f0a54815 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeadditionalpropertieswitharrayofenums/get/requestbody/content/application~1json/schema.java @@ -0,0 +1,19 @@ +package org.openapijsonschematools.client.paths.fakeadditionalpropertieswitharrayofenums.get.requestbody.content.application~1json; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.components.schemas.AdditionalPropertiesWithArrayOfEnums; + +public class Schema extends AdditionalPropertiesWithArrayOfEnums { + // $refed class + + + public static class Schema1 extends AdditionalPropertiesWithArrayOfEnums1 { + private static @Nullable Schema1 instance = null; + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + } +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakebodywithfileschema/put/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakebodywithfileschema/put/requestbody/content/application~1json/schema.java new file mode 100644 index 00000000000..9fef42db1bc --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakebodywithfileschema/put/requestbody/content/application~1json/schema.java @@ -0,0 +1,19 @@ +package org.openapijsonschematools.client.paths.fakebodywithfileschema.put.requestbody.content.application~1json; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.components.schemas.FileSchemaTestClass; + +public class Schema extends FileSchemaTestClass { + // $refed class + + + public static class Schema1 extends FileSchemaTestClass1 { + private static @Nullable Schema1 instance = null; + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + } +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakebodywithqueryparams/put/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakebodywithqueryparams/put/requestbody/content/application~1json/schema.java new file mode 100644 index 00000000000..517b9ecad2f --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakebodywithqueryparams/put/requestbody/content/application~1json/schema.java @@ -0,0 +1,19 @@ +package org.openapijsonschematools.client.paths.fakebodywithqueryparams.put.requestbody.content.application~1json; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.components.schemas.User; + +public class Schema extends User { + // $refed class + + + public static class Schema1 extends User1 { + private static @Nullable Schema1 instance = null; + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + } +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeinlineadditionalproperties/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeinlineadditionalproperties/post/requestbody/content/application~1json/schema.java new file mode 100644 index 00000000000..87939793e47 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeinlineadditionalproperties/post/requestbody/content/application~1json/schema.java @@ -0,0 +1,161 @@ +package org.openapijsonschematools.client.paths.fakeinlineadditionalproperties.post.requestbody.content.application~1json; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; +import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; +import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.BaseBuilder; +import org.openapijsonschematools.client.schemas.StringJsonSchema; +import org.openapijsonschematools.client.schemas.validation.FrozenMap; +import org.openapijsonschematools.client.schemas.validation.JsonSchema; +import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; +import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.MapUtils; +import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; +import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; + +public class Schema { + // nest classes so all schemas and input/output classes can be public + + + public static class AdditionalProperties extends StringJsonSchema { + private static @Nullable AdditionalProperties instance = null; + public static AdditionalProperties getInstance() { + if (instance == null) { + instance = new AdditionalProperties(); + } + return instance; + } + } + + + public static class SchemaMap extends FrozenMap { + protected SchemaMap(FrozenMap m) { + super(m); + } + public static final Set requiredKeys = Set.of(); + public static final Set optionalKeys = Set.of(); + public static SchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + return Schema1.getInstance().validate(arg, configuration); + } + + public String getAdditionalProperty(String name) throws UnsetPropertyException { + return getOrThrow(name); + } + } + + public interface SetterForAdditionalProperties { + Set getKnownKeys(); + Map getInstance(); + T getBuilderAfterAdditionalProperty(Map instance); + + default T additionalProperty(String key, String value) throws InvalidAdditionalPropertyException { + MapUtils.throwIfKeyKnown(key, getKnownKeys(), true); + var instance = getInstance(); + instance.put(key, value); + return getBuilderAfterAdditionalProperty(instance); + } + } + + public static class SchemaMapBuilder implements BaseBuilder, SetterForAdditionalProperties { + private final Map instance; + private static final Set knownKeys = Set.of(); + public Set getKnownKeys() { + return knownKeys; + } + public SchemaMapBuilder() { + this.instance = new LinkedHashMap<>(); + } + public Map build() { + return instance; + } + public Map getInstance() { + return instance; + } + public SchemaMapBuilder getBuilderAfterAdditionalProperty(Map instance) { + return this; + } + } + + + public static class Schema1 extends JsonSchema implements MapSchemaValidator { + private static @Nullable Schema1 instance = null; + + protected Schema1() { + super(new JsonSchemaInfo() + .type(Set.of(Map.class)) + .additionalProperties(AdditionalProperties.class) + ); + } + + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + + public SchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); + for(Map.Entry entry: arg.entrySet()) { + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; + List propertyPathToItem = new ArrayList<>(pathToItem); + propertyPathToItem.add(propertyName); + Object value = entry.getValue(); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + if (!(propertyInstance instanceof String)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + properties.put(propertyName, (String) propertyInstance); + } + FrozenMap castProperties = new FrozenMap<>(properties); + return new SchemaMap(castProperties); + } + + public SchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); + } + + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof Map) { + return validate((Map) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof Map) { + return getNewInstance((Map) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeinlinecomposition/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeinlinecomposition/post/requestbody/content/application~1json/schema.java new file mode 100644 index 00000000000..e2b2682107f --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeinlinecomposition/post/requestbody/content/application~1json/schema.java @@ -0,0 +1,280 @@ +package org.openapijsonschematools.client.paths.fakeinlinecomposition.post.requestbody.content.application~1json; +import java.time.LocalDate; +import java.time.ZonedDateTime; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.UUID; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; +import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; +import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; +import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.FrozenList; +import org.openapijsonschematools.client.schemas.validation.FrozenMap; +import org.openapijsonschematools.client.schemas.validation.JsonSchema; +import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; +import org.openapijsonschematools.client.schemas.validation.ListSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.NullSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.NumberSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; +import org.openapijsonschematools.client.schemas.validation.StringSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; + +public class Schema { + // nest classes so all schemas and input/output classes can be public + + + public static class Schema0 extends JsonSchema implements StringSchemaValidator { + private static @Nullable Schema0 instance = null; + + protected Schema0() { + super(new JsonSchemaInfo() + .type(Set.of( + String.class + )) + .minLength(1) + ); + } + + public static Schema0 getInstance() { + if (instance == null) { + instance = new Schema0(); + } + return instance; + } + + @Override + public String validate(String arg, SchemaConfiguration configuration) throws ValidationException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + String castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof String) { + return validate((String) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof String) { + return getNewInstance((String) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + + public static class Schema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + private static @Nullable Schema1 instance = null; + + protected Schema1() { + super(new JsonSchemaInfo() + .allOf(List.of( + Schema0.class + )) + ); + } + + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + + @Override + public Void validate(Void arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0]"); + Void castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + PathToSchemasMap validatedPathToSchemas = new PathToSchemasMap(); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, validatedPathToSchemas, new LinkedHashSet<>()); + getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + + @Override + public boolean validate(boolean arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0]"); + boolean castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + PathToSchemasMap validatedPathToSchemas = new PathToSchemasMap(); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, validatedPathToSchemas, new LinkedHashSet<>()); + getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + + @Override + public Number validate(Number arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0]"); + Number castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + PathToSchemasMap validatedPathToSchemas = new PathToSchemasMap(); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, validatedPathToSchemas, new LinkedHashSet<>()); + getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + + public int validate(int arg, SchemaConfiguration configuration) { + return (int) validate((Number) arg, configuration); + } + + public long validate(long arg, SchemaConfiguration configuration) { + return (long) validate((Number) arg, configuration); + } + + public float validate(float arg, SchemaConfiguration configuration) { + return (float) validate((Number) arg, configuration); + } + + public double validate(double arg, SchemaConfiguration configuration) { + return (double) validate((Number) arg, configuration); + } + + @Override + public String validate(String arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0]"); + String castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + PathToSchemasMap validatedPathToSchemas = new PathToSchemasMap(); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, validatedPathToSchemas, new LinkedHashSet<>()); + getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + + public String validate(LocalDate arg, SchemaConfiguration configuration) throws ValidationException { + return validate(arg.toString(), configuration); + } + + public String validate(ZonedDateTime arg, SchemaConfiguration configuration) throws ValidationException { + return validate(arg.toString(), configuration); + } + + public String validate(UUID arg, SchemaConfiguration configuration) throws ValidationException { + return validate(arg.toString(), configuration); + } + + @Override + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); + int i = 0; + for (Object item: arg) { + List itemPathToItem = new ArrayList<>(pathToItem); + itemPathToItem.add(i); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + items.add(itemInstance); + i += 1; + } + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); + return newInstanceItems; + } + + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + List castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); + } + + @Override + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); + for(Map.Entry entry: arg.entrySet()) { + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; + List propertyPathToItem = new ArrayList<>(pathToItem); + propertyPathToItem.add(propertyName); + Object value = entry.getValue(); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + properties.put(propertyName, propertyInstance); + } + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); + return castProperties; + } + + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0]"); + Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + PathToSchemasMap validatedPathToSchemas = new PathToSchemasMap(); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, validatedPathToSchemas, new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); + } + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg == null) { + return validate((Void) null, configuration); + } else if (arg instanceof Boolean) { + boolean boolArg = (Boolean) arg; + return validate(boolArg, configuration); + } else if (arg instanceof Number) { + return validate((Number) arg, configuration); + } else if (arg instanceof String) { + return validate((String) arg, configuration); + } else if (arg instanceof List) { + return validate((List) arg, configuration); + } else if (arg instanceof Map) { + return validate((Map) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg == null) { + return getNewInstance((Void) null, pathToItem, pathToSchemas); + } else if (arg instanceof Boolean) { + boolean boolArg = (Boolean) arg; + return getNewInstance(boolArg, pathToItem, pathToSchemas); + } else if (arg instanceof Number) { + return getNewInstance((Number) arg, pathToItem, pathToSchemas); + } else if (arg instanceof String) { + return getNewInstance((String) arg, pathToItem, pathToSchemas); + } else if (arg instanceof List) { + return getNewInstance((List) arg, pathToItem, pathToSchemas); + } else if (arg instanceof Map) { + return getNewInstance((Map) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeinlinecomposition/post/requestbody/content/multipart~1form-data/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeinlinecomposition/post/requestbody/content/multipart~1form-data/schema.java new file mode 100644 index 00000000000..589d4278c0c --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeinlinecomposition/post/requestbody/content/multipart~1form-data/schema.java @@ -0,0 +1,460 @@ +package org.openapijsonschematools.client.paths.fakeinlinecomposition.post.requestbody.content.multipart~1form-data; +import java.time.LocalDate; +import java.time.ZonedDateTime; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.UUID; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; +import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; +import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.BaseBuilder; +import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; +import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.FrozenList; +import org.openapijsonschematools.client.schemas.validation.FrozenMap; +import org.openapijsonschematools.client.schemas.validation.JsonSchema; +import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; +import org.openapijsonschematools.client.schemas.validation.ListSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.NullSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.NumberSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; +import org.openapijsonschematools.client.schemas.validation.PropertyEntry; +import org.openapijsonschematools.client.schemas.validation.StringSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; + +public class Schema { + // nest classes so all schemas and input/output classes can be public + + + public static class Schema0 extends JsonSchema implements StringSchemaValidator { + private static @Nullable Schema0 instance = null; + + protected Schema0() { + super(new JsonSchemaInfo() + .type(Set.of( + String.class + )) + .minLength(1) + ); + } + + public static Schema0 getInstance() { + if (instance == null) { + instance = new Schema0(); + } + return instance; + } + + @Override + public String validate(String arg, SchemaConfiguration configuration) throws ValidationException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + String castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof String) { + return validate((String) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof String) { + return getNewInstance((String) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + + public static class SomeProp extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { + private static @Nullable SomeProp instance = null; + + protected SomeProp() { + super(new JsonSchemaInfo() + .allOf(List.of( + Schema0.class + )) + ); + } + + public static SomeProp getInstance() { + if (instance == null) { + instance = new SomeProp(); + } + return instance; + } + + @Override + public Void validate(Void arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0]"); + Void castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + PathToSchemasMap validatedPathToSchemas = new PathToSchemasMap(); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, validatedPathToSchemas, new LinkedHashSet<>()); + getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + + @Override + public boolean validate(boolean arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0]"); + boolean castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + PathToSchemasMap validatedPathToSchemas = new PathToSchemasMap(); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, validatedPathToSchemas, new LinkedHashSet<>()); + getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + + @Override + public Number validate(Number arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0]"); + Number castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + PathToSchemasMap validatedPathToSchemas = new PathToSchemasMap(); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, validatedPathToSchemas, new LinkedHashSet<>()); + getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + + public int validate(int arg, SchemaConfiguration configuration) { + return (int) validate((Number) arg, configuration); + } + + public long validate(long arg, SchemaConfiguration configuration) { + return (long) validate((Number) arg, configuration); + } + + public float validate(float arg, SchemaConfiguration configuration) { + return (float) validate((Number) arg, configuration); + } + + public double validate(double arg, SchemaConfiguration configuration) { + return (double) validate((Number) arg, configuration); + } + + @Override + public String validate(String arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0]"); + String castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + PathToSchemasMap validatedPathToSchemas = new PathToSchemasMap(); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, validatedPathToSchemas, new LinkedHashSet<>()); + getPathToSchemas(this, castArg, validationMetadata, pathSet); + return castArg; + } + + public String validate(LocalDate arg, SchemaConfiguration configuration) throws ValidationException { + return validate(arg.toString(), configuration); + } + + public String validate(ZonedDateTime arg, SchemaConfiguration configuration) throws ValidationException { + return validate(arg.toString(), configuration); + } + + public String validate(UUID arg, SchemaConfiguration configuration) throws ValidationException { + return validate(arg.toString(), configuration); + } + + @Override + public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List<@Nullable Object> items = new ArrayList<>(); + int i = 0; + for (Object item: arg) { + List itemPathToItem = new ArrayList<>(pathToItem); + itemPathToItem.add(i); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + items.add(itemInstance); + i += 1; + } + FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); + return newInstanceItems; + } + + public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + List castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); + } + + @Override + public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); + for(Map.Entry entry: arg.entrySet()) { + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; + List propertyPathToItem = new ArrayList<>(pathToItem); + propertyPathToItem.add(propertyName); + Object value = entry.getValue(); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + properties.put(propertyName, propertyInstance); + } + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); + return castProperties; + } + + public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0]"); + Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + PathToSchemasMap validatedPathToSchemas = new PathToSchemasMap(); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, validatedPathToSchemas, new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); + } + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg == null) { + return validate((Void) null, configuration); + } else if (arg instanceof Boolean) { + boolean boolArg = (Boolean) arg; + return validate(boolArg, configuration); + } else if (arg instanceof Number) { + return validate((Number) arg, configuration); + } else if (arg instanceof String) { + return validate((String) arg, configuration); + } else if (arg instanceof List) { + return validate((List) arg, configuration); + } else if (arg instanceof Map) { + return validate((Map) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg == null) { + return getNewInstance((Void) null, pathToItem, pathToSchemas); + } else if (arg instanceof Boolean) { + boolean boolArg = (Boolean) arg; + return getNewInstance(boolArg, pathToItem, pathToSchemas); + } else if (arg instanceof Number) { + return getNewInstance((Number) arg, pathToItem, pathToSchemas); + } else if (arg instanceof String) { + return getNewInstance((String) arg, pathToItem, pathToSchemas); + } else if (arg instanceof List) { + return getNewInstance((List) arg, pathToItem, pathToSchemas); + } else if (arg instanceof Map) { + return getNewInstance((Map) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + + public static class SchemaMap extends FrozenMap<@Nullable Object> { + protected SchemaMap(FrozenMap<@Nullable Object> m) { + super(m); + } + public static final Set requiredKeys = Set.of(); + public static final Set optionalKeys = Set.of( + "someProp" + ); + public static SchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + return Schema1.getInstance().validate(arg, configuration); + } + + public @Nullable Object someProp() throws UnsetPropertyException { + return getOrThrow("someProp"); + } + + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { + throwIfKeyKnown(name, requiredKeys, optionalKeys); + throwIfKeyNotPresent(name); + return get(name); + } + } + + public interface SetterForSomeProp { + Map getInstance(); + T getBuilderAfterSomeProp(Map instance); + + default T someProp(Void value) { + var instance = getInstance(); + instance.put("someProp", null); + return getBuilderAfterSomeProp(instance); + } + + default T someProp(boolean value) { + var instance = getInstance(); + instance.put("someProp", value); + return getBuilderAfterSomeProp(instance); + } + + default T someProp(String value) { + var instance = getInstance(); + instance.put("someProp", value); + return getBuilderAfterSomeProp(instance); + } + + default T someProp(int value) { + var instance = getInstance(); + instance.put("someProp", value); + return getBuilderAfterSomeProp(instance); + } + + default T someProp(float value) { + var instance = getInstance(); + instance.put("someProp", value); + return getBuilderAfterSomeProp(instance); + } + + default T someProp(long value) { + var instance = getInstance(); + instance.put("someProp", value); + return getBuilderAfterSomeProp(instance); + } + + default T someProp(double value) { + var instance = getInstance(); + instance.put("someProp", value); + return getBuilderAfterSomeProp(instance); + } + + default T someProp(List value) { + var instance = getInstance(); + instance.put("someProp", value); + return getBuilderAfterSomeProp(instance); + } + + default T someProp(Map value) { + var instance = getInstance(); + instance.put("someProp", value); + return getBuilderAfterSomeProp(instance); + } + } + + public static class SchemaMapBuilder extends UnsetAddPropsSetter implements BaseBuilder<@Nullable Object>, SetterForSomeProp { + private final Map instance; + private static final Set knownKeys = Set.of( + "someProp" + ); + public Set getKnownKeys() { + return knownKeys; + } + public SchemaMapBuilder() { + this.instance = new LinkedHashMap<>(); + } + public Map build() { + return instance; + } + public Map getInstance() { + return instance; + } + public SchemaMapBuilder getBuilderAfterSomeProp(Map instance) { + return this; + } + public SchemaMapBuilder getBuilderAfterAdditionalProperty(Map instance) { + return this; + } + } + + + public static class Schema1 extends JsonSchema implements MapSchemaValidator { + private static @Nullable Schema1 instance = null; + + protected Schema1() { + super(new JsonSchemaInfo() + .type(Set.of(Map.class)) + .properties(Map.ofEntries( + new PropertyEntry("someProp", SomeProp.class) + )) + ); + } + + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + + public SchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); + for(Map.Entry entry: arg.entrySet()) { + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; + List propertyPathToItem = new ArrayList<>(pathToItem); + propertyPathToItem.add(propertyName); + Object value = entry.getValue(); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + properties.put(propertyName, propertyInstance); + } + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); + return new SchemaMap(castProperties); + } + + public SchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); + } + + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof Map) { + return validate((Map) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof Map) { + return getNewInstance((Map) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakejsonformdata/get/requestbody/content/application~1x-www-form-urlencoded/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakejsonformdata/get/requestbody/content/application~1x-www-form-urlencoded/schema.java new file mode 100644 index 00000000000..c24d1b0dbdb --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakejsonformdata/get/requestbody/content/application~1x-www-form-urlencoded/schema.java @@ -0,0 +1,252 @@ +package org.openapijsonschematools.client.paths.fakejsonformdata.get.requestbody.content.application~1x-www-form-urlencoded; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; +import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; +import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.BaseBuilder; +import org.openapijsonschematools.client.schemas.StringJsonSchema; +import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; +import org.openapijsonschematools.client.schemas.validation.FrozenMap; +import org.openapijsonschematools.client.schemas.validation.JsonSchema; +import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; +import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; +import org.openapijsonschematools.client.schemas.validation.PropertyEntry; +import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; + +public class Schema { + // nest classes so all schemas and input/output classes can be public + + + public static class Param extends StringJsonSchema { + private static @Nullable Param instance = null; + public static Param getInstance() { + if (instance == null) { + instance = new Param(); + } + return instance; + } + } + + + public static class Param2 extends StringJsonSchema { + private static @Nullable Param2 instance = null; + public static Param2 getInstance() { + if (instance == null) { + instance = new Param2(); + } + return instance; + } + } + + + public static class SchemaMap extends FrozenMap<@Nullable Object> { + protected SchemaMap(FrozenMap<@Nullable Object> m) { + super(m); + } + public static final Set requiredKeys = Set.of( + "param", + "param2" + ); + public static final Set optionalKeys = Set.of(); + public static SchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + return Schema1.getInstance().validate(arg, configuration); + } + + public String param() { + @Nullable Object value = get("param"); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for param"); + } + return (String) value; + } + + public String param2() { + @Nullable Object value = get("param2"); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for param2"); + } + return (String) value; + } + + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { + throwIfKeyKnown(name, requiredKeys, optionalKeys); + throwIfKeyNotPresent(name); + return get(name); + } + } + + public interface SetterForParam { + Map getInstance(); + T getBuilderAfterParam(Map instance); + + default T param(String value) { + var instance = getInstance(); + instance.put("param", value); + return getBuilderAfterParam(instance); + } + } + + public interface SetterForParam2 { + Map getInstance(); + T getBuilderAfterParam2(Map instance); + + default T param2(String value) { + var instance = getInstance(); + instance.put("param2", value); + return getBuilderAfterParam2(instance); + } + } + + public static class SchemaMap00Builder extends UnsetAddPropsSetter implements BaseBuilder<@Nullable Object> { + private final Map instance; + private static final Set knownKeys = Set.of( + "param", + "param2" + ); + public Set getKnownKeys() { + return knownKeys; + } + public SchemaMap00Builder(Map instance) { + this.instance = instance; + } + public Map build() { + return instance; + } + public Map getInstance() { + return instance; + } + public SchemaMap00Builder getBuilderAfterAdditionalProperty(Map instance) { + return this; + } + } + + public static class SchemaMap01Builder implements SetterForParam2 { + private final Map instance; + public SchemaMap01Builder(Map instance) { + this.instance = instance; + } + public Map getInstance() { + return instance; + } + public SchemaMap00Builder getBuilderAfterParam2(Map instance) { + return new SchemaMap00Builder(instance); + } + } + + public static class SchemaMap10Builder implements SetterForParam { + private final Map instance; + public SchemaMap10Builder(Map instance) { + this.instance = instance; + } + public Map getInstance() { + return instance; + } + public SchemaMap00Builder getBuilderAfterParam(Map instance) { + return new SchemaMap00Builder(instance); + } + } + + public static class SchemaMapBuilder implements SetterForParam, SetterForParam2 { + private final Map instance; + public SchemaMapBuilder() { + this.instance = new LinkedHashMap<>(); + } + public Map getInstance() { + return instance; + } + public SchemaMap01Builder getBuilderAfterParam(Map instance) { + return new SchemaMap01Builder(instance); + } + public SchemaMap10Builder getBuilderAfterParam2(Map instance) { + return new SchemaMap10Builder(instance); + } + } + + + public static class Schema1 extends JsonSchema implements MapSchemaValidator { + private static @Nullable Schema1 instance = null; + + protected Schema1() { + super(new JsonSchemaInfo() + .type(Set.of(Map.class)) + .properties(Map.ofEntries( + new PropertyEntry("param", Param.class), + new PropertyEntry("param2", Param2.class) + )) + .required(Set.of( + "param", + "param2" + )) + ); + } + + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + + public SchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); + for(Map.Entry entry: arg.entrySet()) { + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; + List propertyPathToItem = new ArrayList<>(pathToItem); + propertyPathToItem.add(propertyName); + Object value = entry.getValue(); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + properties.put(propertyName, propertyInstance); + } + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); + return new SchemaMap(castProperties); + } + + public SchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); + } + + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof Map) { + return validate((Map) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof Map) { + return getNewInstance((Map) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakejsonpatch/patch/requestbody/content/application~1json-patch+json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakejsonpatch/patch/requestbody/content/application~1json-patch+json/schema.java new file mode 100644 index 00000000000..2af6dde51a6 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakejsonpatch/patch/requestbody/content/application~1json-patch+json/schema.java @@ -0,0 +1,19 @@ +package org.openapijsonschematools.client.paths.fakejsonpatch.patch.requestbody.content.application~1json-patch+json; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.components.schemas.JSONPatchRequest; + +public class Schema extends JSONPatchRequest { + // $refed class + + + public static class Schema1 extends JSONPatchRequest1 { + private static @Nullable Schema1 instance = null; + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + } +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakejsonwithcharset/post/requestbody/content/application~1json; charset=utf-8/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakejsonwithcharset/post/requestbody/content/application~1json; charset=utf-8/schema.java new file mode 100644 index 00000000000..2fa66f01873 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakejsonwithcharset/post/requestbody/content/application~1json; charset=utf-8/schema.java @@ -0,0 +1,19 @@ +package org.openapijsonschematools.client.paths.fakejsonwithcharset.post.requestbody.content.application~1json; charset=utf-8; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.schemas.AnyTypeJsonSchema; + +public class Schema { + // nest classes so all schemas and input/output classes can be public + + + public static class Schema1 extends AnyTypeJsonSchema { + private static @Nullable Schema1 instance = null; + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + } + +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakemultiplerequestbodycontenttypes/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakemultiplerequestbodycontenttypes/post/requestbody/content/application~1json/schema.java new file mode 100644 index 00000000000..e7fa61f0583 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakemultiplerequestbodycontenttypes/post/requestbody/content/application~1json/schema.java @@ -0,0 +1,178 @@ +package org.openapijsonschematools.client.paths.fakemultiplerequestbodycontenttypes.post.requestbody.content.application~1json; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; +import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; +import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.BaseBuilder; +import org.openapijsonschematools.client.schemas.StringJsonSchema; +import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; +import org.openapijsonschematools.client.schemas.validation.FrozenMap; +import org.openapijsonschematools.client.schemas.validation.JsonSchema; +import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; +import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; +import org.openapijsonschematools.client.schemas.validation.PropertyEntry; +import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; + +public class Schema { + // nest classes so all schemas and input/output classes can be public + + + public static class A extends StringJsonSchema { + private static @Nullable A instance = null; + public static A getInstance() { + if (instance == null) { + instance = new A(); + } + return instance; + } + } + + + public static class SchemaMap extends FrozenMap<@Nullable Object> { + protected SchemaMap(FrozenMap<@Nullable Object> m) { + super(m); + } + public static final Set requiredKeys = Set.of(); + public static final Set optionalKeys = Set.of( + "a" + ); + public static SchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + return Schema1.getInstance().validate(arg, configuration); + } + + public String a() throws UnsetPropertyException { + String key = "a"; + throwIfKeyNotPresent(key); + @Nullable Object value = get(key); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for a"); + } + return (String) value; + } + + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { + throwIfKeyKnown(name, requiredKeys, optionalKeys); + throwIfKeyNotPresent(name); + return get(name); + } + } + + public interface SetterForA { + Map getInstance(); + T getBuilderAfterA(Map instance); + + default T a(String value) { + var instance = getInstance(); + instance.put("a", value); + return getBuilderAfterA(instance); + } + } + + public static class SchemaMapBuilder extends UnsetAddPropsSetter implements BaseBuilder<@Nullable Object>, SetterForA { + private final Map instance; + private static final Set knownKeys = Set.of( + "a" + ); + public Set getKnownKeys() { + return knownKeys; + } + public SchemaMapBuilder() { + this.instance = new LinkedHashMap<>(); + } + public Map build() { + return instance; + } + public Map getInstance() { + return instance; + } + public SchemaMapBuilder getBuilderAfterA(Map instance) { + return this; + } + public SchemaMapBuilder getBuilderAfterAdditionalProperty(Map instance) { + return this; + } + } + + + public static class Schema1 extends JsonSchema implements MapSchemaValidator { + private static @Nullable Schema1 instance = null; + + protected Schema1() { + super(new JsonSchemaInfo() + .type(Set.of(Map.class)) + .properties(Map.ofEntries( + new PropertyEntry("a", A.class) + )) + ); + } + + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + + public SchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); + for(Map.Entry entry: arg.entrySet()) { + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; + List propertyPathToItem = new ArrayList<>(pathToItem); + propertyPathToItem.add(propertyName); + Object value = entry.getValue(); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + properties.put(propertyName, propertyInstance); + } + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); + return new SchemaMap(castProperties); + } + + public SchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); + } + + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof Map) { + return validate((Map) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof Map) { + return getNewInstance((Map) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakemultiplerequestbodycontenttypes/post/requestbody/content/multipart~1form-data/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakemultiplerequestbodycontenttypes/post/requestbody/content/multipart~1form-data/schema.java new file mode 100644 index 00000000000..99b4af98078 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakemultiplerequestbodycontenttypes/post/requestbody/content/multipart~1form-data/schema.java @@ -0,0 +1,178 @@ +package org.openapijsonschematools.client.paths.fakemultiplerequestbodycontenttypes.post.requestbody.content.multipart~1form-data; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; +import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; +import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.BaseBuilder; +import org.openapijsonschematools.client.schemas.StringJsonSchema; +import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; +import org.openapijsonschematools.client.schemas.validation.FrozenMap; +import org.openapijsonschematools.client.schemas.validation.JsonSchema; +import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; +import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; +import org.openapijsonschematools.client.schemas.validation.PropertyEntry; +import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; + +public class Schema { + // nest classes so all schemas and input/output classes can be public + + + public static class B extends StringJsonSchema { + private static @Nullable B instance = null; + public static B getInstance() { + if (instance == null) { + instance = new B(); + } + return instance; + } + } + + + public static class SchemaMap extends FrozenMap<@Nullable Object> { + protected SchemaMap(FrozenMap<@Nullable Object> m) { + super(m); + } + public static final Set requiredKeys = Set.of(); + public static final Set optionalKeys = Set.of( + "b" + ); + public static SchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + return Schema1.getInstance().validate(arg, configuration); + } + + public String b() throws UnsetPropertyException { + String key = "b"; + throwIfKeyNotPresent(key); + @Nullable Object value = get(key); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for b"); + } + return (String) value; + } + + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { + throwIfKeyKnown(name, requiredKeys, optionalKeys); + throwIfKeyNotPresent(name); + return get(name); + } + } + + public interface SetterForB { + Map getInstance(); + T getBuilderAfterB(Map instance); + + default T b(String value) { + var instance = getInstance(); + instance.put("b", value); + return getBuilderAfterB(instance); + } + } + + public static class SchemaMapBuilder extends UnsetAddPropsSetter implements BaseBuilder<@Nullable Object>, SetterForB { + private final Map instance; + private static final Set knownKeys = Set.of( + "b" + ); + public Set getKnownKeys() { + return knownKeys; + } + public SchemaMapBuilder() { + this.instance = new LinkedHashMap<>(); + } + public Map build() { + return instance; + } + public Map getInstance() { + return instance; + } + public SchemaMapBuilder getBuilderAfterB(Map instance) { + return this; + } + public SchemaMapBuilder getBuilderAfterAdditionalProperty(Map instance) { + return this; + } + } + + + public static class Schema1 extends JsonSchema implements MapSchemaValidator { + private static @Nullable Schema1 instance = null; + + protected Schema1() { + super(new JsonSchemaInfo() + .type(Set.of(Map.class)) + .properties(Map.ofEntries( + new PropertyEntry("b", B.class) + )) + ); + } + + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + + public SchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); + for(Map.Entry entry: arg.entrySet()) { + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; + List propertyPathToItem = new ArrayList<>(pathToItem); + propertyPathToItem.add(propertyName); + Object value = entry.getValue(); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + properties.put(propertyName, propertyInstance); + } + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); + return new SchemaMap(castProperties); + } + + public SchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); + } + + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof Map) { + return validate((Map) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof Map) { + return getNewInstance((Map) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeparametercollisions1ababselfab/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeparametercollisions1ababselfab/post/requestbody/content/application~1json/schema.java new file mode 100644 index 00000000000..f894fbce42e --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeparametercollisions1ababselfab/post/requestbody/content/application~1json/schema.java @@ -0,0 +1,19 @@ +package org.openapijsonschematools.client.paths.fakeparametercollisions1ababselfab.post.requestbody.content.application~1json; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.schemas.AnyTypeJsonSchema; + +public class Schema { + // nest classes so all schemas and input/output classes can be public + + + public static class Schema1 extends AnyTypeJsonSchema { + private static @Nullable Schema1 instance = null; + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + } + +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakepemcontenttype/get/requestbody/content/application~1x-pem-file/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakepemcontenttype/get/requestbody/content/application~1x-pem-file/schema.java new file mode 100644 index 00000000000..fb6dc292870 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakepemcontenttype/get/requestbody/content/application~1x-pem-file/schema.java @@ -0,0 +1,19 @@ +package org.openapijsonschematools.client.paths.fakepemcontenttype.get.requestbody.content.application~1x-pem-file; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.schemas.StringJsonSchema; + +public class Schema { + // nest classes so all schemas and input/output classes can be public + + + public static class Schema1 extends StringJsonSchema { + private static @Nullable Schema1 instance = null; + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + } + +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakepetiduploadimagewithrequiredfile/post/requestbody/content/multipart~1form-data/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakepetiduploadimagewithrequiredfile/post/requestbody/content/multipart~1form-data/schema.java new file mode 100644 index 00000000000..36775e5a490 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakepetiduploadimagewithrequiredfile/post/requestbody/content/multipart~1form-data/schema.java @@ -0,0 +1,229 @@ +package org.openapijsonschematools.client.paths.fakepetiduploadimagewithrequiredfile.post.requestbody.content.multipart~1form-data; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; +import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; +import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.BaseBuilder; +import org.openapijsonschematools.client.schemas.StringJsonSchema; +import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; +import org.openapijsonschematools.client.schemas.validation.FrozenMap; +import org.openapijsonschematools.client.schemas.validation.JsonSchema; +import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; +import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; +import org.openapijsonschematools.client.schemas.validation.PropertyEntry; +import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; + +public class Schema { + // nest classes so all schemas and input/output classes can be public + + + public static class AdditionalMetadata extends StringJsonSchema { + private static @Nullable AdditionalMetadata instance = null; + public static AdditionalMetadata getInstance() { + if (instance == null) { + instance = new AdditionalMetadata(); + } + return instance; + } + } + + + public static class RequiredFile extends StringJsonSchema { + // BinarySchema + private static @Nullable RequiredFile instance = null; + public static RequiredFile getInstance() { + if (instance == null) { + instance = new RequiredFile(); + } + return instance; + } + } + + + public static class SchemaMap extends FrozenMap<@Nullable Object> { + protected SchemaMap(FrozenMap<@Nullable Object> m) { + super(m); + } + public static final Set requiredKeys = Set.of( + "requiredFile" + ); + public static final Set optionalKeys = Set.of( + "additionalMetadata" + ); + public static SchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + return Schema1.getInstance().validate(arg, configuration); + } + + public String requiredFile() { + @Nullable Object value = get("requiredFile"); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for requiredFile"); + } + return (String) value; + } + + public String additionalMetadata() throws UnsetPropertyException { + String key = "additionalMetadata"; + throwIfKeyNotPresent(key); + @Nullable Object value = get(key); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for additionalMetadata"); + } + return (String) value; + } + + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { + throwIfKeyKnown(name, requiredKeys, optionalKeys); + throwIfKeyNotPresent(name); + return get(name); + } + } + + public interface SetterForRequiredFile { + Map getInstance(); + T getBuilderAfterRequiredFile(Map instance); + + default T requiredFile(String value) { + var instance = getInstance(); + instance.put("requiredFile", value); + return getBuilderAfterRequiredFile(instance); + } + } + + public interface SetterForAdditionalMetadata { + Map getInstance(); + T getBuilderAfterAdditionalMetadata(Map instance); + + default T additionalMetadata(String value) { + var instance = getInstance(); + instance.put("additionalMetadata", value); + return getBuilderAfterAdditionalMetadata(instance); + } + } + + public static class SchemaMap0Builder extends UnsetAddPropsSetter implements BaseBuilder<@Nullable Object>, SetterForAdditionalMetadata { + private final Map instance; + private static final Set knownKeys = Set.of( + "requiredFile", + "additionalMetadata" + ); + public Set getKnownKeys() { + return knownKeys; + } + public SchemaMap0Builder(Map instance) { + this.instance = instance; + } + public Map build() { + return instance; + } + public Map getInstance() { + return instance; + } + public SchemaMap0Builder getBuilderAfterAdditionalMetadata(Map instance) { + return this; + } + public SchemaMap0Builder getBuilderAfterAdditionalProperty(Map instance) { + return this; + } + } + + public static class SchemaMapBuilder implements SetterForRequiredFile { + private final Map instance; + public SchemaMapBuilder() { + this.instance = new LinkedHashMap<>(); + } + public Map getInstance() { + return instance; + } + public SchemaMap0Builder getBuilderAfterRequiredFile(Map instance) { + return new SchemaMap0Builder(instance); + } + } + + + public static class Schema1 extends JsonSchema implements MapSchemaValidator { + private static @Nullable Schema1 instance = null; + + protected Schema1() { + super(new JsonSchemaInfo() + .type(Set.of(Map.class)) + .properties(Map.ofEntries( + new PropertyEntry("additionalMetadata", AdditionalMetadata.class), + new PropertyEntry("requiredFile", RequiredFile.class) + )) + .required(Set.of( + "requiredFile" + )) + ); + } + + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + + public SchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); + for(Map.Entry entry: arg.entrySet()) { + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; + List propertyPathToItem = new ArrayList<>(pathToItem); + propertyPathToItem.add(propertyName); + Object value = entry.getValue(); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + properties.put(propertyName, propertyInstance); + } + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); + return new SchemaMap(castProperties); + } + + public SchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); + } + + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof Map) { + return validate((Map) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof Map) { + return getNewInstance((Map) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsarraymodel/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsarraymodel/post/requestbody/content/application~1json/schema.java new file mode 100644 index 00000000000..849da921912 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsarraymodel/post/requestbody/content/application~1json/schema.java @@ -0,0 +1,19 @@ +package org.openapijsonschematools.client.paths.fakerefsarraymodel.post.requestbody.content.application~1json; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.components.schemas.AnimalFarm; + +public class Schema extends AnimalFarm { + // $refed class + + + public static class Schema1 extends AnimalFarm1 { + private static @Nullable Schema1 instance = null; + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + } +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsarrayofenums/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsarrayofenums/post/requestbody/content/application~1json/schema.java new file mode 100644 index 00000000000..f707b458d8b --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsarrayofenums/post/requestbody/content/application~1json/schema.java @@ -0,0 +1,19 @@ +package org.openapijsonschematools.client.paths.fakerefsarrayofenums.post.requestbody.content.application~1json; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.components.schemas.ArrayOfEnums; + +public class Schema extends ArrayOfEnums { + // $refed class + + + public static class Schema1 extends ArrayOfEnums1 { + private static @Nullable Schema1 instance = null; + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + } +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsboolean/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsboolean/post/requestbody/content/application~1json/schema.java new file mode 100644 index 00000000000..8ca249e844f --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsboolean/post/requestbody/content/application~1json/schema.java @@ -0,0 +1,19 @@ +package org.openapijsonschematools.client.paths.fakerefsboolean.post.requestbody.content.application~1json; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.components.schemas.BooleanSchema; + +public class Schema extends BooleanSchema { + // $refed class + + + public static class Schema1 extends BooleanSchema1 { + private static @Nullable Schema1 instance = null; + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + } +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefscomposedoneofnumberwithvalidations/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefscomposedoneofnumberwithvalidations/post/requestbody/content/application~1json/schema.java new file mode 100644 index 00000000000..76242966da3 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefscomposedoneofnumberwithvalidations/post/requestbody/content/application~1json/schema.java @@ -0,0 +1,19 @@ +package org.openapijsonschematools.client.paths.fakerefscomposedoneofnumberwithvalidations.post.requestbody.content.application~1json; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.components.schemas.ComposedOneOfDifferentTypes; + +public class Schema extends ComposedOneOfDifferentTypes { + // $refed class + + + public static class Schema1 extends ComposedOneOfDifferentTypes1 { + private static @Nullable Schema1 instance = null; + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + } +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsenum/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsenum/post/requestbody/content/application~1json/schema.java new file mode 100644 index 00000000000..22e8580a10b --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsenum/post/requestbody/content/application~1json/schema.java @@ -0,0 +1,19 @@ +package org.openapijsonschematools.client.paths.fakerefsenum.post.requestbody.content.application~1json; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.components.schemas.StringEnum; + +public class Schema extends StringEnum { + // $refed class + + + public static class Schema1 extends StringEnum1 { + private static @Nullable Schema1 instance = null; + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + } +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsmammal/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsmammal/post/requestbody/content/application~1json/schema.java new file mode 100644 index 00000000000..42353c30024 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsmammal/post/requestbody/content/application~1json/schema.java @@ -0,0 +1,19 @@ +package org.openapijsonschematools.client.paths.fakerefsmammal.post.requestbody.content.application~1json; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.components.schemas.Mammal; + +public class Schema extends Mammal { + // $refed class + + + public static class Schema1 extends Mammal1 { + private static @Nullable Schema1 instance = null; + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + } +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsnumber/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsnumber/post/requestbody/content/application~1json/schema.java new file mode 100644 index 00000000000..5481ce53783 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsnumber/post/requestbody/content/application~1json/schema.java @@ -0,0 +1,19 @@ +package org.openapijsonschematools.client.paths.fakerefsnumber.post.requestbody.content.application~1json; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.components.schemas.NumberWithValidations; + +public class Schema extends NumberWithValidations { + // $refed class + + + public static class Schema1 extends NumberWithValidations1 { + private static @Nullable Schema1 instance = null; + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + } +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsobjectmodelwithrefprops/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsobjectmodelwithrefprops/post/requestbody/content/application~1json/schema.java new file mode 100644 index 00000000000..a9d97ee8e93 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsobjectmodelwithrefprops/post/requestbody/content/application~1json/schema.java @@ -0,0 +1,19 @@ +package org.openapijsonschematools.client.paths.fakerefsobjectmodelwithrefprops.post.requestbody.content.application~1json; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.components.schemas.ObjectModelWithRefProps; + +public class Schema extends ObjectModelWithRefProps { + // $refed class + + + public static class Schema1 extends ObjectModelWithRefProps1 { + private static @Nullable Schema1 instance = null; + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + } +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsstring/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsstring/post/requestbody/content/application~1json/schema.java new file mode 100644 index 00000000000..b9c6891a65e --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsstring/post/requestbody/content/application~1json/schema.java @@ -0,0 +1,19 @@ +package org.openapijsonschematools.client.paths.fakerefsstring.post.requestbody.content.application~1json; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.components.schemas.StringSchema; + +public class Schema extends StringSchema { + // $refed class + + + public static class Schema1 extends StringSchema1 { + private static @Nullable Schema1 instance = null; + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + } +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeuploaddownloadfile/post/requestbody/content/application~1octet-stream/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeuploaddownloadfile/post/requestbody/content/application~1octet-stream/schema.java new file mode 100644 index 00000000000..c82ae1000c7 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeuploaddownloadfile/post/requestbody/content/application~1octet-stream/schema.java @@ -0,0 +1,20 @@ +package org.openapijsonschematools.client.paths.fakeuploaddownloadfile.post.requestbody.content.application~1octet-stream; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.schemas.StringJsonSchema; + +public class Schema { + // nest classes so all schemas and input/output classes can be public + + + public static class Schema1 extends StringJsonSchema { + // BinarySchema + private static @Nullable Schema1 instance = null; + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + } + +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeuploadfile/post/requestbody/content/multipart~1form-data/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeuploadfile/post/requestbody/content/multipart~1form-data/schema.java new file mode 100644 index 00000000000..2f069b0091b --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeuploadfile/post/requestbody/content/multipart~1form-data/schema.java @@ -0,0 +1,229 @@ +package org.openapijsonschematools.client.paths.fakeuploadfile.post.requestbody.content.multipart~1form-data; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; +import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; +import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.BaseBuilder; +import org.openapijsonschematools.client.schemas.StringJsonSchema; +import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; +import org.openapijsonschematools.client.schemas.validation.FrozenMap; +import org.openapijsonschematools.client.schemas.validation.JsonSchema; +import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; +import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; +import org.openapijsonschematools.client.schemas.validation.PropertyEntry; +import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; + +public class Schema { + // nest classes so all schemas and input/output classes can be public + + + public static class AdditionalMetadata extends StringJsonSchema { + private static @Nullable AdditionalMetadata instance = null; + public static AdditionalMetadata getInstance() { + if (instance == null) { + instance = new AdditionalMetadata(); + } + return instance; + } + } + + + public static class File extends StringJsonSchema { + // BinarySchema + private static @Nullable File instance = null; + public static File getInstance() { + if (instance == null) { + instance = new File(); + } + return instance; + } + } + + + public static class SchemaMap extends FrozenMap<@Nullable Object> { + protected SchemaMap(FrozenMap<@Nullable Object> m) { + super(m); + } + public static final Set requiredKeys = Set.of( + "file" + ); + public static final Set optionalKeys = Set.of( + "additionalMetadata" + ); + public static SchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + return Schema1.getInstance().validate(arg, configuration); + } + + public String file() { + @Nullable Object value = get("file"); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for file"); + } + return (String) value; + } + + public String additionalMetadata() throws UnsetPropertyException { + String key = "additionalMetadata"; + throwIfKeyNotPresent(key); + @Nullable Object value = get(key); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for additionalMetadata"); + } + return (String) value; + } + + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { + throwIfKeyKnown(name, requiredKeys, optionalKeys); + throwIfKeyNotPresent(name); + return get(name); + } + } + + public interface SetterForFile { + Map getInstance(); + T getBuilderAfterFile(Map instance); + + default T file(String value) { + var instance = getInstance(); + instance.put("file", value); + return getBuilderAfterFile(instance); + } + } + + public interface SetterForAdditionalMetadata { + Map getInstance(); + T getBuilderAfterAdditionalMetadata(Map instance); + + default T additionalMetadata(String value) { + var instance = getInstance(); + instance.put("additionalMetadata", value); + return getBuilderAfterAdditionalMetadata(instance); + } + } + + public static class SchemaMap0Builder extends UnsetAddPropsSetter implements BaseBuilder<@Nullable Object>, SetterForAdditionalMetadata { + private final Map instance; + private static final Set knownKeys = Set.of( + "file", + "additionalMetadata" + ); + public Set getKnownKeys() { + return knownKeys; + } + public SchemaMap0Builder(Map instance) { + this.instance = instance; + } + public Map build() { + return instance; + } + public Map getInstance() { + return instance; + } + public SchemaMap0Builder getBuilderAfterAdditionalMetadata(Map instance) { + return this; + } + public SchemaMap0Builder getBuilderAfterAdditionalProperty(Map instance) { + return this; + } + } + + public static class SchemaMapBuilder implements SetterForFile { + private final Map instance; + public SchemaMapBuilder() { + this.instance = new LinkedHashMap<>(); + } + public Map getInstance() { + return instance; + } + public SchemaMap0Builder getBuilderAfterFile(Map instance) { + return new SchemaMap0Builder(instance); + } + } + + + public static class Schema1 extends JsonSchema implements MapSchemaValidator { + private static @Nullable Schema1 instance = null; + + protected Schema1() { + super(new JsonSchemaInfo() + .type(Set.of(Map.class)) + .properties(Map.ofEntries( + new PropertyEntry("additionalMetadata", AdditionalMetadata.class), + new PropertyEntry("file", File.class) + )) + .required(Set.of( + "file" + )) + ); + } + + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + + public SchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); + for(Map.Entry entry: arg.entrySet()) { + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; + List propertyPathToItem = new ArrayList<>(pathToItem); + propertyPathToItem.add(propertyName); + Object value = entry.getValue(); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + properties.put(propertyName, propertyInstance); + } + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); + return new SchemaMap(castProperties); + } + + public SchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); + } + + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof Map) { + return validate((Map) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof Map) { + return getNewInstance((Map) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeuploadfiles/post/requestbody/content/multipart~1form-data/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeuploadfiles/post/requestbody/content/multipart~1form-data/schema.java new file mode 100644 index 00000000000..2274b590ae5 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeuploadfiles/post/requestbody/content/multipart~1form-data/schema.java @@ -0,0 +1,279 @@ +package org.openapijsonschematools.client.paths.fakeuploadfiles.post.requestbody.content.multipart~1form-data; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; +import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; +import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.BaseBuilder; +import org.openapijsonschematools.client.schemas.StringJsonSchema; +import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; +import org.openapijsonschematools.client.schemas.validation.FrozenList; +import org.openapijsonschematools.client.schemas.validation.FrozenMap; +import org.openapijsonschematools.client.schemas.validation.JsonSchema; +import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; +import org.openapijsonschematools.client.schemas.validation.ListSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; +import org.openapijsonschematools.client.schemas.validation.PropertyEntry; +import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; + +public class Schema { + // nest classes so all schemas and input/output classes can be public + + + public static class Items extends StringJsonSchema { + // BinarySchema + private static @Nullable Items instance = null; + public static Items getInstance() { + if (instance == null) { + instance = new Items(); + } + return instance; + } + } + + + public static class FilesList extends FrozenList { + protected FilesList(FrozenList m) { + super(m); + } + public static FilesList of(List arg, SchemaConfiguration configuration) throws ValidationException { + return Files.getInstance().validate(arg, configuration); + } + } + + public static class FilesListBuilder { + // class to build List + private final List list; + + public FilesListBuilder() { + list = new ArrayList<>(); + } + + public FilesListBuilder(List list) { + this.list = list; + } + + public FilesListBuilder add(String item) { + list.add(item); + return this; + } + + public List build() { + return list; + } + } + + + public static class Files extends JsonSchema implements ListSchemaValidator { + private static @Nullable Files instance = null; + + protected Files() { + super(new JsonSchemaInfo() + .type(Set.of(List.class)) + .items(Items.class) + ); + } + + public static Files getInstance() { + if (instance == null) { + instance = new Files(); + } + return instance; + } + + @Override + public FilesList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { + List items = new ArrayList<>(); + int i = 0; + for (Object item: arg) { + List itemPathToItem = new ArrayList<>(pathToItem); + itemPathToItem.add(i); + LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); + if (!(itemInstance instanceof String)) { + throw new InvalidTypeException("Invalid instantiated value"); + } + items.add((String) itemInstance); + i += 1; + } + FrozenList newInstanceItems = new FrozenList<>(items); + return new FilesList(newInstanceItems); + } + + public FilesList validate(List arg, SchemaConfiguration configuration) throws ValidationException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + List castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); + } + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof List) { + return validate((List) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof List) { + return getNewInstance((List) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + + public static class SchemaMap extends FrozenMap<@Nullable Object> { + protected SchemaMap(FrozenMap<@Nullable Object> m) { + super(m); + } + public static final Set requiredKeys = Set.of(); + public static final Set optionalKeys = Set.of( + "files" + ); + public static SchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + return Schema1.getInstance().validate(arg, configuration); + } + + public FilesList files() throws UnsetPropertyException { + String key = "files"; + throwIfKeyNotPresent(key); + @Nullable Object value = get(key); + if (!(value instanceof FilesList)) { + throw new InvalidTypeException("Invalid value stored for files"); + } + return (FilesList) value; + } + + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { + throwIfKeyKnown(name, requiredKeys, optionalKeys); + throwIfKeyNotPresent(name); + return get(name); + } + } + + public interface SetterForFiles { + Map getInstance(); + T getBuilderAfterFiles(Map instance); + + default T files(List value) { + var instance = getInstance(); + instance.put("files", value); + return getBuilderAfterFiles(instance); + } + } + + public static class SchemaMapBuilder extends UnsetAddPropsSetter implements BaseBuilder<@Nullable Object>, SetterForFiles { + private final Map instance; + private static final Set knownKeys = Set.of( + "files" + ); + public Set getKnownKeys() { + return knownKeys; + } + public SchemaMapBuilder() { + this.instance = new LinkedHashMap<>(); + } + public Map build() { + return instance; + } + public Map getInstance() { + return instance; + } + public SchemaMapBuilder getBuilderAfterFiles(Map instance) { + return this; + } + public SchemaMapBuilder getBuilderAfterAdditionalProperty(Map instance) { + return this; + } + } + + + public static class Schema1 extends JsonSchema implements MapSchemaValidator { + private static @Nullable Schema1 instance = null; + + protected Schema1() { + super(new JsonSchemaInfo() + .type(Set.of(Map.class)) + .properties(Map.ofEntries( + new PropertyEntry("files", Files.class) + )) + ); + } + + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + + public SchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); + for(Map.Entry entry: arg.entrySet()) { + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; + List propertyPathToItem = new ArrayList<>(pathToItem); + propertyPathToItem.add(propertyName); + Object value = entry.getValue(); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + properties.put(propertyName, propertyInstance); + } + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); + return new SchemaMap(castProperties); + } + + public SchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); + } + + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof Map) { + return validate((Map) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof Map) { + return getNewInstance((Map) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/FooServerservers.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/FooServerservers.java new file mode 100644 index 00000000000..85c9cc1ed0b --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/FooServerservers.java @@ -0,0 +1,11 @@ +package org.openapijsonschematools.client.paths.foo.get.servers; + +public class FooServerservers { + private FooGetServer0 server0; + private FooGetServer1 server1; + + public FooServerservers() { + server0 = new FooGetServer0(); + server1 = new FooGetServer1(); + } +} \ No newline at end of file diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/PetfindbystatusServers.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/PetfindbystatusServers.java new file mode 100644 index 00000000000..6f3bc6a94f5 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/PetfindbystatusServers.java @@ -0,0 +1,11 @@ +package org.openapijsonschematools.client.paths.petfindbystatus.servers; + +public class PetfindbystatusServers { + private PetfindbystatusServer0 server0; + private PetfindbystatusServer1 server1; + + public PetfindbystatusServers() { + server0 = new PetfindbystatusServer0(); + server1 = new PetfindbystatusServer1(); + } +} \ No newline at end of file diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petpetid/post/requestbody/content/application~1x-www-form-urlencoded/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petpetid/post/requestbody/content/application~1x-www-form-urlencoded/schema.java new file mode 100644 index 00000000000..df12e25231e --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petpetid/post/requestbody/content/application~1x-www-form-urlencoded/schema.java @@ -0,0 +1,216 @@ +package org.openapijsonschematools.client.paths.petpetid.post.requestbody.content.application~1x-www-form-urlencoded; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; +import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; +import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.BaseBuilder; +import org.openapijsonschematools.client.schemas.StringJsonSchema; +import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; +import org.openapijsonschematools.client.schemas.validation.FrozenMap; +import org.openapijsonschematools.client.schemas.validation.JsonSchema; +import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; +import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; +import org.openapijsonschematools.client.schemas.validation.PropertyEntry; +import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; + +public class Schema { + // nest classes so all schemas and input/output classes can be public + + + public static class Name extends StringJsonSchema { + private static @Nullable Name instance = null; + public static Name getInstance() { + if (instance == null) { + instance = new Name(); + } + return instance; + } + } + + + public static class Status extends StringJsonSchema { + private static @Nullable Status instance = null; + public static Status getInstance() { + if (instance == null) { + instance = new Status(); + } + return instance; + } + } + + + public static class SchemaMap extends FrozenMap<@Nullable Object> { + protected SchemaMap(FrozenMap<@Nullable Object> m) { + super(m); + } + public static final Set requiredKeys = Set.of(); + public static final Set optionalKeys = Set.of( + "name", + "status" + ); + public static SchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + return Schema1.getInstance().validate(arg, configuration); + } + + public String name() throws UnsetPropertyException { + String key = "name"; + throwIfKeyNotPresent(key); + @Nullable Object value = get(key); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for name"); + } + return (String) value; + } + + public String status() throws UnsetPropertyException { + String key = "status"; + throwIfKeyNotPresent(key); + @Nullable Object value = get(key); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for status"); + } + return (String) value; + } + + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { + throwIfKeyKnown(name, requiredKeys, optionalKeys); + throwIfKeyNotPresent(name); + return get(name); + } + } + + public interface SetterForName { + Map getInstance(); + T getBuilderAfterName(Map instance); + + default T name(String value) { + var instance = getInstance(); + instance.put("name", value); + return getBuilderAfterName(instance); + } + } + + public interface SetterForStatus { + Map getInstance(); + T getBuilderAfterStatus(Map instance); + + default T status(String value) { + var instance = getInstance(); + instance.put("status", value); + return getBuilderAfterStatus(instance); + } + } + + public static class SchemaMapBuilder extends UnsetAddPropsSetter implements BaseBuilder<@Nullable Object>, SetterForName, SetterForStatus { + private final Map instance; + private static final Set knownKeys = Set.of( + "name", + "status" + ); + public Set getKnownKeys() { + return knownKeys; + } + public SchemaMapBuilder() { + this.instance = new LinkedHashMap<>(); + } + public Map build() { + return instance; + } + public Map getInstance() { + return instance; + } + public SchemaMapBuilder getBuilderAfterName(Map instance) { + return this; + } + public SchemaMapBuilder getBuilderAfterStatus(Map instance) { + return this; + } + public SchemaMapBuilder getBuilderAfterAdditionalProperty(Map instance) { + return this; + } + } + + + public static class Schema1 extends JsonSchema implements MapSchemaValidator { + private static @Nullable Schema1 instance = null; + + protected Schema1() { + super(new JsonSchemaInfo() + .type(Set.of(Map.class)) + .properties(Map.ofEntries( + new PropertyEntry("name", Name.class), + new PropertyEntry("status", Status.class) + )) + ); + } + + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + + public SchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); + for(Map.Entry entry: arg.entrySet()) { + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; + List propertyPathToItem = new ArrayList<>(pathToItem); + propertyPathToItem.add(propertyName); + Object value = entry.getValue(); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + properties.put(propertyName, propertyInstance); + } + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); + return new SchemaMap(castProperties); + } + + public SchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); + } + + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof Map) { + return validate((Map) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof Map) { + return getNewInstance((Map) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petpetiduploadimage/post/requestbody/content/multipart~1form-data/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petpetiduploadimage/post/requestbody/content/multipart~1form-data/schema.java new file mode 100644 index 00000000000..aacf8cf4e7e --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petpetiduploadimage/post/requestbody/content/multipart~1form-data/schema.java @@ -0,0 +1,217 @@ +package org.openapijsonschematools.client.paths.petpetiduploadimage.post.requestbody.content.multipart~1form-data; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; +import org.openapijsonschematools.client.configurations.SchemaConfiguration; +import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; +import org.openapijsonschematools.client.exceptions.InvalidTypeException; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; +import org.openapijsonschematools.client.exceptions.ValidationException; +import org.openapijsonschematools.client.schemas.BaseBuilder; +import org.openapijsonschematools.client.schemas.StringJsonSchema; +import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; +import org.openapijsonschematools.client.schemas.validation.FrozenMap; +import org.openapijsonschematools.client.schemas.validation.JsonSchema; +import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; +import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; +import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; +import org.openapijsonschematools.client.schemas.validation.PropertyEntry; +import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; + +public class Schema { + // nest classes so all schemas and input/output classes can be public + + + public static class AdditionalMetadata extends StringJsonSchema { + private static @Nullable AdditionalMetadata instance = null; + public static AdditionalMetadata getInstance() { + if (instance == null) { + instance = new AdditionalMetadata(); + } + return instance; + } + } + + + public static class File extends StringJsonSchema { + // BinarySchema + private static @Nullable File instance = null; + public static File getInstance() { + if (instance == null) { + instance = new File(); + } + return instance; + } + } + + + public static class SchemaMap extends FrozenMap<@Nullable Object> { + protected SchemaMap(FrozenMap<@Nullable Object> m) { + super(m); + } + public static final Set requiredKeys = Set.of(); + public static final Set optionalKeys = Set.of( + "additionalMetadata", + "file" + ); + public static SchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { + return Schema1.getInstance().validate(arg, configuration); + } + + public String additionalMetadata() throws UnsetPropertyException { + String key = "additionalMetadata"; + throwIfKeyNotPresent(key); + @Nullable Object value = get(key); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for additionalMetadata"); + } + return (String) value; + } + + public String file() throws UnsetPropertyException { + String key = "file"; + throwIfKeyNotPresent(key); + @Nullable Object value = get(key); + if (!(value instanceof String)) { + throw new InvalidTypeException("Invalid value stored for file"); + } + return (String) value; + } + + public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { + throwIfKeyKnown(name, requiredKeys, optionalKeys); + throwIfKeyNotPresent(name); + return get(name); + } + } + + public interface SetterForAdditionalMetadata { + Map getInstance(); + T getBuilderAfterAdditionalMetadata(Map instance); + + default T additionalMetadata(String value) { + var instance = getInstance(); + instance.put("additionalMetadata", value); + return getBuilderAfterAdditionalMetadata(instance); + } + } + + public interface SetterForFile { + Map getInstance(); + T getBuilderAfterFile(Map instance); + + default T file(String value) { + var instance = getInstance(); + instance.put("file", value); + return getBuilderAfterFile(instance); + } + } + + public static class SchemaMapBuilder extends UnsetAddPropsSetter implements BaseBuilder<@Nullable Object>, SetterForAdditionalMetadata, SetterForFile { + private final Map instance; + private static final Set knownKeys = Set.of( + "additionalMetadata", + "file" + ); + public Set getKnownKeys() { + return knownKeys; + } + public SchemaMapBuilder() { + this.instance = new LinkedHashMap<>(); + } + public Map build() { + return instance; + } + public Map getInstance() { + return instance; + } + public SchemaMapBuilder getBuilderAfterAdditionalMetadata(Map instance) { + return this; + } + public SchemaMapBuilder getBuilderAfterFile(Map instance) { + return this; + } + public SchemaMapBuilder getBuilderAfterAdditionalProperty(Map instance) { + return this; + } + } + + + public static class Schema1 extends JsonSchema implements MapSchemaValidator { + private static @Nullable Schema1 instance = null; + + protected Schema1() { + super(new JsonSchemaInfo() + .type(Set.of(Map.class)) + .properties(Map.ofEntries( + new PropertyEntry("additionalMetadata", AdditionalMetadata.class), + new PropertyEntry("file", File.class) + )) + ); + } + + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + + public SchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { + LinkedHashMap properties = new LinkedHashMap<>(); + for(Map.Entry entry: arg.entrySet()) { + @Nullable Object entryKey = entry.getKey(); + if (!(entryKey instanceof String)) { + throw new InvalidTypeException("Invalid non-string key value"); + } + String propertyName = (String) entryKey; + List propertyPathToItem = new ArrayList<>(pathToItem); + propertyPathToItem.add(propertyName); + Object value = entry.getValue(); + LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); + if (schemas == null) { + throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); + } + JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); + @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); + properties.put(propertyName, propertyInstance); + } + FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); + return new SchemaMap(castProperties); + } + + public SchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + Set> pathSet = new HashSet<>(); + List pathToItem = List.of("args[0"); + Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); + SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); + ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); + PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); + return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); + } + + + @Override + public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { + if (arg instanceof Map) { + return validate((Map) arg, configuration); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); + } + @Override + public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { + if (arg instanceof Map) { + return getNewInstance((Map) arg, pathToItem, pathToSchemas); + } + throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); + } + } + +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/storeorder/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/storeorder/post/requestbody/content/application~1json/schema.java new file mode 100644 index 00000000000..c4c611c68fe --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/storeorder/post/requestbody/content/application~1json/schema.java @@ -0,0 +1,19 @@ +package org.openapijsonschematools.client.paths.storeorder.post.requestbody.content.application~1json; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.components.schemas.Order; + +public class Schema extends Order { + // $refed class + + + public static class Schema1 extends Order1 { + private static @Nullable Schema1 instance = null; + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + } +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/user/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/user/post/requestbody/content/application~1json/schema.java new file mode 100644 index 00000000000..c3ae5581bc1 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/user/post/requestbody/content/application~1json/schema.java @@ -0,0 +1,19 @@ +package org.openapijsonschematools.client.paths.user.post.requestbody.content.application~1json; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.components.schemas.User; + +public class Schema extends User { + // $refed class + + + public static class Schema1 extends User1 { + private static @Nullable Schema1 instance = null; + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + } +} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/userusername/put/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/userusername/put/requestbody/content/application~1json/schema.java new file mode 100644 index 00000000000..169d047fefd --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/userusername/put/requestbody/content/application~1json/schema.java @@ -0,0 +1,19 @@ +package org.openapijsonschematools.client.paths.userusername.put.requestbody.content.application~1json; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.openapijsonschematools.client.components.schemas.User; + +public class Schema extends User { + // $refed class + + + public static class Schema1 extends User1 { + private static @Nullable Schema1 instance = null; + public static Schema1 getInstance() { + if (instance == null) { + instance = new Schema1(); + } + return instance; + } + } +} diff --git a/src/main/java/org/openapijsonschematools/codegen/generatorrunner/DefaultGeneratorRunner.java b/src/main/java/org/openapijsonschematools/codegen/generatorrunner/DefaultGeneratorRunner.java index 971618f0602..0c37e648662 100644 --- a/src/main/java/org/openapijsonschematools/codegen/generatorrunner/DefaultGeneratorRunner.java +++ b/src/main/java/org/openapijsonschematools/codegen/generatorrunner/DefaultGeneratorRunner.java @@ -1391,7 +1391,10 @@ private void generateServers(List files, List servers, Stri if (servers == null && servers.isEmpty()) { return; } - generateXs(files, jsonPath, CodegenConstants.JSON_PATH_LOCATION_TYPE.SERVERS, CodegenConstants.SERVERS, null, true); + Map serversTemplateData = new HashMap<>(); + serversTemplateData.put("packageName", generator.packageName()); + serversTemplateData.put("servers", servers); + generateXs(files, jsonPath, CodegenConstants.JSON_PATH_LOCATION_TYPE.SERVERS, CodegenConstants.SERVERS, serversTemplateData, true); int i = 0; for (CodegenServer server: servers) { diff --git a/src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java b/src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java index 537c4fda31f..87e02c3050f 100644 --- a/src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java +++ b/src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java @@ -43,14 +43,9 @@ import org.openapijsonschematools.codegen.config.GlobalSettings; import org.openapijsonschematools.codegen.generators.generatormetadata.GeneratorLanguage; import org.openapijsonschematools.codegen.generators.generatormetadata.GeneratorType; -import org.openapijsonschematools.codegen.generators.generatormetadata.features.ComponentsFeature; import org.openapijsonschematools.codegen.generators.generatormetadata.features.DataTypeFeature; -import org.openapijsonschematools.codegen.generators.generatormetadata.features.DocumentationFeature; import org.openapijsonschematools.codegen.generators.generatormetadata.features.GlobalFeature; -import org.openapijsonschematools.codegen.generators.generatormetadata.features.OperationFeature; -import org.openapijsonschematools.codegen.generators.generatormetadata.features.ParameterFeature; import org.openapijsonschematools.codegen.generators.generatormetadata.features.SchemaFeature; -import org.openapijsonschematools.codegen.generators.generatormetadata.features.SecurityFeature; import org.openapijsonschematools.codegen.generators.generatormetadata.features.WireFormatFeature; import org.openapijsonschematools.codegen.generators.models.VendorExtension; import org.openapijsonschematools.codegen.generators.openapimodels.ArrayListWithContext; @@ -3804,16 +3799,16 @@ private void updatePathsFilepath(String[] pathPieces) { if (pathPieces.length < 4) { return; } - if (pathPieces.length < 5) { - return; - } Set xParameters = new HashSet<>(); xParameters.add("PathParameters"); xParameters.add("QueryParameters"); xParameters.add("HeaderParameters"); xParameters.add("CookieParameters"); if (pathPieces[3].equals("servers")) { - if (pathPieces.length == 5) { + if (pathPieces.length == 4) { + // #/paths/somePath/servers + pathPieces[3] = toServerFilename("s", jsonPath); + } else if (pathPieces.length == 5) { // #/paths/somePath/servers/0 pathPieces[4] = toServerFilename(pathPieces[4], jsonPath); } else { @@ -3821,6 +3816,7 @@ private void updatePathsFilepath(String[] pathPieces) { pathPieces[4] = "server" + pathPieces[4]; pathPieces[5] = "Variables"; } + return; } else if (pathPieces[3].equals("parameters")) { // #/paths/somePath/parameters/0 pathPieces[4] = toParameterFilename(pathPieces[4], null); @@ -3837,19 +3833,23 @@ private void updatePathsFilepath(String[] pathPieces) { pathPieces[5] = getSchemaFilename(jsonPath); return; } - } else if (pathPieces[4].equals("requestBody")) { + } else if (pathPieces.length == 4) { + // #/paths/SomePath/get + return; + } + if (pathPieces[4].equals("requestBody")) { // #/paths/somePath/get/requestBody pathPieces[4] = requestBodyIdentifier; - } else if (xParameters.contains(pathPieces[4])) { + } + if (xParameters.contains(pathPieces[4])) { // #/paths/somePath/get/PathParameters // synthetic jsonPath pathPieces[4] = getSchemaFilename(jsonPath); - } - if (pathPieces.length < 6) { - return; - } - if (pathPieces[4].equals("servers")) { - if (pathPieces.length == 6) { + } else if (pathPieces[4].equals("servers")) { + if (pathPieces.length == 5) { + // #/paths/somePath/get/servers + pathPieces[4] = toServerFilename("s", jsonPath); + } else if (pathPieces.length == 6) { // #/paths/somePath/get/servers/0 pathPieces[5] = toServerFilename(pathPieces[5], jsonPath); } else { @@ -3923,7 +3923,7 @@ private void updatePathsFilepath(String[] pathPieces) { } } - private void updateServersFilepath(String[] pathPieces) { + protected void updateServersFilepath(String[] pathPieces) { if (pathPieces.length == 2) { // #/servers return; @@ -5045,7 +5045,7 @@ public CodegenPathItem fromPathItem(PathItem pathItem, String jsonPath) { // sort them operations = new TreeMap<>(operations); List specServers = pathItem.getServers(); - List servers = fromServers(specServers, jsonPath + "/servers"); + ArrayListWithContext servers = fromServers(specServers, jsonPath + "/servers"); return new CodegenPathItem( summary, @@ -5057,11 +5057,11 @@ public CodegenPathItem fromPathItem(PathItem pathItem, String jsonPath) { } @Override - public List fromServers(List servers, String jsonPath) { + public ArrayListWithContext fromServers(List servers, String jsonPath) { if (servers == null) { return null; } - List codegenServers = new LinkedList<>(); + ArrayListWithContext codegenServers = new ArrayListWithContext<>(); int i = 0; boolean rootServer = jsonPath.equals("#/servers"); for (Server server : servers) { @@ -5080,6 +5080,8 @@ public List fromServers(List servers, String jsonPath) { codegenServers.add(cs); i ++; } + CodegenKey serversJsonPathPiece = getKey("s", "servers", jsonPath); + codegenServers.setJsonPathPiece(serversJsonPathPiece); return codegenServers; } diff --git a/src/main/java/org/openapijsonschematools/codegen/generators/Generator.java b/src/main/java/org/openapijsonschematools/codegen/generators/Generator.java index b15c2c4249f..690d6c8cee3 100644 --- a/src/main/java/org/openapijsonschematools/codegen/generators/Generator.java +++ b/src/main/java/org/openapijsonschematools/codegen/generators/Generator.java @@ -36,6 +36,7 @@ import org.openapijsonschematools.codegen.generators.generatormetadata.GeneratorLanguage; import org.openapijsonschematools.codegen.generators.generatormetadata.GeneratorType; import org.openapijsonschematools.codegen.generators.models.VendorExtension; +import org.openapijsonschematools.codegen.generators.openapimodels.ArrayListWithContext; import org.openapijsonschematools.codegen.generators.openapimodels.CodegenRefInfo; import org.openapijsonschematools.codegen.templating.SupportingFile; import org.openapijsonschematools.codegen.generators.models.CliOption; @@ -157,7 +158,7 @@ public interface Generator { CodegenPathItem fromPathItem(PathItem pathItem, String jsonPath); - List fromServers(List servers, String jsonPath); + ArrayListWithContext fromServers(List servers, String jsonPath); CodegenSchema fromServerVariables(Map variables, String jsonPath); diff --git a/src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java b/src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java index d51c134fd57..53a507373f1 100644 --- a/src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java +++ b/src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java @@ -117,16 +117,42 @@ public String toModuleFilename(String name, String jsonPath) { return usedName.toLowerCase(Locale.ROOT); } + protected void updateServersFilepath(String[] pathPieces) { + if (pathPieces.length == 2) { + // #/servers + pathPieces[1] = "Servers"; + return; + } else if (pathPieces.length == 3) { + // #/servers/0 + String jsonPath = "#/servers/" + pathPieces[2]; + pathPieces[2] = toServerFilename(pathPieces[2], jsonPath); + } else { + // #/servers/0/variables + pathPieces[2] = toServerFilename(pathPieces[2], null).toLowerCase(Locale.ROOT); + pathPieces[3] = "Variables"; + } + } + @Override public String getPascalCaseServer(String basename, String jsonPath) { if (jsonPath != null) { String[] pathPieces = jsonPath.split("/"); - if (jsonPath.startsWith("#/servers/")) { + if (jsonPath.equals("#/servers")) { + return "Servers"; + } else if (jsonPath.startsWith("#/servers/") && pathPieces.length == 3) { return "Server"+pathPieces[2]; + } else if (jsonPath.startsWith("#/paths") && pathPieces.length == 4) { + // #/paths/somePath/servers + CodegenKey pathKey = getKey(ModelUtils.decodeSlashes(pathPieces[2]), "paths"); + return pathKey.pascalCase + "Servers"; } else if (jsonPath.startsWith("#/paths") && pathPieces.length == 5) { // #/paths/somePath/servers/0 CodegenKey pathKey = getKey(ModelUtils.decodeSlashes(pathPieces[2]), "paths"); return pathKey.pascalCase + "Server"+ pathPieces[4]; + } else if (jsonPath.startsWith("#/paths") && pathPieces.length == 5) { + // #/paths/somePath/get/servers + CodegenKey pathKey = getKey(ModelUtils.decodeSlashes(pathPieces[2]), "paths"); + return pathKey.pascalCase + StringUtils.capitalize(pathPieces[3]) + "Servers"; } else if (jsonPath.startsWith("#/paths") && pathPieces.length == 6) { // #/paths/somePath/get/servers/0 CodegenKey pathKey = getKey(ModelUtils.decodeSlashes(pathPieces[2]), "paths"); @@ -2344,6 +2370,12 @@ public void setOpenAPI(OpenAPI openAPI) { put("src/main/java/packagename/servers/Server.hbs", ".java"); }} ); + jsonPathTemplateFiles.put( + CodegenConstants.JSON_PATH_LOCATION_TYPE.SERVERS, + new HashMap<>() {{ + put("src/main/java/packagename/servers/Servers.hbs", ".java"); + }} + ); jsonPathDocTemplateFiles.put( CodegenConstants.JSON_PATH_LOCATION_TYPE.SERVER, new HashMap<>() {{ diff --git a/src/main/resources/java/src/main/java/packagename/servers/Servers.hbs b/src/main/resources/java/src/main/java/packagename/servers/Servers.hbs new file mode 100644 index 00000000000..5175db573b1 --- /dev/null +++ b/src/main/resources/java/src/main/java/packagename/servers/Servers.hbs @@ -0,0 +1,15 @@ +{{#with servers.0}} +package {{{packageName}}}.{{subpackage}}; +{{/with}} + +public class {{servers.jsonPathPiece.pascalCase}} { + {{#each servers}} + private {{jsonPathPiece.pascalCase}} server{{@index}}; + {{/each}} + + public {{servers.jsonPathPiece.pascalCase}}() { + {{#each servers}} + server{{@index}} = new {{jsonPathPiece.pascalCase}}(); + {{/each}} + } +} \ No newline at end of file From 69a8dda0e92e82f9041b1a32a159a87ceafa88f3 Mon Sep 17 00:00:00 2001 From: Justin Black Date: Tue, 23 Jan 2024 16:45:26 -0800 Subject: [PATCH 3/6] Adds java comment --- .../schema.java | 459 ------ .../schema.java | 1353 ----------------- .../content/application~1json/schema.java | 19 - .../content/application~1json/schema.java | 19 - .../content/application~1json/schema.java | 19 - .../content/application~1json/schema.java | 161 -- .../content/application~1json/schema.java | 280 ---- .../content/multipart~1form-data/schema.java | 460 ------ .../schema.java | 252 --- .../application~1json-patch+json/schema.java | 19 - .../schema.java | 19 - .../content/application~1json/schema.java | 178 --- .../content/multipart~1form-data/schema.java | 178 --- .../content/application~1json/schema.java | 19 - .../application~1x-pem-file/schema.java | 19 - .../content/multipart~1form-data/schema.java | 229 --- .../content/application~1json/schema.java | 19 - .../content/application~1json/schema.java | 19 - .../content/application~1json/schema.java | 19 - .../content/application~1json/schema.java | 19 - .../content/application~1json/schema.java | 19 - .../content/application~1json/schema.java | 19 - .../content/application~1json/schema.java | 19 - .../content/application~1json/schema.java | 19 - .../content/application~1json/schema.java | 19 - .../application~1octet-stream/schema.java | 20 - .../content/multipart~1form-data/schema.java | 229 --- .../content/multipart~1form-data/schema.java | 279 ---- .../schema.java | 216 --- .../content/multipart~1form-data/schema.java | 217 --- .../content/application~1json/schema.java | 19 - .../content/application~1json/schema.java | 19 - .../content/application~1json/schema.java | 19 - .../codegen/generators/DefaultGenerator.java | 1 + 34 files changed, 1 insertion(+), 4872 deletions(-) delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/requestbody/content/application~1x-www-form-urlencoded/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/post/requestbody/content/application~1x-www-form-urlencoded/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeadditionalpropertieswitharrayofenums/get/requestbody/content/application~1json/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakebodywithfileschema/put/requestbody/content/application~1json/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakebodywithqueryparams/put/requestbody/content/application~1json/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeinlineadditionalproperties/post/requestbody/content/application~1json/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeinlinecomposition/post/requestbody/content/application~1json/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeinlinecomposition/post/requestbody/content/multipart~1form-data/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakejsonformdata/get/requestbody/content/application~1x-www-form-urlencoded/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakejsonpatch/patch/requestbody/content/application~1json-patch+json/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakejsonwithcharset/post/requestbody/content/application~1json; charset=utf-8/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakemultiplerequestbodycontenttypes/post/requestbody/content/application~1json/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakemultiplerequestbodycontenttypes/post/requestbody/content/multipart~1form-data/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeparametercollisions1ababselfab/post/requestbody/content/application~1json/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakepemcontenttype/get/requestbody/content/application~1x-pem-file/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakepetiduploadimagewithrequiredfile/post/requestbody/content/multipart~1form-data/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsarraymodel/post/requestbody/content/application~1json/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsarrayofenums/post/requestbody/content/application~1json/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsboolean/post/requestbody/content/application~1json/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefscomposedoneofnumberwithvalidations/post/requestbody/content/application~1json/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsenum/post/requestbody/content/application~1json/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsmammal/post/requestbody/content/application~1json/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsnumber/post/requestbody/content/application~1json/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsobjectmodelwithrefprops/post/requestbody/content/application~1json/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsstring/post/requestbody/content/application~1json/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeuploaddownloadfile/post/requestbody/content/application~1octet-stream/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeuploadfile/post/requestbody/content/multipart~1form-data/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeuploadfiles/post/requestbody/content/multipart~1form-data/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petpetid/post/requestbody/content/application~1x-www-form-urlencoded/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petpetiduploadimage/post/requestbody/content/multipart~1form-data/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/storeorder/post/requestbody/content/application~1json/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/user/post/requestbody/content/application~1json/schema.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/userusername/put/requestbody/content/application~1json/schema.java diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/requestbody/content/application~1x-www-form-urlencoded/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/requestbody/content/application~1x-www-form-urlencoded/schema.java deleted file mode 100644 index 1ccdb1ed22e..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/get/requestbody/content/application~1x-www-form-urlencoded/schema.java +++ /dev/null @@ -1,459 +0,0 @@ -package org.openapijsonschematools.client.paths.fake.get.requestbody.content.application~1x-www-form-urlencoded; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; -import org.openapijsonschematools.client.configurations.SchemaConfiguration; -import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; -import org.openapijsonschematools.client.exceptions.InvalidTypeException; -import org.openapijsonschematools.client.exceptions.UnsetPropertyException; -import org.openapijsonschematools.client.exceptions.ValidationException; -import org.openapijsonschematools.client.schemas.BaseBuilder; -import org.openapijsonschematools.client.schemas.SetMaker; -import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; -import org.openapijsonschematools.client.schemas.validation.DefaultValueMethod; -import org.openapijsonschematools.client.schemas.validation.FrozenList; -import org.openapijsonschematools.client.schemas.validation.FrozenMap; -import org.openapijsonschematools.client.schemas.validation.JsonSchema; -import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; -import org.openapijsonschematools.client.schemas.validation.ListSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; -import org.openapijsonschematools.client.schemas.validation.PropertyEntry; -import org.openapijsonschematools.client.schemas.validation.StringEnumValidator; -import org.openapijsonschematools.client.schemas.validation.StringSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.StringValueMethod; -import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; - -public class Schema { - // nest classes so all schemas and input/output classes can be public - - public enum StringItemsEnums implements StringValueMethod { - GREATER_THAN_SIGN(">"), - DOLLAR_SIGN("$"); - private final String value; - - StringItemsEnums(String value) { - this.value = value; - } - public String value() { - return this.value; - } - } - - - public static class Items extends JsonSchema implements StringSchemaValidator, StringEnumValidator, DefaultValueMethod { - private static @Nullable Items instance = null; - - protected Items() { - super(new JsonSchemaInfo() - .type(Set.of( - String.class - )) - .enumValues(SetMaker.makeSet( - ">", - "$" - )) - .defaultValue("$") - ); - } - - public static Items getInstance() { - if (instance == null) { - instance = new Items(); - } - return instance; - } - - @Override - public String validate(String arg, SchemaConfiguration configuration) throws ValidationException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - String castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - getPathToSchemas(this, castArg, validationMetadata, pathSet); - return castArg; - } - - @Override - public String validate(StringItemsEnums arg,SchemaConfiguration configuration) throws ValidationException { - return validate(arg.value(), configuration); - } - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof String) { - return validate((String) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof String) { - return getNewInstance((String) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - public String defaultValue() { - if (defaultValue instanceof String) { - return (String) defaultValue; - } - throw new InvalidTypeException("Invalid type stored in defaultValue"); - } - } - - public static class EnumFormStringArrayList extends FrozenList { - protected EnumFormStringArrayList(FrozenList m) { - super(m); - } - public static EnumFormStringArrayList of(List arg, SchemaConfiguration configuration) throws ValidationException { - return EnumFormStringArray.getInstance().validate(arg, configuration); - } - } - - public static class EnumFormStringArrayListBuilder { - // class to build List - private final List list; - - public EnumFormStringArrayListBuilder() { - list = new ArrayList<>(); - } - - public EnumFormStringArrayListBuilder(List list) { - this.list = list; - } - - public EnumFormStringArrayListBuilder add(String item) { - list.add(item); - return this; - } - - public EnumFormStringArrayListBuilder add(StringItemsEnums item) { - list.add(item.value()); - return this; - } - - public List build() { - return list; - } - } - - - public static class EnumFormStringArray extends JsonSchema implements ListSchemaValidator { - private static @Nullable EnumFormStringArray instance = null; - - protected EnumFormStringArray() { - super(new JsonSchemaInfo() - .type(Set.of(List.class)) - .items(Items.class) - ); - } - - public static EnumFormStringArray getInstance() { - if (instance == null) { - instance = new EnumFormStringArray(); - } - return instance; - } - - @Override - public EnumFormStringArrayList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); - int i = 0; - for (Object item: arg) { - List itemPathToItem = new ArrayList<>(pathToItem); - itemPathToItem.add(i); - LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); - if (schemas == null) { - throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); - } - JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); - @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - if (!(itemInstance instanceof String)) { - throw new InvalidTypeException("Invalid instantiated value"); - } - items.add((String) itemInstance); - i += 1; - } - FrozenList newInstanceItems = new FrozenList<>(items); - return new EnumFormStringArrayList(newInstanceItems); - } - - public EnumFormStringArrayList validate(List arg, SchemaConfiguration configuration) throws ValidationException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - List castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); - return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); - } - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof List) { - return validate((List) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof List) { - return getNewInstance((List) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - public enum StringEnumFormStringEnums implements StringValueMethod { - _ABC("_abc"), - HYPHEN_MINUS_EFG("-efg"), - LEFT_PARENTHESIS_XYZ_RIGHT_PARENTHESIS("(xyz)"); - private final String value; - - StringEnumFormStringEnums(String value) { - this.value = value; - } - public String value() { - return this.value; - } - } - - - public static class EnumFormString extends JsonSchema implements StringSchemaValidator, StringEnumValidator, DefaultValueMethod { - private static @Nullable EnumFormString instance = null; - - protected EnumFormString() { - super(new JsonSchemaInfo() - .type(Set.of( - String.class - )) - .enumValues(SetMaker.makeSet( - "_abc", - "-efg", - "(xyz)" - )) - .defaultValue("-efg") - ); - } - - public static EnumFormString getInstance() { - if (instance == null) { - instance = new EnumFormString(); - } - return instance; - } - - @Override - public String validate(String arg, SchemaConfiguration configuration) throws ValidationException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - String castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - getPathToSchemas(this, castArg, validationMetadata, pathSet); - return castArg; - } - - @Override - public String validate(StringEnumFormStringEnums arg,SchemaConfiguration configuration) throws ValidationException { - return validate(arg.value(), configuration); - } - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof String) { - return validate((String) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof String) { - return getNewInstance((String) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - public String defaultValue() { - if (defaultValue instanceof String) { - return (String) defaultValue; - } - throw new InvalidTypeException("Invalid type stored in defaultValue"); - } - } - - public static class SchemaMap extends FrozenMap<@Nullable Object> { - protected SchemaMap(FrozenMap<@Nullable Object> m) { - super(m); - } - public static final Set requiredKeys = Set.of(); - public static final Set optionalKeys = Set.of( - "enum_form_string_array", - "enum_form_string" - ); - public static SchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { - return Schema1.getInstance().validate(arg, configuration); - } - - public EnumFormStringArrayList enum_form_string_array() throws UnsetPropertyException { - String key = "enum_form_string_array"; - throwIfKeyNotPresent(key); - @Nullable Object value = get(key); - if (!(value instanceof EnumFormStringArrayList)) { - throw new InvalidTypeException("Invalid value stored for enum_form_string_array"); - } - return (EnumFormStringArrayList) value; - } - - public String enum_form_string() throws UnsetPropertyException { - String key = "enum_form_string"; - throwIfKeyNotPresent(key); - @Nullable Object value = get(key); - if (!(value instanceof String)) { - throw new InvalidTypeException("Invalid value stored for enum_form_string"); - } - return (String) value; - } - - public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { - throwIfKeyKnown(name, requiredKeys, optionalKeys); - throwIfKeyNotPresent(name); - return get(name); - } - } - - public interface SetterForEnumFormStringArray { - Map getInstance(); - T getBuilderAfterEnumFormStringArray(Map instance); - - default T enum_form_string_array(List value) { - var instance = getInstance(); - instance.put("enum_form_string_array", value); - return getBuilderAfterEnumFormStringArray(instance); - } - } - - public interface SetterForEnumFormString { - Map getInstance(); - T getBuilderAfterEnumFormString(Map instance); - - default T enum_form_string(String value) { - var instance = getInstance(); - instance.put("enum_form_string", value); - return getBuilderAfterEnumFormString(instance); - } - - default T enum_form_string(StringEnumFormStringEnums value) { - var instance = getInstance(); - instance.put("enum_form_string", value.value()); - return getBuilderAfterEnumFormString(instance); - } - } - - public static class SchemaMapBuilder extends UnsetAddPropsSetter implements BaseBuilder<@Nullable Object>, SetterForEnumFormStringArray, SetterForEnumFormString { - private final Map instance; - private static final Set knownKeys = Set.of( - "enum_form_string_array", - "enum_form_string" - ); - public Set getKnownKeys() { - return knownKeys; - } - public SchemaMapBuilder() { - this.instance = new LinkedHashMap<>(); - } - public Map build() { - return instance; - } - public Map getInstance() { - return instance; - } - public SchemaMapBuilder getBuilderAfterEnumFormStringArray(Map instance) { - return this; - } - public SchemaMapBuilder getBuilderAfterEnumFormString(Map instance) { - return this; - } - public SchemaMapBuilder getBuilderAfterAdditionalProperty(Map instance) { - return this; - } - } - - - public static class Schema1 extends JsonSchema implements MapSchemaValidator { - private static @Nullable Schema1 instance = null; - - protected Schema1() { - super(new JsonSchemaInfo() - .type(Set.of(Map.class)) - .properties(Map.ofEntries( - new PropertyEntry("enum_form_string_array", EnumFormStringArray.class), - new PropertyEntry("enum_form_string", EnumFormString.class) - )) - ); - } - - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - - public SchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); - for(Map.Entry entry: arg.entrySet()) { - @Nullable Object entryKey = entry.getKey(); - if (!(entryKey instanceof String)) { - throw new InvalidTypeException("Invalid non-string key value"); - } - String propertyName = (String) entryKey; - List propertyPathToItem = new ArrayList<>(pathToItem); - propertyPathToItem.add(propertyName); - Object value = entry.getValue(); - LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); - if (schemas == null) { - throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); - } - JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); - @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, propertyInstance); - } - FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); - return new SchemaMap(castProperties); - } - - public SchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); - return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); - } - - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof Map) { - return validate((Map) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof Map) { - return getNewInstance((Map) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/post/requestbody/content/application~1x-www-form-urlencoded/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/post/requestbody/content/application~1x-www-form-urlencoded/schema.java deleted file mode 100644 index 61d313e6029..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fake/post/requestbody/content/application~1x-www-form-urlencoded/schema.java +++ /dev/null @@ -1,1353 +0,0 @@ -package org.openapijsonschematools.client.paths.fake.post.requestbody.content.application~1x-www-form-urlencoded; -import java.time.ZonedDateTime; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import java.util.regex.Pattern; -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; -import org.openapijsonschematools.client.configurations.SchemaConfiguration; -import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; -import org.openapijsonschematools.client.exceptions.InvalidTypeException; -import org.openapijsonschematools.client.exceptions.UnsetPropertyException; -import org.openapijsonschematools.client.exceptions.ValidationException; -import org.openapijsonschematools.client.schemas.BaseBuilder; -import org.openapijsonschematools.client.schemas.DateJsonSchema; -import org.openapijsonschematools.client.schemas.Int64JsonSchema; -import org.openapijsonschematools.client.schemas.StringJsonSchema; -import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; -import org.openapijsonschematools.client.schemas.validation.DefaultValueMethod; -import org.openapijsonschematools.client.schemas.validation.FrozenMap; -import org.openapijsonschematools.client.schemas.validation.JsonSchema; -import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; -import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.NumberSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; -import org.openapijsonschematools.client.schemas.validation.PropertyEntry; -import org.openapijsonschematools.client.schemas.validation.StringSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; - -public class Schema { - // nest classes so all schemas and input/output classes can be public - - - public static class IntegerSchema extends JsonSchema implements NumberSchemaValidator { - private static @Nullable IntegerSchema instance = null; - - protected IntegerSchema() { - super(new JsonSchemaInfo() - .type(Set.of( - Integer.class, - Long.class, - Float.class, - Double.class - )) - .format("int") - .maximum(100) - .minimum(10) - ); - } - - public static IntegerSchema getInstance() { - if (instance == null) { - instance = new IntegerSchema(); - } - return instance; - } - - @Override - public Number validate(Number arg, SchemaConfiguration configuration) throws ValidationException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - Number castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - getPathToSchemas(this, castArg, validationMetadata, pathSet); - return castArg; - } - - public int validate(int arg, SchemaConfiguration configuration) throws ValidationException { - return (int) validate((Number) arg, configuration); - } - - public float validate(float arg, SchemaConfiguration configuration) throws ValidationException { - return (float) validate((Number) arg, configuration); - } - - public long validate(long arg, SchemaConfiguration configuration) throws ValidationException { - return (long) validate((Number) arg, configuration); - } - - public double validate(double arg, SchemaConfiguration configuration) throws ValidationException { - return (double) validate((Number) arg, configuration); - } - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof Number) { - return validate((Number) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof Number) { - return getNewInstance((Number) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - - public static class Int32 extends JsonSchema implements NumberSchemaValidator { - private static @Nullable Int32 instance = null; - - protected Int32() { - super(new JsonSchemaInfo() - .type(Set.of( - Integer.class, - Long.class, - Float.class, - Double.class - )) - .format("int32") - .maximum(200) - .minimum(20) - ); - } - - public static Int32 getInstance() { - if (instance == null) { - instance = new Int32(); - } - return instance; - } - - @Override - public Number validate(Number arg, SchemaConfiguration configuration) throws ValidationException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - Number castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - getPathToSchemas(this, castArg, validationMetadata, pathSet); - return castArg; - } - - public int validate(int arg, SchemaConfiguration configuration) throws ValidationException { - return (int) validate((Number) arg, configuration); - } - - public float validate(float arg, SchemaConfiguration configuration) throws ValidationException { - return (float) validate((Number) arg, configuration); - } - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof Number) { - return validate((Number) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof Number) { - return getNewInstance((Number) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - - public static class Int64 extends Int64JsonSchema { - private static @Nullable Int64 instance = null; - public static Int64 getInstance() { - if (instance == null) { - instance = new Int64(); - } - return instance; - } - } - - - public static class NumberSchema extends JsonSchema implements NumberSchemaValidator { - private static @Nullable NumberSchema instance = null; - - protected NumberSchema() { - super(new JsonSchemaInfo() - .type(Set.of( - Integer.class, - Long.class, - Float.class, - Double.class - )) - .maximum(543.2) - .minimum(32.1) - ); - } - - public static NumberSchema getInstance() { - if (instance == null) { - instance = new NumberSchema(); - } - return instance; - } - - @Override - public Number validate(Number arg, SchemaConfiguration configuration) throws ValidationException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - Number castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); - return castArg; - } - - public int validate(int arg, SchemaConfiguration configuration) throws ValidationException { - return (int) validate((Number) arg, configuration); - } - - public long validate(long arg, SchemaConfiguration configuration) throws ValidationException { - return (long) validate((Number) arg, configuration); - } - - public float validate(float arg, SchemaConfiguration configuration) throws ValidationException { - return (float) validate((Number) arg, configuration); - } - - public double validate(double arg, SchemaConfiguration configuration) throws ValidationException { - return (double) validate((Number) arg, configuration); - } - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof Number) { - return validate((Number) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof Number) { - return getNewInstance((Number) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - - public static class FloatSchema extends JsonSchema implements NumberSchemaValidator { - private static @Nullable FloatSchema instance = null; - - protected FloatSchema() { - super(new JsonSchemaInfo() - .type(Set.of( - Integer.class, - Long.class, - Float.class, - Double.class - )) - .format("float") - .maximum(987.6) - ); - } - - public static FloatSchema getInstance() { - if (instance == null) { - instance = new FloatSchema(); - } - return instance; - } - - @Override - public Number validate(Number arg, SchemaConfiguration configuration) throws ValidationException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - Number castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); - return castArg; - } - public float validate(float arg, SchemaConfiguration configuration) throws ValidationException { - return (float) validate((Number) arg, configuration); - } - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof Number) { - return validate((Number) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof Number) { - return getNewInstance((Number) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - - public static class DoubleSchema extends JsonSchema implements NumberSchemaValidator { - private static @Nullable DoubleSchema instance = null; - - protected DoubleSchema() { - super(new JsonSchemaInfo() - .type(Set.of( - Integer.class, - Long.class, - Float.class, - Double.class - )) - .format("double") - .maximum(123.4) - .minimum(67.8) - ); - } - - public static DoubleSchema getInstance() { - if (instance == null) { - instance = new DoubleSchema(); - } - return instance; - } - - @Override - public Number validate(Number arg, SchemaConfiguration configuration) throws ValidationException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - Number castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); - return castArg; - } - public double validate(double arg, SchemaConfiguration configuration) throws ValidationException { - return (double) validate((Number) arg, configuration); - } - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof Number) { - return validate((Number) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof Number) { - return getNewInstance((Number) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - - public static class StringSchema extends JsonSchema implements StringSchemaValidator { - private static @Nullable StringSchema instance = null; - - protected StringSchema() { - super(new JsonSchemaInfo() - .type(Set.of( - String.class - )) - .pattern(Pattern.compile( - "[a-z]", - Pattern.CASE_INSENSITIVE - )) - ); - } - - public static StringSchema getInstance() { - if (instance == null) { - instance = new StringSchema(); - } - return instance; - } - - @Override - public String validate(String arg, SchemaConfiguration configuration) throws ValidationException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - String castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - getPathToSchemas(this, castArg, validationMetadata, pathSet); - return castArg; - } - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof String) { - return validate((String) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof String) { - return getNewInstance((String) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - - public static class PatternWithoutDelimiter extends JsonSchema implements StringSchemaValidator { - private static @Nullable PatternWithoutDelimiter instance = null; - - protected PatternWithoutDelimiter() { - super(new JsonSchemaInfo() - .type(Set.of( - String.class - )) - .pattern(Pattern.compile( - "^[A-Z].*" - )) - ); - } - - public static PatternWithoutDelimiter getInstance() { - if (instance == null) { - instance = new PatternWithoutDelimiter(); - } - return instance; - } - - @Override - public String validate(String arg, SchemaConfiguration configuration) throws ValidationException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - String castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - getPathToSchemas(this, castArg, validationMetadata, pathSet); - return castArg; - } - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof String) { - return validate((String) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof String) { - return getNewInstance((String) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - - public static class ByteSchema extends StringJsonSchema { - private static @Nullable ByteSchema instance = null; - public static ByteSchema getInstance() { - if (instance == null) { - instance = new ByteSchema(); - } - return instance; - } - } - - - public static class Binary extends StringJsonSchema { - // BinarySchema - private static @Nullable Binary instance = null; - public static Binary getInstance() { - if (instance == null) { - instance = new Binary(); - } - return instance; - } - } - - - public static class Date extends DateJsonSchema { - private static @Nullable Date instance = null; - public static Date getInstance() { - if (instance == null) { - instance = new Date(); - } - return instance; - } - } - - - public static class DateTime extends JsonSchema implements StringSchemaValidator, DefaultValueMethod { - private static @Nullable DateTime instance = null; - - protected DateTime() { - super(new JsonSchemaInfo() - .type(Set.of( - String.class - )) - .format("date-time") - .defaultValue("2010-02-01T10:20:10.111110+01:00") - ); - } - - public static DateTime getInstance() { - if (instance == null) { - instance = new DateTime(); - } - return instance; - } - - @Override - public String validate(String arg, SchemaConfiguration configuration) throws ValidationException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - String castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - getPathToSchemas(this, castArg, validationMetadata, pathSet); - return castArg; - } - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof String) { - return validate((String) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof String) { - return getNewInstance((String) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - public String defaultValue() { - if (defaultValue instanceof String) { - return (String) defaultValue; - } - throw new InvalidTypeException("Invalid type stored in defaultValue"); - } - } - - public static class Password extends JsonSchema implements StringSchemaValidator { - private static @Nullable Password instance = null; - - protected Password() { - super(new JsonSchemaInfo() - .type(Set.of( - String.class - )) - .format("password") - .maxLength(64) - .minLength(10) - ); - } - - public static Password getInstance() { - if (instance == null) { - instance = new Password(); - } - return instance; - } - - @Override - public String validate(String arg, SchemaConfiguration configuration) throws ValidationException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - String castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - getPathToSchemas(this, castArg, validationMetadata, pathSet); - return castArg; - } - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof String) { - return validate((String) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof String) { - return getNewInstance((String) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - - public static class Callback extends StringJsonSchema { - private static @Nullable Callback instance = null; - public static Callback getInstance() { - if (instance == null) { - instance = new Callback(); - } - return instance; - } - } - - - public static class SchemaMap extends FrozenMap<@Nullable Object> { - protected SchemaMap(FrozenMap<@Nullable Object> m) { - super(m); - } - public static final Set requiredKeys = Set.of( - "byte", - "double", - "number", - "pattern_without_delimiter" - ); - public static final Set optionalKeys = Set.of( - "integer", - "int32", - "int64", - "float", - "string", - "binary", - "date", - "dateTime", - "password", - "callback" - ); - public static SchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { - return Schema1.getInstance().validate(arg, configuration); - } - - public String pattern_without_delimiter() { - @Nullable Object value = get("pattern_without_delimiter"); - if (!(value instanceof String)) { - throw new InvalidTypeException("Invalid value stored for pattern_without_delimiter"); - } - return (String) value; - } - - public Number int32() throws UnsetPropertyException { - String key = "int32"; - throwIfKeyNotPresent(key); - @Nullable Object value = get(key); - if (!(value instanceof Number)) { - throw new InvalidTypeException("Invalid value stored for int32"); - } - return (Number) value; - } - - public Number int64() throws UnsetPropertyException { - String key = "int64"; - throwIfKeyNotPresent(key); - @Nullable Object value = get(key); - if (!(value instanceof Number)) { - throw new InvalidTypeException("Invalid value stored for int64"); - } - return (Number) value; - } - - public String binary() throws UnsetPropertyException { - String key = "binary"; - throwIfKeyNotPresent(key); - @Nullable Object value = get(key); - if (!(value instanceof String)) { - throw new InvalidTypeException("Invalid value stored for binary"); - } - return (String) value; - } - - public String date() throws UnsetPropertyException { - String key = "date"; - throwIfKeyNotPresent(key); - @Nullable Object value = get(key); - if (!(value instanceof String)) { - throw new InvalidTypeException("Invalid value stored for date"); - } - return (String) value; - } - - public String dateTime() throws UnsetPropertyException { - String key = "dateTime"; - throwIfKeyNotPresent(key); - @Nullable Object value = get(key); - if (!(value instanceof String)) { - throw new InvalidTypeException("Invalid value stored for dateTime"); - } - return (String) value; - } - - public String password() throws UnsetPropertyException { - String key = "password"; - throwIfKeyNotPresent(key); - @Nullable Object value = get(key); - if (!(value instanceof String)) { - throw new InvalidTypeException("Invalid value stored for password"); - } - return (String) value; - } - - public String callback() throws UnsetPropertyException { - String key = "callback"; - throwIfKeyNotPresent(key); - @Nullable Object value = get(key); - if (!(value instanceof String)) { - throw new InvalidTypeException("Invalid value stored for callback"); - } - return (String) value; - } - - public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { - throwIfKeyKnown(name, requiredKeys, optionalKeys); - throwIfKeyNotPresent(name); - return get(name); - } - } - - public interface SetterForByteSchema { - Map getInstance(); - T getBuilderAfterByteSchema(Map instance); - - default T setByte(String value) { - var instance = getInstance(); - instance.put("byte", value); - return getBuilderAfterByteSchema(instance); - } - } - - public interface SetterForDoubleSchema { - Map getInstance(); - T getBuilderAfterDoubleSchema(Map instance); - - default T setDouble(int value) { - var instance = getInstance(); - instance.put("double", value); - return getBuilderAfterDoubleSchema(instance); - } - - default T setDouble(float value) { - var instance = getInstance(); - instance.put("double", value); - return getBuilderAfterDoubleSchema(instance); - } - - default T setDouble(long value) { - var instance = getInstance(); - instance.put("double", value); - return getBuilderAfterDoubleSchema(instance); - } - - default T setDouble(double value) { - var instance = getInstance(); - instance.put("double", value); - return getBuilderAfterDoubleSchema(instance); - } - } - - public interface SetterForNumberSchema { - Map getInstance(); - T getBuilderAfterNumberSchema(Map instance); - - default T setNumber(int value) { - var instance = getInstance(); - instance.put("number", value); - return getBuilderAfterNumberSchema(instance); - } - - default T setNumber(float value) { - var instance = getInstance(); - instance.put("number", value); - return getBuilderAfterNumberSchema(instance); - } - - default T setNumber(long value) { - var instance = getInstance(); - instance.put("number", value); - return getBuilderAfterNumberSchema(instance); - } - - default T setNumber(double value) { - var instance = getInstance(); - instance.put("number", value); - return getBuilderAfterNumberSchema(instance); - } - } - - public interface SetterForPatternWithoutDelimiter { - Map getInstance(); - T getBuilderAfterPatternWithoutDelimiter(Map instance); - - default T pattern_without_delimiter(String value) { - var instance = getInstance(); - instance.put("pattern_without_delimiter", value); - return getBuilderAfterPatternWithoutDelimiter(instance); - } - } - - public interface SetterForIntegerSchema { - Map getInstance(); - T getBuilderAfterIntegerSchema(Map instance); - - default T setInteger(int value) { - var instance = getInstance(); - instance.put("integer", value); - return getBuilderAfterIntegerSchema(instance); - } - - default T setInteger(float value) { - var instance = getInstance(); - instance.put("integer", value); - return getBuilderAfterIntegerSchema(instance); - } - - default T setInteger(long value) { - var instance = getInstance(); - instance.put("integer", value); - return getBuilderAfterIntegerSchema(instance); - } - - default T setInteger(double value) { - var instance = getInstance(); - instance.put("integer", value); - return getBuilderAfterIntegerSchema(instance); - } - } - - public interface SetterForInt32 { - Map getInstance(); - T getBuilderAfterInt32(Map instance); - - default T int32(int value) { - var instance = getInstance(); - instance.put("int32", value); - return getBuilderAfterInt32(instance); - } - - default T int32(float value) { - var instance = getInstance(); - instance.put("int32", value); - return getBuilderAfterInt32(instance); - } - } - - public interface SetterForInt64 { - Map getInstance(); - T getBuilderAfterInt64(Map instance); - - default T int64(int value) { - var instance = getInstance(); - instance.put("int64", value); - return getBuilderAfterInt64(instance); - } - - default T int64(float value) { - var instance = getInstance(); - instance.put("int64", value); - return getBuilderAfterInt64(instance); - } - - default T int64(long value) { - var instance = getInstance(); - instance.put("int64", value); - return getBuilderAfterInt64(instance); - } - - default T int64(double value) { - var instance = getInstance(); - instance.put("int64", value); - return getBuilderAfterInt64(instance); - } - } - - public interface SetterForFloatSchema { - Map getInstance(); - T getBuilderAfterFloatSchema(Map instance); - - default T setFloat(int value) { - var instance = getInstance(); - instance.put("float", value); - return getBuilderAfterFloatSchema(instance); - } - - default T setFloat(float value) { - var instance = getInstance(); - instance.put("float", value); - return getBuilderAfterFloatSchema(instance); - } - - default T setFloat(long value) { - var instance = getInstance(); - instance.put("float", value); - return getBuilderAfterFloatSchema(instance); - } - - default T setFloat(double value) { - var instance = getInstance(); - instance.put("float", value); - return getBuilderAfterFloatSchema(instance); - } - } - - public interface SetterForStringSchema { - Map getInstance(); - T getBuilderAfterStringSchema(Map instance); - - default T setString(String value) { - var instance = getInstance(); - instance.put("string", value); - return getBuilderAfterStringSchema(instance); - } - } - - public interface SetterForBinary { - Map getInstance(); - T getBuilderAfterBinary(Map instance); - - default T binary(String value) { - var instance = getInstance(); - instance.put("binary", value); - return getBuilderAfterBinary(instance); - } - } - - public interface SetterForDate { - Map getInstance(); - T getBuilderAfterDate(Map instance); - - default T date(String value) { - var instance = getInstance(); - instance.put("date", value); - return getBuilderAfterDate(instance); - } - } - - public interface SetterForDateTime { - Map getInstance(); - T getBuilderAfterDateTime(Map instance); - - default T dateTime(String value) { - var instance = getInstance(); - instance.put("dateTime", value); - return getBuilderAfterDateTime(instance); - } - } - - public interface SetterForPassword { - Map getInstance(); - T getBuilderAfterPassword(Map instance); - - default T password(String value) { - var instance = getInstance(); - instance.put("password", value); - return getBuilderAfterPassword(instance); - } - } - - public interface SetterForCallback { - Map getInstance(); - T getBuilderAfterCallback(Map instance); - - default T callback(String value) { - var instance = getInstance(); - instance.put("callback", value); - return getBuilderAfterCallback(instance); - } - } - - public static class SchemaMap0000Builder extends UnsetAddPropsSetter implements BaseBuilder<@Nullable Object>, SetterForIntegerSchema, SetterForInt32, SetterForInt64, SetterForFloatSchema, SetterForStringSchema, SetterForBinary, SetterForDate, SetterForDateTime, SetterForPassword, SetterForCallback { - private final Map instance; - private static final Set knownKeys = Set.of( - "byte", - "double", - "number", - "pattern_without_delimiter", - "integer", - "int32", - "int64", - "float", - "string", - "binary", - "date", - "dateTime", - "password", - "callback" - ); - public Set getKnownKeys() { - return knownKeys; - } - public SchemaMap0000Builder(Map instance) { - this.instance = instance; - } - public Map build() { - return instance; - } - public Map getInstance() { - return instance; - } - public SchemaMap0000Builder getBuilderAfterIntegerSchema(Map instance) { - return this; - } - public SchemaMap0000Builder getBuilderAfterInt32(Map instance) { - return this; - } - public SchemaMap0000Builder getBuilderAfterInt64(Map instance) { - return this; - } - public SchemaMap0000Builder getBuilderAfterFloatSchema(Map instance) { - return this; - } - public SchemaMap0000Builder getBuilderAfterStringSchema(Map instance) { - return this; - } - public SchemaMap0000Builder getBuilderAfterBinary(Map instance) { - return this; - } - public SchemaMap0000Builder getBuilderAfterDate(Map instance) { - return this; - } - public SchemaMap0000Builder getBuilderAfterDateTime(Map instance) { - return this; - } - public SchemaMap0000Builder getBuilderAfterPassword(Map instance) { - return this; - } - public SchemaMap0000Builder getBuilderAfterCallback(Map instance) { - return this; - } - public SchemaMap0000Builder getBuilderAfterAdditionalProperty(Map instance) { - return this; - } - } - - public static class SchemaMap0001Builder implements SetterForPatternWithoutDelimiter { - private final Map instance; - public SchemaMap0001Builder(Map instance) { - this.instance = instance; - } - public Map getInstance() { - return instance; - } - public SchemaMap0000Builder getBuilderAfterPatternWithoutDelimiter(Map instance) { - return new SchemaMap0000Builder(instance); - } - } - - public static class SchemaMap0010Builder implements SetterForNumberSchema { - private final Map instance; - public SchemaMap0010Builder(Map instance) { - this.instance = instance; - } - public Map getInstance() { - return instance; - } - public SchemaMap0000Builder getBuilderAfterNumberSchema(Map instance) { - return new SchemaMap0000Builder(instance); - } - } - - public static class SchemaMap0011Builder implements SetterForNumberSchema, SetterForPatternWithoutDelimiter { - private final Map instance; - public SchemaMap0011Builder(Map instance) { - this.instance = instance; - } - public Map getInstance() { - return instance; - } - public SchemaMap0001Builder getBuilderAfterNumberSchema(Map instance) { - return new SchemaMap0001Builder(instance); - } - public SchemaMap0010Builder getBuilderAfterPatternWithoutDelimiter(Map instance) { - return new SchemaMap0010Builder(instance); - } - } - - public static class SchemaMap0100Builder implements SetterForDoubleSchema { - private final Map instance; - public SchemaMap0100Builder(Map instance) { - this.instance = instance; - } - public Map getInstance() { - return instance; - } - public SchemaMap0000Builder getBuilderAfterDoubleSchema(Map instance) { - return new SchemaMap0000Builder(instance); - } - } - - public static class SchemaMap0101Builder implements SetterForDoubleSchema, SetterForPatternWithoutDelimiter { - private final Map instance; - public SchemaMap0101Builder(Map instance) { - this.instance = instance; - } - public Map getInstance() { - return instance; - } - public SchemaMap0001Builder getBuilderAfterDoubleSchema(Map instance) { - return new SchemaMap0001Builder(instance); - } - public SchemaMap0100Builder getBuilderAfterPatternWithoutDelimiter(Map instance) { - return new SchemaMap0100Builder(instance); - } - } - - public static class SchemaMap0110Builder implements SetterForDoubleSchema, SetterForNumberSchema { - private final Map instance; - public SchemaMap0110Builder(Map instance) { - this.instance = instance; - } - public Map getInstance() { - return instance; - } - public SchemaMap0010Builder getBuilderAfterDoubleSchema(Map instance) { - return new SchemaMap0010Builder(instance); - } - public SchemaMap0100Builder getBuilderAfterNumberSchema(Map instance) { - return new SchemaMap0100Builder(instance); - } - } - - public static class SchemaMap0111Builder implements SetterForDoubleSchema, SetterForNumberSchema, SetterForPatternWithoutDelimiter { - private final Map instance; - public SchemaMap0111Builder(Map instance) { - this.instance = instance; - } - public Map getInstance() { - return instance; - } - public SchemaMap0011Builder getBuilderAfterDoubleSchema(Map instance) { - return new SchemaMap0011Builder(instance); - } - public SchemaMap0101Builder getBuilderAfterNumberSchema(Map instance) { - return new SchemaMap0101Builder(instance); - } - public SchemaMap0110Builder getBuilderAfterPatternWithoutDelimiter(Map instance) { - return new SchemaMap0110Builder(instance); - } - } - - public static class SchemaMap1000Builder implements SetterForByteSchema { - private final Map instance; - public SchemaMap1000Builder(Map instance) { - this.instance = instance; - } - public Map getInstance() { - return instance; - } - public SchemaMap0000Builder getBuilderAfterByteSchema(Map instance) { - return new SchemaMap0000Builder(instance); - } - } - - public static class SchemaMap1001Builder implements SetterForByteSchema, SetterForPatternWithoutDelimiter { - private final Map instance; - public SchemaMap1001Builder(Map instance) { - this.instance = instance; - } - public Map getInstance() { - return instance; - } - public SchemaMap0001Builder getBuilderAfterByteSchema(Map instance) { - return new SchemaMap0001Builder(instance); - } - public SchemaMap1000Builder getBuilderAfterPatternWithoutDelimiter(Map instance) { - return new SchemaMap1000Builder(instance); - } - } - - public static class SchemaMap1010Builder implements SetterForByteSchema, SetterForNumberSchema { - private final Map instance; - public SchemaMap1010Builder(Map instance) { - this.instance = instance; - } - public Map getInstance() { - return instance; - } - public SchemaMap0010Builder getBuilderAfterByteSchema(Map instance) { - return new SchemaMap0010Builder(instance); - } - public SchemaMap1000Builder getBuilderAfterNumberSchema(Map instance) { - return new SchemaMap1000Builder(instance); - } - } - - public static class SchemaMap1011Builder implements SetterForByteSchema, SetterForNumberSchema, SetterForPatternWithoutDelimiter { - private final Map instance; - public SchemaMap1011Builder(Map instance) { - this.instance = instance; - } - public Map getInstance() { - return instance; - } - public SchemaMap0011Builder getBuilderAfterByteSchema(Map instance) { - return new SchemaMap0011Builder(instance); - } - public SchemaMap1001Builder getBuilderAfterNumberSchema(Map instance) { - return new SchemaMap1001Builder(instance); - } - public SchemaMap1010Builder getBuilderAfterPatternWithoutDelimiter(Map instance) { - return new SchemaMap1010Builder(instance); - } - } - - public static class SchemaMap1100Builder implements SetterForByteSchema, SetterForDoubleSchema { - private final Map instance; - public SchemaMap1100Builder(Map instance) { - this.instance = instance; - } - public Map getInstance() { - return instance; - } - public SchemaMap0100Builder getBuilderAfterByteSchema(Map instance) { - return new SchemaMap0100Builder(instance); - } - public SchemaMap1000Builder getBuilderAfterDoubleSchema(Map instance) { - return new SchemaMap1000Builder(instance); - } - } - - public static class SchemaMap1101Builder implements SetterForByteSchema, SetterForDoubleSchema, SetterForPatternWithoutDelimiter { - private final Map instance; - public SchemaMap1101Builder(Map instance) { - this.instance = instance; - } - public Map getInstance() { - return instance; - } - public SchemaMap0101Builder getBuilderAfterByteSchema(Map instance) { - return new SchemaMap0101Builder(instance); - } - public SchemaMap1001Builder getBuilderAfterDoubleSchema(Map instance) { - return new SchemaMap1001Builder(instance); - } - public SchemaMap1100Builder getBuilderAfterPatternWithoutDelimiter(Map instance) { - return new SchemaMap1100Builder(instance); - } - } - - public static class SchemaMap1110Builder implements SetterForByteSchema, SetterForDoubleSchema, SetterForNumberSchema { - private final Map instance; - public SchemaMap1110Builder(Map instance) { - this.instance = instance; - } - public Map getInstance() { - return instance; - } - public SchemaMap0110Builder getBuilderAfterByteSchema(Map instance) { - return new SchemaMap0110Builder(instance); - } - public SchemaMap1010Builder getBuilderAfterDoubleSchema(Map instance) { - return new SchemaMap1010Builder(instance); - } - public SchemaMap1100Builder getBuilderAfterNumberSchema(Map instance) { - return new SchemaMap1100Builder(instance); - } - } - - public static class SchemaMapBuilder implements SetterForByteSchema, SetterForDoubleSchema, SetterForNumberSchema, SetterForPatternWithoutDelimiter { - private final Map instance; - public SchemaMapBuilder() { - this.instance = new LinkedHashMap<>(); - } - public Map getInstance() { - return instance; - } - public SchemaMap0111Builder getBuilderAfterByteSchema(Map instance) { - return new SchemaMap0111Builder(instance); - } - public SchemaMap1011Builder getBuilderAfterDoubleSchema(Map instance) { - return new SchemaMap1011Builder(instance); - } - public SchemaMap1101Builder getBuilderAfterNumberSchema(Map instance) { - return new SchemaMap1101Builder(instance); - } - public SchemaMap1110Builder getBuilderAfterPatternWithoutDelimiter(Map instance) { - return new SchemaMap1110Builder(instance); - } - } - - - public static class Schema1 extends JsonSchema implements MapSchemaValidator { - private static @Nullable Schema1 instance = null; - - protected Schema1() { - super(new JsonSchemaInfo() - .type(Set.of(Map.class)) - .properties(Map.ofEntries( - new PropertyEntry("integer", IntegerSchema.class), - new PropertyEntry("int32", Int32.class), - new PropertyEntry("int64", Int64.class), - new PropertyEntry("number", NumberSchema.class), - new PropertyEntry("float", FloatSchema.class), - new PropertyEntry("double", DoubleSchema.class), - new PropertyEntry("string", StringSchema.class), - new PropertyEntry("pattern_without_delimiter", PatternWithoutDelimiter.class), - new PropertyEntry("byte", ByteSchema.class), - new PropertyEntry("binary", Binary.class), - new PropertyEntry("date", Date.class), - new PropertyEntry("dateTime", DateTime.class), - new PropertyEntry("password", Password.class), - new PropertyEntry("callback", Callback.class) - )) - .required(Set.of( - "byte", - "double", - "number", - "pattern_without_delimiter" - )) - ); - } - - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - - public SchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); - for(Map.Entry entry: arg.entrySet()) { - @Nullable Object entryKey = entry.getKey(); - if (!(entryKey instanceof String)) { - throw new InvalidTypeException("Invalid non-string key value"); - } - String propertyName = (String) entryKey; - List propertyPathToItem = new ArrayList<>(pathToItem); - propertyPathToItem.add(propertyName); - Object value = entry.getValue(); - LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); - if (schemas == null) { - throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); - } - JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); - @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, propertyInstance); - } - FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); - return new SchemaMap(castProperties); - } - - public SchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); - return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); - } - - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof Map) { - return validate((Map) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof Map) { - return getNewInstance((Map) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeadditionalpropertieswitharrayofenums/get/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeadditionalpropertieswitharrayofenums/get/requestbody/content/application~1json/schema.java deleted file mode 100644 index 011f0a54815..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeadditionalpropertieswitharrayofenums/get/requestbody/content/application~1json/schema.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.openapijsonschematools.client.paths.fakeadditionalpropertieswitharrayofenums.get.requestbody.content.application~1json; - -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.components.schemas.AdditionalPropertiesWithArrayOfEnums; - -public class Schema extends AdditionalPropertiesWithArrayOfEnums { - // $refed class - - - public static class Schema1 extends AdditionalPropertiesWithArrayOfEnums1 { - private static @Nullable Schema1 instance = null; - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - } -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakebodywithfileschema/put/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakebodywithfileschema/put/requestbody/content/application~1json/schema.java deleted file mode 100644 index 9fef42db1bc..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakebodywithfileschema/put/requestbody/content/application~1json/schema.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.openapijsonschematools.client.paths.fakebodywithfileschema.put.requestbody.content.application~1json; - -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.components.schemas.FileSchemaTestClass; - -public class Schema extends FileSchemaTestClass { - // $refed class - - - public static class Schema1 extends FileSchemaTestClass1 { - private static @Nullable Schema1 instance = null; - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - } -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakebodywithqueryparams/put/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakebodywithqueryparams/put/requestbody/content/application~1json/schema.java deleted file mode 100644 index 517b9ecad2f..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakebodywithqueryparams/put/requestbody/content/application~1json/schema.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.openapijsonschematools.client.paths.fakebodywithqueryparams.put.requestbody.content.application~1json; - -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.components.schemas.User; - -public class Schema extends User { - // $refed class - - - public static class Schema1 extends User1 { - private static @Nullable Schema1 instance = null; - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - } -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeinlineadditionalproperties/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeinlineadditionalproperties/post/requestbody/content/application~1json/schema.java deleted file mode 100644 index 87939793e47..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeinlineadditionalproperties/post/requestbody/content/application~1json/schema.java +++ /dev/null @@ -1,161 +0,0 @@ -package org.openapijsonschematools.client.paths.fakeinlineadditionalproperties.post.requestbody.content.application~1json; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; -import org.openapijsonschematools.client.configurations.SchemaConfiguration; -import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; -import org.openapijsonschematools.client.exceptions.InvalidTypeException; -import org.openapijsonschematools.client.exceptions.UnsetPropertyException; -import org.openapijsonschematools.client.exceptions.ValidationException; -import org.openapijsonschematools.client.schemas.BaseBuilder; -import org.openapijsonschematools.client.schemas.StringJsonSchema; -import org.openapijsonschematools.client.schemas.validation.FrozenMap; -import org.openapijsonschematools.client.schemas.validation.JsonSchema; -import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; -import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.MapUtils; -import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; -import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; - -public class Schema { - // nest classes so all schemas and input/output classes can be public - - - public static class AdditionalProperties extends StringJsonSchema { - private static @Nullable AdditionalProperties instance = null; - public static AdditionalProperties getInstance() { - if (instance == null) { - instance = new AdditionalProperties(); - } - return instance; - } - } - - - public static class SchemaMap extends FrozenMap { - protected SchemaMap(FrozenMap m) { - super(m); - } - public static final Set requiredKeys = Set.of(); - public static final Set optionalKeys = Set.of(); - public static SchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { - return Schema1.getInstance().validate(arg, configuration); - } - - public String getAdditionalProperty(String name) throws UnsetPropertyException { - return getOrThrow(name); - } - } - - public interface SetterForAdditionalProperties { - Set getKnownKeys(); - Map getInstance(); - T getBuilderAfterAdditionalProperty(Map instance); - - default T additionalProperty(String key, String value) throws InvalidAdditionalPropertyException { - MapUtils.throwIfKeyKnown(key, getKnownKeys(), true); - var instance = getInstance(); - instance.put(key, value); - return getBuilderAfterAdditionalProperty(instance); - } - } - - public static class SchemaMapBuilder implements BaseBuilder, SetterForAdditionalProperties { - private final Map instance; - private static final Set knownKeys = Set.of(); - public Set getKnownKeys() { - return knownKeys; - } - public SchemaMapBuilder() { - this.instance = new LinkedHashMap<>(); - } - public Map build() { - return instance; - } - public Map getInstance() { - return instance; - } - public SchemaMapBuilder getBuilderAfterAdditionalProperty(Map instance) { - return this; - } - } - - - public static class Schema1 extends JsonSchema implements MapSchemaValidator { - private static @Nullable Schema1 instance = null; - - protected Schema1() { - super(new JsonSchemaInfo() - .type(Set.of(Map.class)) - .additionalProperties(AdditionalProperties.class) - ); - } - - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - - public SchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); - for(Map.Entry entry: arg.entrySet()) { - @Nullable Object entryKey = entry.getKey(); - if (!(entryKey instanceof String)) { - throw new InvalidTypeException("Invalid non-string key value"); - } - String propertyName = (String) entryKey; - List propertyPathToItem = new ArrayList<>(pathToItem); - propertyPathToItem.add(propertyName); - Object value = entry.getValue(); - LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); - if (schemas == null) { - throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); - } - JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); - @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - if (!(propertyInstance instanceof String)) { - throw new InvalidTypeException("Invalid instantiated value"); - } - properties.put(propertyName, (String) propertyInstance); - } - FrozenMap castProperties = new FrozenMap<>(properties); - return new SchemaMap(castProperties); - } - - public SchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); - return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); - } - - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof Map) { - return validate((Map) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof Map) { - return getNewInstance((Map) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeinlinecomposition/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeinlinecomposition/post/requestbody/content/application~1json/schema.java deleted file mode 100644 index e2b2682107f..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeinlinecomposition/post/requestbody/content/application~1json/schema.java +++ /dev/null @@ -1,280 +0,0 @@ -package org.openapijsonschematools.client.paths.fakeinlinecomposition.post.requestbody.content.application~1json; -import java.time.LocalDate; -import java.time.ZonedDateTime; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import java.util.UUID; -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; -import org.openapijsonschematools.client.configurations.SchemaConfiguration; -import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; -import org.openapijsonschematools.client.exceptions.InvalidTypeException; -import org.openapijsonschematools.client.exceptions.UnsetPropertyException; -import org.openapijsonschematools.client.exceptions.ValidationException; -import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; -import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.FrozenList; -import org.openapijsonschematools.client.schemas.validation.FrozenMap; -import org.openapijsonschematools.client.schemas.validation.JsonSchema; -import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; -import org.openapijsonschematools.client.schemas.validation.ListSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.NullSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.NumberSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; -import org.openapijsonschematools.client.schemas.validation.StringSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; - -public class Schema { - // nest classes so all schemas and input/output classes can be public - - - public static class Schema0 extends JsonSchema implements StringSchemaValidator { - private static @Nullable Schema0 instance = null; - - protected Schema0() { - super(new JsonSchemaInfo() - .type(Set.of( - String.class - )) - .minLength(1) - ); - } - - public static Schema0 getInstance() { - if (instance == null) { - instance = new Schema0(); - } - return instance; - } - - @Override - public String validate(String arg, SchemaConfiguration configuration) throws ValidationException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - String castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - getPathToSchemas(this, castArg, validationMetadata, pathSet); - return castArg; - } - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof String) { - return validate((String) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof String) { - return getNewInstance((String) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - - public static class Schema1 extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { - private static @Nullable Schema1 instance = null; - - protected Schema1() { - super(new JsonSchemaInfo() - .allOf(List.of( - Schema0.class - )) - ); - } - - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - - @Override - public Void validate(Void arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0]"); - Void castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - PathToSchemasMap validatedPathToSchemas = new PathToSchemasMap(); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, validatedPathToSchemas, new LinkedHashSet<>()); - getPathToSchemas(this, castArg, validationMetadata, pathSet); - return castArg; - } - - @Override - public boolean validate(boolean arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0]"); - boolean castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - PathToSchemasMap validatedPathToSchemas = new PathToSchemasMap(); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, validatedPathToSchemas, new LinkedHashSet<>()); - getPathToSchemas(this, castArg, validationMetadata, pathSet); - return castArg; - } - - @Override - public Number validate(Number arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0]"); - Number castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - PathToSchemasMap validatedPathToSchemas = new PathToSchemasMap(); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, validatedPathToSchemas, new LinkedHashSet<>()); - getPathToSchemas(this, castArg, validationMetadata, pathSet); - return castArg; - } - - public int validate(int arg, SchemaConfiguration configuration) { - return (int) validate((Number) arg, configuration); - } - - public long validate(long arg, SchemaConfiguration configuration) { - return (long) validate((Number) arg, configuration); - } - - public float validate(float arg, SchemaConfiguration configuration) { - return (float) validate((Number) arg, configuration); - } - - public double validate(double arg, SchemaConfiguration configuration) { - return (double) validate((Number) arg, configuration); - } - - @Override - public String validate(String arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0]"); - String castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - PathToSchemasMap validatedPathToSchemas = new PathToSchemasMap(); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, validatedPathToSchemas, new LinkedHashSet<>()); - getPathToSchemas(this, castArg, validationMetadata, pathSet); - return castArg; - } - - public String validate(LocalDate arg, SchemaConfiguration configuration) throws ValidationException { - return validate(arg.toString(), configuration); - } - - public String validate(ZonedDateTime arg, SchemaConfiguration configuration) throws ValidationException { - return validate(arg.toString(), configuration); - } - - public String validate(UUID arg, SchemaConfiguration configuration) throws ValidationException { - return validate(arg.toString(), configuration); - } - - @Override - public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List<@Nullable Object> items = new ArrayList<>(); - int i = 0; - for (Object item: arg) { - List itemPathToItem = new ArrayList<>(pathToItem); - itemPathToItem.add(i); - LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); - if (schemas == null) { - throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); - } - JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); - @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(itemInstance); - i += 1; - } - FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); - return newInstanceItems; - } - - public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - List castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); - return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); - } - - @Override - public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); - for(Map.Entry entry: arg.entrySet()) { - @Nullable Object entryKey = entry.getKey(); - if (!(entryKey instanceof String)) { - throw new InvalidTypeException("Invalid non-string key value"); - } - String propertyName = (String) entryKey; - List propertyPathToItem = new ArrayList<>(pathToItem); - propertyPathToItem.add(propertyName); - Object value = entry.getValue(); - LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); - if (schemas == null) { - throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); - } - JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); - @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, propertyInstance); - } - FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); - return castProperties; - } - - public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0]"); - Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - PathToSchemasMap validatedPathToSchemas = new PathToSchemasMap(); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, validatedPathToSchemas, new LinkedHashSet<>()); - PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); - return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); - } - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg == null) { - return validate((Void) null, configuration); - } else if (arg instanceof Boolean) { - boolean boolArg = (Boolean) arg; - return validate(boolArg, configuration); - } else if (arg instanceof Number) { - return validate((Number) arg, configuration); - } else if (arg instanceof String) { - return validate((String) arg, configuration); - } else if (arg instanceof List) { - return validate((List) arg, configuration); - } else if (arg instanceof Map) { - return validate((Map) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg == null) { - return getNewInstance((Void) null, pathToItem, pathToSchemas); - } else if (arg instanceof Boolean) { - boolean boolArg = (Boolean) arg; - return getNewInstance(boolArg, pathToItem, pathToSchemas); - } else if (arg instanceof Number) { - return getNewInstance((Number) arg, pathToItem, pathToSchemas); - } else if (arg instanceof String) { - return getNewInstance((String) arg, pathToItem, pathToSchemas); - } else if (arg instanceof List) { - return getNewInstance((List) arg, pathToItem, pathToSchemas); - } else if (arg instanceof Map) { - return getNewInstance((Map) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeinlinecomposition/post/requestbody/content/multipart~1form-data/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeinlinecomposition/post/requestbody/content/multipart~1form-data/schema.java deleted file mode 100644 index 589d4278c0c..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeinlinecomposition/post/requestbody/content/multipart~1form-data/schema.java +++ /dev/null @@ -1,460 +0,0 @@ -package org.openapijsonschematools.client.paths.fakeinlinecomposition.post.requestbody.content.multipart~1form-data; -import java.time.LocalDate; -import java.time.ZonedDateTime; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import java.util.UUID; -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; -import org.openapijsonschematools.client.configurations.SchemaConfiguration; -import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; -import org.openapijsonschematools.client.exceptions.InvalidTypeException; -import org.openapijsonschematools.client.exceptions.UnsetPropertyException; -import org.openapijsonschematools.client.exceptions.ValidationException; -import org.openapijsonschematools.client.schemas.BaseBuilder; -import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; -import org.openapijsonschematools.client.schemas.validation.BooleanSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.FrozenList; -import org.openapijsonschematools.client.schemas.validation.FrozenMap; -import org.openapijsonschematools.client.schemas.validation.JsonSchema; -import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; -import org.openapijsonschematools.client.schemas.validation.ListSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.NullSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.NumberSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; -import org.openapijsonschematools.client.schemas.validation.PropertyEntry; -import org.openapijsonschematools.client.schemas.validation.StringSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; - -public class Schema { - // nest classes so all schemas and input/output classes can be public - - - public static class Schema0 extends JsonSchema implements StringSchemaValidator { - private static @Nullable Schema0 instance = null; - - protected Schema0() { - super(new JsonSchemaInfo() - .type(Set.of( - String.class - )) - .minLength(1) - ); - } - - public static Schema0 getInstance() { - if (instance == null) { - instance = new Schema0(); - } - return instance; - } - - @Override - public String validate(String arg, SchemaConfiguration configuration) throws ValidationException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - String castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - getPathToSchemas(this, castArg, validationMetadata, pathSet); - return castArg; - } - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof String) { - return validate((String) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof String) { - return getNewInstance((String) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - - public static class SomeProp extends JsonSchema implements NullSchemaValidator, BooleanSchemaValidator, NumberSchemaValidator, StringSchemaValidator, ListSchemaValidator>, MapSchemaValidator> { - private static @Nullable SomeProp instance = null; - - protected SomeProp() { - super(new JsonSchemaInfo() - .allOf(List.of( - Schema0.class - )) - ); - } - - public static SomeProp getInstance() { - if (instance == null) { - instance = new SomeProp(); - } - return instance; - } - - @Override - public Void validate(Void arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0]"); - Void castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - PathToSchemasMap validatedPathToSchemas = new PathToSchemasMap(); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, validatedPathToSchemas, new LinkedHashSet<>()); - getPathToSchemas(this, castArg, validationMetadata, pathSet); - return castArg; - } - - @Override - public boolean validate(boolean arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0]"); - boolean castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - PathToSchemasMap validatedPathToSchemas = new PathToSchemasMap(); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, validatedPathToSchemas, new LinkedHashSet<>()); - getPathToSchemas(this, castArg, validationMetadata, pathSet); - return castArg; - } - - @Override - public Number validate(Number arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0]"); - Number castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - PathToSchemasMap validatedPathToSchemas = new PathToSchemasMap(); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, validatedPathToSchemas, new LinkedHashSet<>()); - getPathToSchemas(this, castArg, validationMetadata, pathSet); - return castArg; - } - - public int validate(int arg, SchemaConfiguration configuration) { - return (int) validate((Number) arg, configuration); - } - - public long validate(long arg, SchemaConfiguration configuration) { - return (long) validate((Number) arg, configuration); - } - - public float validate(float arg, SchemaConfiguration configuration) { - return (float) validate((Number) arg, configuration); - } - - public double validate(double arg, SchemaConfiguration configuration) { - return (double) validate((Number) arg, configuration); - } - - @Override - public String validate(String arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0]"); - String castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - PathToSchemasMap validatedPathToSchemas = new PathToSchemasMap(); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, validatedPathToSchemas, new LinkedHashSet<>()); - getPathToSchemas(this, castArg, validationMetadata, pathSet); - return castArg; - } - - public String validate(LocalDate arg, SchemaConfiguration configuration) throws ValidationException { - return validate(arg.toString(), configuration); - } - - public String validate(ZonedDateTime arg, SchemaConfiguration configuration) throws ValidationException { - return validate(arg.toString(), configuration); - } - - public String validate(UUID arg, SchemaConfiguration configuration) throws ValidationException { - return validate(arg.toString(), configuration); - } - - @Override - public FrozenList<@Nullable Object> getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List<@Nullable Object> items = new ArrayList<>(); - int i = 0; - for (Object item: arg) { - List itemPathToItem = new ArrayList<>(pathToItem); - itemPathToItem.add(i); - LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); - if (schemas == null) { - throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); - } - JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); - @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - items.add(itemInstance); - i += 1; - } - FrozenList<@Nullable Object> newInstanceItems = new FrozenList<>(items); - return newInstanceItems; - } - - public FrozenList<@Nullable Object> validate(List arg, SchemaConfiguration configuration) throws ValidationException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - List castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); - return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); - } - - @Override - public FrozenMap<@Nullable Object> getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); - for(Map.Entry entry: arg.entrySet()) { - @Nullable Object entryKey = entry.getKey(); - if (!(entryKey instanceof String)) { - throw new InvalidTypeException("Invalid non-string key value"); - } - String propertyName = (String) entryKey; - List propertyPathToItem = new ArrayList<>(pathToItem); - propertyPathToItem.add(propertyName); - Object value = entry.getValue(); - LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); - if (schemas == null) { - throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); - } - JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); - @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, propertyInstance); - } - FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); - return castProperties; - } - - public FrozenMap<@Nullable Object> validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0]"); - Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - PathToSchemasMap validatedPathToSchemas = new PathToSchemasMap(); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, validatedPathToSchemas, new LinkedHashSet<>()); - PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); - return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); - } - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg == null) { - return validate((Void) null, configuration); - } else if (arg instanceof Boolean) { - boolean boolArg = (Boolean) arg; - return validate(boolArg, configuration); - } else if (arg instanceof Number) { - return validate((Number) arg, configuration); - } else if (arg instanceof String) { - return validate((String) arg, configuration); - } else if (arg instanceof List) { - return validate((List) arg, configuration); - } else if (arg instanceof Map) { - return validate((Map) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg == null) { - return getNewInstance((Void) null, pathToItem, pathToSchemas); - } else if (arg instanceof Boolean) { - boolean boolArg = (Boolean) arg; - return getNewInstance(boolArg, pathToItem, pathToSchemas); - } else if (arg instanceof Number) { - return getNewInstance((Number) arg, pathToItem, pathToSchemas); - } else if (arg instanceof String) { - return getNewInstance((String) arg, pathToItem, pathToSchemas); - } else if (arg instanceof List) { - return getNewInstance((List) arg, pathToItem, pathToSchemas); - } else if (arg instanceof Map) { - return getNewInstance((Map) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - - public static class SchemaMap extends FrozenMap<@Nullable Object> { - protected SchemaMap(FrozenMap<@Nullable Object> m) { - super(m); - } - public static final Set requiredKeys = Set.of(); - public static final Set optionalKeys = Set.of( - "someProp" - ); - public static SchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { - return Schema1.getInstance().validate(arg, configuration); - } - - public @Nullable Object someProp() throws UnsetPropertyException { - return getOrThrow("someProp"); - } - - public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { - throwIfKeyKnown(name, requiredKeys, optionalKeys); - throwIfKeyNotPresent(name); - return get(name); - } - } - - public interface SetterForSomeProp { - Map getInstance(); - T getBuilderAfterSomeProp(Map instance); - - default T someProp(Void value) { - var instance = getInstance(); - instance.put("someProp", null); - return getBuilderAfterSomeProp(instance); - } - - default T someProp(boolean value) { - var instance = getInstance(); - instance.put("someProp", value); - return getBuilderAfterSomeProp(instance); - } - - default T someProp(String value) { - var instance = getInstance(); - instance.put("someProp", value); - return getBuilderAfterSomeProp(instance); - } - - default T someProp(int value) { - var instance = getInstance(); - instance.put("someProp", value); - return getBuilderAfterSomeProp(instance); - } - - default T someProp(float value) { - var instance = getInstance(); - instance.put("someProp", value); - return getBuilderAfterSomeProp(instance); - } - - default T someProp(long value) { - var instance = getInstance(); - instance.put("someProp", value); - return getBuilderAfterSomeProp(instance); - } - - default T someProp(double value) { - var instance = getInstance(); - instance.put("someProp", value); - return getBuilderAfterSomeProp(instance); - } - - default T someProp(List value) { - var instance = getInstance(); - instance.put("someProp", value); - return getBuilderAfterSomeProp(instance); - } - - default T someProp(Map value) { - var instance = getInstance(); - instance.put("someProp", value); - return getBuilderAfterSomeProp(instance); - } - } - - public static class SchemaMapBuilder extends UnsetAddPropsSetter implements BaseBuilder<@Nullable Object>, SetterForSomeProp { - private final Map instance; - private static final Set knownKeys = Set.of( - "someProp" - ); - public Set getKnownKeys() { - return knownKeys; - } - public SchemaMapBuilder() { - this.instance = new LinkedHashMap<>(); - } - public Map build() { - return instance; - } - public Map getInstance() { - return instance; - } - public SchemaMapBuilder getBuilderAfterSomeProp(Map instance) { - return this; - } - public SchemaMapBuilder getBuilderAfterAdditionalProperty(Map instance) { - return this; - } - } - - - public static class Schema1 extends JsonSchema implements MapSchemaValidator { - private static @Nullable Schema1 instance = null; - - protected Schema1() { - super(new JsonSchemaInfo() - .type(Set.of(Map.class)) - .properties(Map.ofEntries( - new PropertyEntry("someProp", SomeProp.class) - )) - ); - } - - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - - public SchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); - for(Map.Entry entry: arg.entrySet()) { - @Nullable Object entryKey = entry.getKey(); - if (!(entryKey instanceof String)) { - throw new InvalidTypeException("Invalid non-string key value"); - } - String propertyName = (String) entryKey; - List propertyPathToItem = new ArrayList<>(pathToItem); - propertyPathToItem.add(propertyName); - Object value = entry.getValue(); - LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); - if (schemas == null) { - throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); - } - JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); - @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, propertyInstance); - } - FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); - return new SchemaMap(castProperties); - } - - public SchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); - return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); - } - - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof Map) { - return validate((Map) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof Map) { - return getNewInstance((Map) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakejsonformdata/get/requestbody/content/application~1x-www-form-urlencoded/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakejsonformdata/get/requestbody/content/application~1x-www-form-urlencoded/schema.java deleted file mode 100644 index c24d1b0dbdb..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakejsonformdata/get/requestbody/content/application~1x-www-form-urlencoded/schema.java +++ /dev/null @@ -1,252 +0,0 @@ -package org.openapijsonschematools.client.paths.fakejsonformdata.get.requestbody.content.application~1x-www-form-urlencoded; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; -import org.openapijsonschematools.client.configurations.SchemaConfiguration; -import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; -import org.openapijsonschematools.client.exceptions.InvalidTypeException; -import org.openapijsonschematools.client.exceptions.UnsetPropertyException; -import org.openapijsonschematools.client.exceptions.ValidationException; -import org.openapijsonschematools.client.schemas.BaseBuilder; -import org.openapijsonschematools.client.schemas.StringJsonSchema; -import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; -import org.openapijsonschematools.client.schemas.validation.FrozenMap; -import org.openapijsonschematools.client.schemas.validation.JsonSchema; -import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; -import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; -import org.openapijsonschematools.client.schemas.validation.PropertyEntry; -import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; - -public class Schema { - // nest classes so all schemas and input/output classes can be public - - - public static class Param extends StringJsonSchema { - private static @Nullable Param instance = null; - public static Param getInstance() { - if (instance == null) { - instance = new Param(); - } - return instance; - } - } - - - public static class Param2 extends StringJsonSchema { - private static @Nullable Param2 instance = null; - public static Param2 getInstance() { - if (instance == null) { - instance = new Param2(); - } - return instance; - } - } - - - public static class SchemaMap extends FrozenMap<@Nullable Object> { - protected SchemaMap(FrozenMap<@Nullable Object> m) { - super(m); - } - public static final Set requiredKeys = Set.of( - "param", - "param2" - ); - public static final Set optionalKeys = Set.of(); - public static SchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { - return Schema1.getInstance().validate(arg, configuration); - } - - public String param() { - @Nullable Object value = get("param"); - if (!(value instanceof String)) { - throw new InvalidTypeException("Invalid value stored for param"); - } - return (String) value; - } - - public String param2() { - @Nullable Object value = get("param2"); - if (!(value instanceof String)) { - throw new InvalidTypeException("Invalid value stored for param2"); - } - return (String) value; - } - - public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { - throwIfKeyKnown(name, requiredKeys, optionalKeys); - throwIfKeyNotPresent(name); - return get(name); - } - } - - public interface SetterForParam { - Map getInstance(); - T getBuilderAfterParam(Map instance); - - default T param(String value) { - var instance = getInstance(); - instance.put("param", value); - return getBuilderAfterParam(instance); - } - } - - public interface SetterForParam2 { - Map getInstance(); - T getBuilderAfterParam2(Map instance); - - default T param2(String value) { - var instance = getInstance(); - instance.put("param2", value); - return getBuilderAfterParam2(instance); - } - } - - public static class SchemaMap00Builder extends UnsetAddPropsSetter implements BaseBuilder<@Nullable Object> { - private final Map instance; - private static final Set knownKeys = Set.of( - "param", - "param2" - ); - public Set getKnownKeys() { - return knownKeys; - } - public SchemaMap00Builder(Map instance) { - this.instance = instance; - } - public Map build() { - return instance; - } - public Map getInstance() { - return instance; - } - public SchemaMap00Builder getBuilderAfterAdditionalProperty(Map instance) { - return this; - } - } - - public static class SchemaMap01Builder implements SetterForParam2 { - private final Map instance; - public SchemaMap01Builder(Map instance) { - this.instance = instance; - } - public Map getInstance() { - return instance; - } - public SchemaMap00Builder getBuilderAfterParam2(Map instance) { - return new SchemaMap00Builder(instance); - } - } - - public static class SchemaMap10Builder implements SetterForParam { - private final Map instance; - public SchemaMap10Builder(Map instance) { - this.instance = instance; - } - public Map getInstance() { - return instance; - } - public SchemaMap00Builder getBuilderAfterParam(Map instance) { - return new SchemaMap00Builder(instance); - } - } - - public static class SchemaMapBuilder implements SetterForParam, SetterForParam2 { - private final Map instance; - public SchemaMapBuilder() { - this.instance = new LinkedHashMap<>(); - } - public Map getInstance() { - return instance; - } - public SchemaMap01Builder getBuilderAfterParam(Map instance) { - return new SchemaMap01Builder(instance); - } - public SchemaMap10Builder getBuilderAfterParam2(Map instance) { - return new SchemaMap10Builder(instance); - } - } - - - public static class Schema1 extends JsonSchema implements MapSchemaValidator { - private static @Nullable Schema1 instance = null; - - protected Schema1() { - super(new JsonSchemaInfo() - .type(Set.of(Map.class)) - .properties(Map.ofEntries( - new PropertyEntry("param", Param.class), - new PropertyEntry("param2", Param2.class) - )) - .required(Set.of( - "param", - "param2" - )) - ); - } - - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - - public SchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); - for(Map.Entry entry: arg.entrySet()) { - @Nullable Object entryKey = entry.getKey(); - if (!(entryKey instanceof String)) { - throw new InvalidTypeException("Invalid non-string key value"); - } - String propertyName = (String) entryKey; - List propertyPathToItem = new ArrayList<>(pathToItem); - propertyPathToItem.add(propertyName); - Object value = entry.getValue(); - LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); - if (schemas == null) { - throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); - } - JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); - @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, propertyInstance); - } - FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); - return new SchemaMap(castProperties); - } - - public SchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); - return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); - } - - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof Map) { - return validate((Map) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof Map) { - return getNewInstance((Map) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakejsonpatch/patch/requestbody/content/application~1json-patch+json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakejsonpatch/patch/requestbody/content/application~1json-patch+json/schema.java deleted file mode 100644 index 2af6dde51a6..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakejsonpatch/patch/requestbody/content/application~1json-patch+json/schema.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.openapijsonschematools.client.paths.fakejsonpatch.patch.requestbody.content.application~1json-patch+json; - -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.components.schemas.JSONPatchRequest; - -public class Schema extends JSONPatchRequest { - // $refed class - - - public static class Schema1 extends JSONPatchRequest1 { - private static @Nullable Schema1 instance = null; - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - } -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakejsonwithcharset/post/requestbody/content/application~1json; charset=utf-8/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakejsonwithcharset/post/requestbody/content/application~1json; charset=utf-8/schema.java deleted file mode 100644 index 2fa66f01873..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakejsonwithcharset/post/requestbody/content/application~1json; charset=utf-8/schema.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.openapijsonschematools.client.paths.fakejsonwithcharset.post.requestbody.content.application~1json; charset=utf-8; -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.schemas.AnyTypeJsonSchema; - -public class Schema { - // nest classes so all schemas and input/output classes can be public - - - public static class Schema1 extends AnyTypeJsonSchema { - private static @Nullable Schema1 instance = null; - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - } - -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakemultiplerequestbodycontenttypes/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakemultiplerequestbodycontenttypes/post/requestbody/content/application~1json/schema.java deleted file mode 100644 index e7fa61f0583..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakemultiplerequestbodycontenttypes/post/requestbody/content/application~1json/schema.java +++ /dev/null @@ -1,178 +0,0 @@ -package org.openapijsonschematools.client.paths.fakemultiplerequestbodycontenttypes.post.requestbody.content.application~1json; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; -import org.openapijsonschematools.client.configurations.SchemaConfiguration; -import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; -import org.openapijsonschematools.client.exceptions.InvalidTypeException; -import org.openapijsonschematools.client.exceptions.UnsetPropertyException; -import org.openapijsonschematools.client.exceptions.ValidationException; -import org.openapijsonschematools.client.schemas.BaseBuilder; -import org.openapijsonschematools.client.schemas.StringJsonSchema; -import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; -import org.openapijsonschematools.client.schemas.validation.FrozenMap; -import org.openapijsonschematools.client.schemas.validation.JsonSchema; -import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; -import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; -import org.openapijsonschematools.client.schemas.validation.PropertyEntry; -import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; - -public class Schema { - // nest classes so all schemas and input/output classes can be public - - - public static class A extends StringJsonSchema { - private static @Nullable A instance = null; - public static A getInstance() { - if (instance == null) { - instance = new A(); - } - return instance; - } - } - - - public static class SchemaMap extends FrozenMap<@Nullable Object> { - protected SchemaMap(FrozenMap<@Nullable Object> m) { - super(m); - } - public static final Set requiredKeys = Set.of(); - public static final Set optionalKeys = Set.of( - "a" - ); - public static SchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { - return Schema1.getInstance().validate(arg, configuration); - } - - public String a() throws UnsetPropertyException { - String key = "a"; - throwIfKeyNotPresent(key); - @Nullable Object value = get(key); - if (!(value instanceof String)) { - throw new InvalidTypeException("Invalid value stored for a"); - } - return (String) value; - } - - public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { - throwIfKeyKnown(name, requiredKeys, optionalKeys); - throwIfKeyNotPresent(name); - return get(name); - } - } - - public interface SetterForA { - Map getInstance(); - T getBuilderAfterA(Map instance); - - default T a(String value) { - var instance = getInstance(); - instance.put("a", value); - return getBuilderAfterA(instance); - } - } - - public static class SchemaMapBuilder extends UnsetAddPropsSetter implements BaseBuilder<@Nullable Object>, SetterForA { - private final Map instance; - private static final Set knownKeys = Set.of( - "a" - ); - public Set getKnownKeys() { - return knownKeys; - } - public SchemaMapBuilder() { - this.instance = new LinkedHashMap<>(); - } - public Map build() { - return instance; - } - public Map getInstance() { - return instance; - } - public SchemaMapBuilder getBuilderAfterA(Map instance) { - return this; - } - public SchemaMapBuilder getBuilderAfterAdditionalProperty(Map instance) { - return this; - } - } - - - public static class Schema1 extends JsonSchema implements MapSchemaValidator { - private static @Nullable Schema1 instance = null; - - protected Schema1() { - super(new JsonSchemaInfo() - .type(Set.of(Map.class)) - .properties(Map.ofEntries( - new PropertyEntry("a", A.class) - )) - ); - } - - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - - public SchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); - for(Map.Entry entry: arg.entrySet()) { - @Nullable Object entryKey = entry.getKey(); - if (!(entryKey instanceof String)) { - throw new InvalidTypeException("Invalid non-string key value"); - } - String propertyName = (String) entryKey; - List propertyPathToItem = new ArrayList<>(pathToItem); - propertyPathToItem.add(propertyName); - Object value = entry.getValue(); - LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); - if (schemas == null) { - throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); - } - JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); - @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, propertyInstance); - } - FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); - return new SchemaMap(castProperties); - } - - public SchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); - return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); - } - - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof Map) { - return validate((Map) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof Map) { - return getNewInstance((Map) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakemultiplerequestbodycontenttypes/post/requestbody/content/multipart~1form-data/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakemultiplerequestbodycontenttypes/post/requestbody/content/multipart~1form-data/schema.java deleted file mode 100644 index 99b4af98078..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakemultiplerequestbodycontenttypes/post/requestbody/content/multipart~1form-data/schema.java +++ /dev/null @@ -1,178 +0,0 @@ -package org.openapijsonschematools.client.paths.fakemultiplerequestbodycontenttypes.post.requestbody.content.multipart~1form-data; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; -import org.openapijsonschematools.client.configurations.SchemaConfiguration; -import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; -import org.openapijsonschematools.client.exceptions.InvalidTypeException; -import org.openapijsonschematools.client.exceptions.UnsetPropertyException; -import org.openapijsonschematools.client.exceptions.ValidationException; -import org.openapijsonschematools.client.schemas.BaseBuilder; -import org.openapijsonschematools.client.schemas.StringJsonSchema; -import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; -import org.openapijsonschematools.client.schemas.validation.FrozenMap; -import org.openapijsonschematools.client.schemas.validation.JsonSchema; -import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; -import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; -import org.openapijsonschematools.client.schemas.validation.PropertyEntry; -import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; - -public class Schema { - // nest classes so all schemas and input/output classes can be public - - - public static class B extends StringJsonSchema { - private static @Nullable B instance = null; - public static B getInstance() { - if (instance == null) { - instance = new B(); - } - return instance; - } - } - - - public static class SchemaMap extends FrozenMap<@Nullable Object> { - protected SchemaMap(FrozenMap<@Nullable Object> m) { - super(m); - } - public static final Set requiredKeys = Set.of(); - public static final Set optionalKeys = Set.of( - "b" - ); - public static SchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { - return Schema1.getInstance().validate(arg, configuration); - } - - public String b() throws UnsetPropertyException { - String key = "b"; - throwIfKeyNotPresent(key); - @Nullable Object value = get(key); - if (!(value instanceof String)) { - throw new InvalidTypeException("Invalid value stored for b"); - } - return (String) value; - } - - public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { - throwIfKeyKnown(name, requiredKeys, optionalKeys); - throwIfKeyNotPresent(name); - return get(name); - } - } - - public interface SetterForB { - Map getInstance(); - T getBuilderAfterB(Map instance); - - default T b(String value) { - var instance = getInstance(); - instance.put("b", value); - return getBuilderAfterB(instance); - } - } - - public static class SchemaMapBuilder extends UnsetAddPropsSetter implements BaseBuilder<@Nullable Object>, SetterForB { - private final Map instance; - private static final Set knownKeys = Set.of( - "b" - ); - public Set getKnownKeys() { - return knownKeys; - } - public SchemaMapBuilder() { - this.instance = new LinkedHashMap<>(); - } - public Map build() { - return instance; - } - public Map getInstance() { - return instance; - } - public SchemaMapBuilder getBuilderAfterB(Map instance) { - return this; - } - public SchemaMapBuilder getBuilderAfterAdditionalProperty(Map instance) { - return this; - } - } - - - public static class Schema1 extends JsonSchema implements MapSchemaValidator { - private static @Nullable Schema1 instance = null; - - protected Schema1() { - super(new JsonSchemaInfo() - .type(Set.of(Map.class)) - .properties(Map.ofEntries( - new PropertyEntry("b", B.class) - )) - ); - } - - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - - public SchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); - for(Map.Entry entry: arg.entrySet()) { - @Nullable Object entryKey = entry.getKey(); - if (!(entryKey instanceof String)) { - throw new InvalidTypeException("Invalid non-string key value"); - } - String propertyName = (String) entryKey; - List propertyPathToItem = new ArrayList<>(pathToItem); - propertyPathToItem.add(propertyName); - Object value = entry.getValue(); - LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); - if (schemas == null) { - throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); - } - JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); - @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, propertyInstance); - } - FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); - return new SchemaMap(castProperties); - } - - public SchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); - return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); - } - - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof Map) { - return validate((Map) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof Map) { - return getNewInstance((Map) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeparametercollisions1ababselfab/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeparametercollisions1ababselfab/post/requestbody/content/application~1json/schema.java deleted file mode 100644 index f894fbce42e..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeparametercollisions1ababselfab/post/requestbody/content/application~1json/schema.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.openapijsonschematools.client.paths.fakeparametercollisions1ababselfab.post.requestbody.content.application~1json; -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.schemas.AnyTypeJsonSchema; - -public class Schema { - // nest classes so all schemas and input/output classes can be public - - - public static class Schema1 extends AnyTypeJsonSchema { - private static @Nullable Schema1 instance = null; - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - } - -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakepemcontenttype/get/requestbody/content/application~1x-pem-file/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakepemcontenttype/get/requestbody/content/application~1x-pem-file/schema.java deleted file mode 100644 index fb6dc292870..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakepemcontenttype/get/requestbody/content/application~1x-pem-file/schema.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.openapijsonschematools.client.paths.fakepemcontenttype.get.requestbody.content.application~1x-pem-file; -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.schemas.StringJsonSchema; - -public class Schema { - // nest classes so all schemas and input/output classes can be public - - - public static class Schema1 extends StringJsonSchema { - private static @Nullable Schema1 instance = null; - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - } - -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakepetiduploadimagewithrequiredfile/post/requestbody/content/multipart~1form-data/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakepetiduploadimagewithrequiredfile/post/requestbody/content/multipart~1form-data/schema.java deleted file mode 100644 index 36775e5a490..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakepetiduploadimagewithrequiredfile/post/requestbody/content/multipart~1form-data/schema.java +++ /dev/null @@ -1,229 +0,0 @@ -package org.openapijsonschematools.client.paths.fakepetiduploadimagewithrequiredfile.post.requestbody.content.multipart~1form-data; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; -import org.openapijsonschematools.client.configurations.SchemaConfiguration; -import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; -import org.openapijsonschematools.client.exceptions.InvalidTypeException; -import org.openapijsonschematools.client.exceptions.UnsetPropertyException; -import org.openapijsonschematools.client.exceptions.ValidationException; -import org.openapijsonschematools.client.schemas.BaseBuilder; -import org.openapijsonschematools.client.schemas.StringJsonSchema; -import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; -import org.openapijsonschematools.client.schemas.validation.FrozenMap; -import org.openapijsonschematools.client.schemas.validation.JsonSchema; -import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; -import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; -import org.openapijsonschematools.client.schemas.validation.PropertyEntry; -import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; - -public class Schema { - // nest classes so all schemas and input/output classes can be public - - - public static class AdditionalMetadata extends StringJsonSchema { - private static @Nullable AdditionalMetadata instance = null; - public static AdditionalMetadata getInstance() { - if (instance == null) { - instance = new AdditionalMetadata(); - } - return instance; - } - } - - - public static class RequiredFile extends StringJsonSchema { - // BinarySchema - private static @Nullable RequiredFile instance = null; - public static RequiredFile getInstance() { - if (instance == null) { - instance = new RequiredFile(); - } - return instance; - } - } - - - public static class SchemaMap extends FrozenMap<@Nullable Object> { - protected SchemaMap(FrozenMap<@Nullable Object> m) { - super(m); - } - public static final Set requiredKeys = Set.of( - "requiredFile" - ); - public static final Set optionalKeys = Set.of( - "additionalMetadata" - ); - public static SchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { - return Schema1.getInstance().validate(arg, configuration); - } - - public String requiredFile() { - @Nullable Object value = get("requiredFile"); - if (!(value instanceof String)) { - throw new InvalidTypeException("Invalid value stored for requiredFile"); - } - return (String) value; - } - - public String additionalMetadata() throws UnsetPropertyException { - String key = "additionalMetadata"; - throwIfKeyNotPresent(key); - @Nullable Object value = get(key); - if (!(value instanceof String)) { - throw new InvalidTypeException("Invalid value stored for additionalMetadata"); - } - return (String) value; - } - - public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { - throwIfKeyKnown(name, requiredKeys, optionalKeys); - throwIfKeyNotPresent(name); - return get(name); - } - } - - public interface SetterForRequiredFile { - Map getInstance(); - T getBuilderAfterRequiredFile(Map instance); - - default T requiredFile(String value) { - var instance = getInstance(); - instance.put("requiredFile", value); - return getBuilderAfterRequiredFile(instance); - } - } - - public interface SetterForAdditionalMetadata { - Map getInstance(); - T getBuilderAfterAdditionalMetadata(Map instance); - - default T additionalMetadata(String value) { - var instance = getInstance(); - instance.put("additionalMetadata", value); - return getBuilderAfterAdditionalMetadata(instance); - } - } - - public static class SchemaMap0Builder extends UnsetAddPropsSetter implements BaseBuilder<@Nullable Object>, SetterForAdditionalMetadata { - private final Map instance; - private static final Set knownKeys = Set.of( - "requiredFile", - "additionalMetadata" - ); - public Set getKnownKeys() { - return knownKeys; - } - public SchemaMap0Builder(Map instance) { - this.instance = instance; - } - public Map build() { - return instance; - } - public Map getInstance() { - return instance; - } - public SchemaMap0Builder getBuilderAfterAdditionalMetadata(Map instance) { - return this; - } - public SchemaMap0Builder getBuilderAfterAdditionalProperty(Map instance) { - return this; - } - } - - public static class SchemaMapBuilder implements SetterForRequiredFile { - private final Map instance; - public SchemaMapBuilder() { - this.instance = new LinkedHashMap<>(); - } - public Map getInstance() { - return instance; - } - public SchemaMap0Builder getBuilderAfterRequiredFile(Map instance) { - return new SchemaMap0Builder(instance); - } - } - - - public static class Schema1 extends JsonSchema implements MapSchemaValidator { - private static @Nullable Schema1 instance = null; - - protected Schema1() { - super(new JsonSchemaInfo() - .type(Set.of(Map.class)) - .properties(Map.ofEntries( - new PropertyEntry("additionalMetadata", AdditionalMetadata.class), - new PropertyEntry("requiredFile", RequiredFile.class) - )) - .required(Set.of( - "requiredFile" - )) - ); - } - - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - - public SchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); - for(Map.Entry entry: arg.entrySet()) { - @Nullable Object entryKey = entry.getKey(); - if (!(entryKey instanceof String)) { - throw new InvalidTypeException("Invalid non-string key value"); - } - String propertyName = (String) entryKey; - List propertyPathToItem = new ArrayList<>(pathToItem); - propertyPathToItem.add(propertyName); - Object value = entry.getValue(); - LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); - if (schemas == null) { - throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); - } - JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); - @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, propertyInstance); - } - FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); - return new SchemaMap(castProperties); - } - - public SchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); - return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); - } - - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof Map) { - return validate((Map) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof Map) { - return getNewInstance((Map) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsarraymodel/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsarraymodel/post/requestbody/content/application~1json/schema.java deleted file mode 100644 index 849da921912..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsarraymodel/post/requestbody/content/application~1json/schema.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.openapijsonschematools.client.paths.fakerefsarraymodel.post.requestbody.content.application~1json; - -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.components.schemas.AnimalFarm; - -public class Schema extends AnimalFarm { - // $refed class - - - public static class Schema1 extends AnimalFarm1 { - private static @Nullable Schema1 instance = null; - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - } -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsarrayofenums/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsarrayofenums/post/requestbody/content/application~1json/schema.java deleted file mode 100644 index f707b458d8b..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsarrayofenums/post/requestbody/content/application~1json/schema.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.openapijsonschematools.client.paths.fakerefsarrayofenums.post.requestbody.content.application~1json; - -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.components.schemas.ArrayOfEnums; - -public class Schema extends ArrayOfEnums { - // $refed class - - - public static class Schema1 extends ArrayOfEnums1 { - private static @Nullable Schema1 instance = null; - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - } -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsboolean/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsboolean/post/requestbody/content/application~1json/schema.java deleted file mode 100644 index 8ca249e844f..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsboolean/post/requestbody/content/application~1json/schema.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.openapijsonschematools.client.paths.fakerefsboolean.post.requestbody.content.application~1json; - -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.components.schemas.BooleanSchema; - -public class Schema extends BooleanSchema { - // $refed class - - - public static class Schema1 extends BooleanSchema1 { - private static @Nullable Schema1 instance = null; - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - } -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefscomposedoneofnumberwithvalidations/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefscomposedoneofnumberwithvalidations/post/requestbody/content/application~1json/schema.java deleted file mode 100644 index 76242966da3..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefscomposedoneofnumberwithvalidations/post/requestbody/content/application~1json/schema.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.openapijsonschematools.client.paths.fakerefscomposedoneofnumberwithvalidations.post.requestbody.content.application~1json; - -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.components.schemas.ComposedOneOfDifferentTypes; - -public class Schema extends ComposedOneOfDifferentTypes { - // $refed class - - - public static class Schema1 extends ComposedOneOfDifferentTypes1 { - private static @Nullable Schema1 instance = null; - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - } -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsenum/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsenum/post/requestbody/content/application~1json/schema.java deleted file mode 100644 index 22e8580a10b..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsenum/post/requestbody/content/application~1json/schema.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.openapijsonschematools.client.paths.fakerefsenum.post.requestbody.content.application~1json; - -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.components.schemas.StringEnum; - -public class Schema extends StringEnum { - // $refed class - - - public static class Schema1 extends StringEnum1 { - private static @Nullable Schema1 instance = null; - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - } -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsmammal/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsmammal/post/requestbody/content/application~1json/schema.java deleted file mode 100644 index 42353c30024..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsmammal/post/requestbody/content/application~1json/schema.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.openapijsonschematools.client.paths.fakerefsmammal.post.requestbody.content.application~1json; - -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.components.schemas.Mammal; - -public class Schema extends Mammal { - // $refed class - - - public static class Schema1 extends Mammal1 { - private static @Nullable Schema1 instance = null; - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - } -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsnumber/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsnumber/post/requestbody/content/application~1json/schema.java deleted file mode 100644 index 5481ce53783..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsnumber/post/requestbody/content/application~1json/schema.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.openapijsonschematools.client.paths.fakerefsnumber.post.requestbody.content.application~1json; - -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.components.schemas.NumberWithValidations; - -public class Schema extends NumberWithValidations { - // $refed class - - - public static class Schema1 extends NumberWithValidations1 { - private static @Nullable Schema1 instance = null; - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - } -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsobjectmodelwithrefprops/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsobjectmodelwithrefprops/post/requestbody/content/application~1json/schema.java deleted file mode 100644 index a9d97ee8e93..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsobjectmodelwithrefprops/post/requestbody/content/application~1json/schema.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.openapijsonschematools.client.paths.fakerefsobjectmodelwithrefprops.post.requestbody.content.application~1json; - -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.components.schemas.ObjectModelWithRefProps; - -public class Schema extends ObjectModelWithRefProps { - // $refed class - - - public static class Schema1 extends ObjectModelWithRefProps1 { - private static @Nullable Schema1 instance = null; - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - } -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsstring/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsstring/post/requestbody/content/application~1json/schema.java deleted file mode 100644 index b9c6891a65e..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakerefsstring/post/requestbody/content/application~1json/schema.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.openapijsonschematools.client.paths.fakerefsstring.post.requestbody.content.application~1json; - -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.components.schemas.StringSchema; - -public class Schema extends StringSchema { - // $refed class - - - public static class Schema1 extends StringSchema1 { - private static @Nullable Schema1 instance = null; - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - } -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeuploaddownloadfile/post/requestbody/content/application~1octet-stream/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeuploaddownloadfile/post/requestbody/content/application~1octet-stream/schema.java deleted file mode 100644 index c82ae1000c7..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeuploaddownloadfile/post/requestbody/content/application~1octet-stream/schema.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.openapijsonschematools.client.paths.fakeuploaddownloadfile.post.requestbody.content.application~1octet-stream; -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.schemas.StringJsonSchema; - -public class Schema { - // nest classes so all schemas and input/output classes can be public - - - public static class Schema1 extends StringJsonSchema { - // BinarySchema - private static @Nullable Schema1 instance = null; - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - } - -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeuploadfile/post/requestbody/content/multipart~1form-data/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeuploadfile/post/requestbody/content/multipart~1form-data/schema.java deleted file mode 100644 index 2f069b0091b..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeuploadfile/post/requestbody/content/multipart~1form-data/schema.java +++ /dev/null @@ -1,229 +0,0 @@ -package org.openapijsonschematools.client.paths.fakeuploadfile.post.requestbody.content.multipart~1form-data; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; -import org.openapijsonschematools.client.configurations.SchemaConfiguration; -import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; -import org.openapijsonschematools.client.exceptions.InvalidTypeException; -import org.openapijsonschematools.client.exceptions.UnsetPropertyException; -import org.openapijsonschematools.client.exceptions.ValidationException; -import org.openapijsonschematools.client.schemas.BaseBuilder; -import org.openapijsonschematools.client.schemas.StringJsonSchema; -import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; -import org.openapijsonschematools.client.schemas.validation.FrozenMap; -import org.openapijsonschematools.client.schemas.validation.JsonSchema; -import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; -import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; -import org.openapijsonschematools.client.schemas.validation.PropertyEntry; -import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; - -public class Schema { - // nest classes so all schemas and input/output classes can be public - - - public static class AdditionalMetadata extends StringJsonSchema { - private static @Nullable AdditionalMetadata instance = null; - public static AdditionalMetadata getInstance() { - if (instance == null) { - instance = new AdditionalMetadata(); - } - return instance; - } - } - - - public static class File extends StringJsonSchema { - // BinarySchema - private static @Nullable File instance = null; - public static File getInstance() { - if (instance == null) { - instance = new File(); - } - return instance; - } - } - - - public static class SchemaMap extends FrozenMap<@Nullable Object> { - protected SchemaMap(FrozenMap<@Nullable Object> m) { - super(m); - } - public static final Set requiredKeys = Set.of( - "file" - ); - public static final Set optionalKeys = Set.of( - "additionalMetadata" - ); - public static SchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { - return Schema1.getInstance().validate(arg, configuration); - } - - public String file() { - @Nullable Object value = get("file"); - if (!(value instanceof String)) { - throw new InvalidTypeException("Invalid value stored for file"); - } - return (String) value; - } - - public String additionalMetadata() throws UnsetPropertyException { - String key = "additionalMetadata"; - throwIfKeyNotPresent(key); - @Nullable Object value = get(key); - if (!(value instanceof String)) { - throw new InvalidTypeException("Invalid value stored for additionalMetadata"); - } - return (String) value; - } - - public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { - throwIfKeyKnown(name, requiredKeys, optionalKeys); - throwIfKeyNotPresent(name); - return get(name); - } - } - - public interface SetterForFile { - Map getInstance(); - T getBuilderAfterFile(Map instance); - - default T file(String value) { - var instance = getInstance(); - instance.put("file", value); - return getBuilderAfterFile(instance); - } - } - - public interface SetterForAdditionalMetadata { - Map getInstance(); - T getBuilderAfterAdditionalMetadata(Map instance); - - default T additionalMetadata(String value) { - var instance = getInstance(); - instance.put("additionalMetadata", value); - return getBuilderAfterAdditionalMetadata(instance); - } - } - - public static class SchemaMap0Builder extends UnsetAddPropsSetter implements BaseBuilder<@Nullable Object>, SetterForAdditionalMetadata { - private final Map instance; - private static final Set knownKeys = Set.of( - "file", - "additionalMetadata" - ); - public Set getKnownKeys() { - return knownKeys; - } - public SchemaMap0Builder(Map instance) { - this.instance = instance; - } - public Map build() { - return instance; - } - public Map getInstance() { - return instance; - } - public SchemaMap0Builder getBuilderAfterAdditionalMetadata(Map instance) { - return this; - } - public SchemaMap0Builder getBuilderAfterAdditionalProperty(Map instance) { - return this; - } - } - - public static class SchemaMapBuilder implements SetterForFile { - private final Map instance; - public SchemaMapBuilder() { - this.instance = new LinkedHashMap<>(); - } - public Map getInstance() { - return instance; - } - public SchemaMap0Builder getBuilderAfterFile(Map instance) { - return new SchemaMap0Builder(instance); - } - } - - - public static class Schema1 extends JsonSchema implements MapSchemaValidator { - private static @Nullable Schema1 instance = null; - - protected Schema1() { - super(new JsonSchemaInfo() - .type(Set.of(Map.class)) - .properties(Map.ofEntries( - new PropertyEntry("additionalMetadata", AdditionalMetadata.class), - new PropertyEntry("file", File.class) - )) - .required(Set.of( - "file" - )) - ); - } - - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - - public SchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); - for(Map.Entry entry: arg.entrySet()) { - @Nullable Object entryKey = entry.getKey(); - if (!(entryKey instanceof String)) { - throw new InvalidTypeException("Invalid non-string key value"); - } - String propertyName = (String) entryKey; - List propertyPathToItem = new ArrayList<>(pathToItem); - propertyPathToItem.add(propertyName); - Object value = entry.getValue(); - LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); - if (schemas == null) { - throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); - } - JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); - @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, propertyInstance); - } - FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); - return new SchemaMap(castProperties); - } - - public SchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); - return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); - } - - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof Map) { - return validate((Map) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof Map) { - return getNewInstance((Map) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeuploadfiles/post/requestbody/content/multipart~1form-data/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeuploadfiles/post/requestbody/content/multipart~1form-data/schema.java deleted file mode 100644 index 2274b590ae5..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/fakeuploadfiles/post/requestbody/content/multipart~1form-data/schema.java +++ /dev/null @@ -1,279 +0,0 @@ -package org.openapijsonschematools.client.paths.fakeuploadfiles.post.requestbody.content.multipart~1form-data; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; -import org.openapijsonschematools.client.configurations.SchemaConfiguration; -import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; -import org.openapijsonschematools.client.exceptions.InvalidTypeException; -import org.openapijsonschematools.client.exceptions.UnsetPropertyException; -import org.openapijsonschematools.client.exceptions.ValidationException; -import org.openapijsonschematools.client.schemas.BaseBuilder; -import org.openapijsonschematools.client.schemas.StringJsonSchema; -import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; -import org.openapijsonschematools.client.schemas.validation.FrozenList; -import org.openapijsonschematools.client.schemas.validation.FrozenMap; -import org.openapijsonschematools.client.schemas.validation.JsonSchema; -import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; -import org.openapijsonschematools.client.schemas.validation.ListSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; -import org.openapijsonschematools.client.schemas.validation.PropertyEntry; -import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; - -public class Schema { - // nest classes so all schemas and input/output classes can be public - - - public static class Items extends StringJsonSchema { - // BinarySchema - private static @Nullable Items instance = null; - public static Items getInstance() { - if (instance == null) { - instance = new Items(); - } - return instance; - } - } - - - public static class FilesList extends FrozenList { - protected FilesList(FrozenList m) { - super(m); - } - public static FilesList of(List arg, SchemaConfiguration configuration) throws ValidationException { - return Files.getInstance().validate(arg, configuration); - } - } - - public static class FilesListBuilder { - // class to build List - private final List list; - - public FilesListBuilder() { - list = new ArrayList<>(); - } - - public FilesListBuilder(List list) { - this.list = list; - } - - public FilesListBuilder add(String item) { - list.add(item); - return this; - } - - public List build() { - return list; - } - } - - - public static class Files extends JsonSchema implements ListSchemaValidator { - private static @Nullable Files instance = null; - - protected Files() { - super(new JsonSchemaInfo() - .type(Set.of(List.class)) - .items(Items.class) - ); - } - - public static Files getInstance() { - if (instance == null) { - instance = new Files(); - } - return instance; - } - - @Override - public FilesList getNewInstance(List arg, List pathToItem, PathToSchemasMap pathToSchemas) { - List items = new ArrayList<>(); - int i = 0; - for (Object item: arg) { - List itemPathToItem = new ArrayList<>(pathToItem); - itemPathToItem.add(i); - LinkedHashMap schemas = pathToSchemas.get(itemPathToItem); - if (schemas == null) { - throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); - } - JsonSchema itemSchema = schemas.entrySet().iterator().next().getKey(); - @Nullable Object itemInstance = itemSchema.getNewInstance(item, itemPathToItem, pathToSchemas); - if (!(itemInstance instanceof String)) { - throw new InvalidTypeException("Invalid instantiated value"); - } - items.add((String) itemInstance); - i += 1; - } - FrozenList newInstanceItems = new FrozenList<>(items); - return new FilesList(newInstanceItems); - } - - public FilesList validate(List arg, SchemaConfiguration configuration) throws ValidationException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - List castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); - return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); - } - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof List) { - return validate((List) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof List) { - return getNewInstance((List) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - - public static class SchemaMap extends FrozenMap<@Nullable Object> { - protected SchemaMap(FrozenMap<@Nullable Object> m) { - super(m); - } - public static final Set requiredKeys = Set.of(); - public static final Set optionalKeys = Set.of( - "files" - ); - public static SchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { - return Schema1.getInstance().validate(arg, configuration); - } - - public FilesList files() throws UnsetPropertyException { - String key = "files"; - throwIfKeyNotPresent(key); - @Nullable Object value = get(key); - if (!(value instanceof FilesList)) { - throw new InvalidTypeException("Invalid value stored for files"); - } - return (FilesList) value; - } - - public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { - throwIfKeyKnown(name, requiredKeys, optionalKeys); - throwIfKeyNotPresent(name); - return get(name); - } - } - - public interface SetterForFiles { - Map getInstance(); - T getBuilderAfterFiles(Map instance); - - default T files(List value) { - var instance = getInstance(); - instance.put("files", value); - return getBuilderAfterFiles(instance); - } - } - - public static class SchemaMapBuilder extends UnsetAddPropsSetter implements BaseBuilder<@Nullable Object>, SetterForFiles { - private final Map instance; - private static final Set knownKeys = Set.of( - "files" - ); - public Set getKnownKeys() { - return knownKeys; - } - public SchemaMapBuilder() { - this.instance = new LinkedHashMap<>(); - } - public Map build() { - return instance; - } - public Map getInstance() { - return instance; - } - public SchemaMapBuilder getBuilderAfterFiles(Map instance) { - return this; - } - public SchemaMapBuilder getBuilderAfterAdditionalProperty(Map instance) { - return this; - } - } - - - public static class Schema1 extends JsonSchema implements MapSchemaValidator { - private static @Nullable Schema1 instance = null; - - protected Schema1() { - super(new JsonSchemaInfo() - .type(Set.of(Map.class)) - .properties(Map.ofEntries( - new PropertyEntry("files", Files.class) - )) - ); - } - - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - - public SchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); - for(Map.Entry entry: arg.entrySet()) { - @Nullable Object entryKey = entry.getKey(); - if (!(entryKey instanceof String)) { - throw new InvalidTypeException("Invalid non-string key value"); - } - String propertyName = (String) entryKey; - List propertyPathToItem = new ArrayList<>(pathToItem); - propertyPathToItem.add(propertyName); - Object value = entry.getValue(); - LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); - if (schemas == null) { - throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); - } - JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); - @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, propertyInstance); - } - FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); - return new SchemaMap(castProperties); - } - - public SchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); - return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); - } - - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof Map) { - return validate((Map) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof Map) { - return getNewInstance((Map) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petpetid/post/requestbody/content/application~1x-www-form-urlencoded/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petpetid/post/requestbody/content/application~1x-www-form-urlencoded/schema.java deleted file mode 100644 index df12e25231e..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petpetid/post/requestbody/content/application~1x-www-form-urlencoded/schema.java +++ /dev/null @@ -1,216 +0,0 @@ -package org.openapijsonschematools.client.paths.petpetid.post.requestbody.content.application~1x-www-form-urlencoded; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; -import org.openapijsonschematools.client.configurations.SchemaConfiguration; -import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; -import org.openapijsonschematools.client.exceptions.InvalidTypeException; -import org.openapijsonschematools.client.exceptions.UnsetPropertyException; -import org.openapijsonschematools.client.exceptions.ValidationException; -import org.openapijsonschematools.client.schemas.BaseBuilder; -import org.openapijsonschematools.client.schemas.StringJsonSchema; -import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; -import org.openapijsonschematools.client.schemas.validation.FrozenMap; -import org.openapijsonschematools.client.schemas.validation.JsonSchema; -import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; -import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; -import org.openapijsonschematools.client.schemas.validation.PropertyEntry; -import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; - -public class Schema { - // nest classes so all schemas and input/output classes can be public - - - public static class Name extends StringJsonSchema { - private static @Nullable Name instance = null; - public static Name getInstance() { - if (instance == null) { - instance = new Name(); - } - return instance; - } - } - - - public static class Status extends StringJsonSchema { - private static @Nullable Status instance = null; - public static Status getInstance() { - if (instance == null) { - instance = new Status(); - } - return instance; - } - } - - - public static class SchemaMap extends FrozenMap<@Nullable Object> { - protected SchemaMap(FrozenMap<@Nullable Object> m) { - super(m); - } - public static final Set requiredKeys = Set.of(); - public static final Set optionalKeys = Set.of( - "name", - "status" - ); - public static SchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { - return Schema1.getInstance().validate(arg, configuration); - } - - public String name() throws UnsetPropertyException { - String key = "name"; - throwIfKeyNotPresent(key); - @Nullable Object value = get(key); - if (!(value instanceof String)) { - throw new InvalidTypeException("Invalid value stored for name"); - } - return (String) value; - } - - public String status() throws UnsetPropertyException { - String key = "status"; - throwIfKeyNotPresent(key); - @Nullable Object value = get(key); - if (!(value instanceof String)) { - throw new InvalidTypeException("Invalid value stored for status"); - } - return (String) value; - } - - public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { - throwIfKeyKnown(name, requiredKeys, optionalKeys); - throwIfKeyNotPresent(name); - return get(name); - } - } - - public interface SetterForName { - Map getInstance(); - T getBuilderAfterName(Map instance); - - default T name(String value) { - var instance = getInstance(); - instance.put("name", value); - return getBuilderAfterName(instance); - } - } - - public interface SetterForStatus { - Map getInstance(); - T getBuilderAfterStatus(Map instance); - - default T status(String value) { - var instance = getInstance(); - instance.put("status", value); - return getBuilderAfterStatus(instance); - } - } - - public static class SchemaMapBuilder extends UnsetAddPropsSetter implements BaseBuilder<@Nullable Object>, SetterForName, SetterForStatus { - private final Map instance; - private static final Set knownKeys = Set.of( - "name", - "status" - ); - public Set getKnownKeys() { - return knownKeys; - } - public SchemaMapBuilder() { - this.instance = new LinkedHashMap<>(); - } - public Map build() { - return instance; - } - public Map getInstance() { - return instance; - } - public SchemaMapBuilder getBuilderAfterName(Map instance) { - return this; - } - public SchemaMapBuilder getBuilderAfterStatus(Map instance) { - return this; - } - public SchemaMapBuilder getBuilderAfterAdditionalProperty(Map instance) { - return this; - } - } - - - public static class Schema1 extends JsonSchema implements MapSchemaValidator { - private static @Nullable Schema1 instance = null; - - protected Schema1() { - super(new JsonSchemaInfo() - .type(Set.of(Map.class)) - .properties(Map.ofEntries( - new PropertyEntry("name", Name.class), - new PropertyEntry("status", Status.class) - )) - ); - } - - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - - public SchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); - for(Map.Entry entry: arg.entrySet()) { - @Nullable Object entryKey = entry.getKey(); - if (!(entryKey instanceof String)) { - throw new InvalidTypeException("Invalid non-string key value"); - } - String propertyName = (String) entryKey; - List propertyPathToItem = new ArrayList<>(pathToItem); - propertyPathToItem.add(propertyName); - Object value = entry.getValue(); - LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); - if (schemas == null) { - throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); - } - JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); - @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, propertyInstance); - } - FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); - return new SchemaMap(castProperties); - } - - public SchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); - return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); - } - - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof Map) { - return validate((Map) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof Map) { - return getNewInstance((Map) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petpetiduploadimage/post/requestbody/content/multipart~1form-data/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petpetiduploadimage/post/requestbody/content/multipart~1form-data/schema.java deleted file mode 100644 index aacf8cf4e7e..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petpetiduploadimage/post/requestbody/content/multipart~1form-data/schema.java +++ /dev/null @@ -1,217 +0,0 @@ -package org.openapijsonschematools.client.paths.petpetiduploadimage.post.requestbody.content.multipart~1form-data; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags; -import org.openapijsonschematools.client.configurations.SchemaConfiguration; -import org.openapijsonschematools.client.exceptions.InvalidAdditionalPropertyException; -import org.openapijsonschematools.client.exceptions.InvalidTypeException; -import org.openapijsonschematools.client.exceptions.UnsetPropertyException; -import org.openapijsonschematools.client.exceptions.ValidationException; -import org.openapijsonschematools.client.schemas.BaseBuilder; -import org.openapijsonschematools.client.schemas.StringJsonSchema; -import org.openapijsonschematools.client.schemas.UnsetAddPropsSetter; -import org.openapijsonschematools.client.schemas.validation.FrozenMap; -import org.openapijsonschematools.client.schemas.validation.JsonSchema; -import org.openapijsonschematools.client.schemas.validation.JsonSchemaInfo; -import org.openapijsonschematools.client.schemas.validation.MapSchemaValidator; -import org.openapijsonschematools.client.schemas.validation.PathToSchemasMap; -import org.openapijsonschematools.client.schemas.validation.PropertyEntry; -import org.openapijsonschematools.client.schemas.validation.ValidationMetadata; - -public class Schema { - // nest classes so all schemas and input/output classes can be public - - - public static class AdditionalMetadata extends StringJsonSchema { - private static @Nullable AdditionalMetadata instance = null; - public static AdditionalMetadata getInstance() { - if (instance == null) { - instance = new AdditionalMetadata(); - } - return instance; - } - } - - - public static class File extends StringJsonSchema { - // BinarySchema - private static @Nullable File instance = null; - public static File getInstance() { - if (instance == null) { - instance = new File(); - } - return instance; - } - } - - - public static class SchemaMap extends FrozenMap<@Nullable Object> { - protected SchemaMap(FrozenMap<@Nullable Object> m) { - super(m); - } - public static final Set requiredKeys = Set.of(); - public static final Set optionalKeys = Set.of( - "additionalMetadata", - "file" - ); - public static SchemaMap of(Map arg, SchemaConfiguration configuration) throws ValidationException { - return Schema1.getInstance().validate(arg, configuration); - } - - public String additionalMetadata() throws UnsetPropertyException { - String key = "additionalMetadata"; - throwIfKeyNotPresent(key); - @Nullable Object value = get(key); - if (!(value instanceof String)) { - throw new InvalidTypeException("Invalid value stored for additionalMetadata"); - } - return (String) value; - } - - public String file() throws UnsetPropertyException { - String key = "file"; - throwIfKeyNotPresent(key); - @Nullable Object value = get(key); - if (!(value instanceof String)) { - throw new InvalidTypeException("Invalid value stored for file"); - } - return (String) value; - } - - public @Nullable Object getAdditionalProperty(String name) throws UnsetPropertyException, InvalidAdditionalPropertyException { - throwIfKeyKnown(name, requiredKeys, optionalKeys); - throwIfKeyNotPresent(name); - return get(name); - } - } - - public interface SetterForAdditionalMetadata { - Map getInstance(); - T getBuilderAfterAdditionalMetadata(Map instance); - - default T additionalMetadata(String value) { - var instance = getInstance(); - instance.put("additionalMetadata", value); - return getBuilderAfterAdditionalMetadata(instance); - } - } - - public interface SetterForFile { - Map getInstance(); - T getBuilderAfterFile(Map instance); - - default T file(String value) { - var instance = getInstance(); - instance.put("file", value); - return getBuilderAfterFile(instance); - } - } - - public static class SchemaMapBuilder extends UnsetAddPropsSetter implements BaseBuilder<@Nullable Object>, SetterForAdditionalMetadata, SetterForFile { - private final Map instance; - private static final Set knownKeys = Set.of( - "additionalMetadata", - "file" - ); - public Set getKnownKeys() { - return knownKeys; - } - public SchemaMapBuilder() { - this.instance = new LinkedHashMap<>(); - } - public Map build() { - return instance; - } - public Map getInstance() { - return instance; - } - public SchemaMapBuilder getBuilderAfterAdditionalMetadata(Map instance) { - return this; - } - public SchemaMapBuilder getBuilderAfterFile(Map instance) { - return this; - } - public SchemaMapBuilder getBuilderAfterAdditionalProperty(Map instance) { - return this; - } - } - - - public static class Schema1 extends JsonSchema implements MapSchemaValidator { - private static @Nullable Schema1 instance = null; - - protected Schema1() { - super(new JsonSchemaInfo() - .type(Set.of(Map.class)) - .properties(Map.ofEntries( - new PropertyEntry("additionalMetadata", AdditionalMetadata.class), - new PropertyEntry("file", File.class) - )) - ); - } - - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - - public SchemaMap getNewInstance(Map arg, List pathToItem, PathToSchemasMap pathToSchemas) { - LinkedHashMap properties = new LinkedHashMap<>(); - for(Map.Entry entry: arg.entrySet()) { - @Nullable Object entryKey = entry.getKey(); - if (!(entryKey instanceof String)) { - throw new InvalidTypeException("Invalid non-string key value"); - } - String propertyName = (String) entryKey; - List propertyPathToItem = new ArrayList<>(pathToItem); - propertyPathToItem.add(propertyName); - Object value = entry.getValue(); - LinkedHashMap schemas = pathToSchemas.get(propertyPathToItem); - if (schemas == null) { - throw new InvalidTypeException("Validation result is invalid, schemas must exist for a pathToItem"); - } - JsonSchema propertySchema = schemas.entrySet().iterator().next().getKey(); - @Nullable Object propertyInstance = propertySchema.getNewInstance(value, propertyPathToItem, pathToSchemas); - properties.put(propertyName, propertyInstance); - } - FrozenMap<@Nullable Object> castProperties = new FrozenMap<>(properties); - return new SchemaMap(castProperties); - } - - public SchemaMap validate(Map arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - Set> pathSet = new HashSet<>(); - List pathToItem = List.of("args[0"); - Map castArg = castToAllowedTypes(arg, pathToItem, pathSet); - SchemaConfiguration usedConfiguration = Objects.requireNonNullElseGet(configuration, () -> new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone())); - ValidationMetadata validationMetadata = new ValidationMetadata(pathToItem, usedConfiguration, new PathToSchemasMap(), new LinkedHashSet<>()); - PathToSchemasMap pathToSchemasMap = getPathToSchemas(this, castArg, validationMetadata, pathSet); - return getNewInstance(castArg, validationMetadata.pathToItem(), pathToSchemasMap); - } - - - @Override - public @Nullable Object validate(@Nullable Object arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException { - if (arg instanceof Map) { - return validate((Map) arg, configuration); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be validated by this schema"); - } - @Override - public @Nullable Object getNewInstance(@Nullable Object arg, List pathToItem, PathToSchemasMap pathToSchemas) throws InvalidTypeException { - if (arg instanceof Map) { - return getNewInstance((Map) arg, pathToItem, pathToSchemas); - } - throw new InvalidTypeException("Invalid input type="+getClass(arg)+". It can't be instantiated by this schema"); - } - } - -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/storeorder/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/storeorder/post/requestbody/content/application~1json/schema.java deleted file mode 100644 index c4c611c68fe..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/storeorder/post/requestbody/content/application~1json/schema.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.openapijsonschematools.client.paths.storeorder.post.requestbody.content.application~1json; - -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.components.schemas.Order; - -public class Schema extends Order { - // $refed class - - - public static class Schema1 extends Order1 { - private static @Nullable Schema1 instance = null; - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - } -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/user/post/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/user/post/requestbody/content/application~1json/schema.java deleted file mode 100644 index c3ae5581bc1..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/user/post/requestbody/content/application~1json/schema.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.openapijsonschematools.client.paths.user.post.requestbody.content.application~1json; - -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.components.schemas.User; - -public class Schema extends User { - // $refed class - - - public static class Schema1 extends User1 { - private static @Nullable Schema1 instance = null; - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - } -} diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/userusername/put/requestbody/content/application~1json/schema.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/userusername/put/requestbody/content/application~1json/schema.java deleted file mode 100644 index 169d047fefd..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/userusername/put/requestbody/content/application~1json/schema.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.openapijsonschematools.client.paths.userusername.put.requestbody.content.application~1json; - -import org.checkerframework.checker.nullness.qual.Nullable; -import org.openapijsonschematools.client.components.schemas.User; - -public class Schema extends User { - // $refed class - - - public static class Schema1 extends User1 { - private static @Nullable Schema1 instance = null; - public static Schema1 getInstance() { - if (instance == null) { - instance = new Schema1(); - } - return instance; - } - } -} diff --git a/src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java b/src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java index 87e02c3050f..5ace420b7e8 100644 --- a/src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java +++ b/src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java @@ -3917,6 +3917,7 @@ private void updatePathsFilepath(String[] pathPieces) { String contentType = ModelUtils.decodeSlashes(pathPieces[6]); pathPieces[6] = toContentTypeFilename(contentType); if (pathPieces.length == 8) { + // #/paths/somePath/get/requestBody/content/application-json/schema pathPieces[7] = getSchemaFilename(jsonPath); } } From 1d06d35d10ef7dbf4ec8da08d51bca465520d473 Mon Sep 17 00:00:00 2001 From: Justin Black Date: Tue, 23 Jan 2024 17:05:51 -0800 Subject: [PATCH 4/6] Adds classes to store servers for each jsonPath including root servers --- .../client/Servers.java | 24 ++++++++++++++++--- .../paths/foo/get/FooServerservers.java | 19 +++++++++++++-- .../PetfindbystatusServers.java | 19 +++++++++++++-- .../main/java/packagename/servers/Servers.hbs | 20 +++++++++++++++- 4 files changed, 74 insertions(+), 8 deletions(-) diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/Servers.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/Servers.java index 53a165c353f..e5a699b3be6 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/Servers.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/Servers.java @@ -1,13 +1,31 @@ package org.openapijsonschematools.client.servers; +import org.openapijsonschematools.client.servers.Server0; +import org.openapijsonschematools.client.servers.Server1; +import org.openapijsonschematools.client.servers.Server2; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.Objects; + public class Servers { - private Server0 server0; - private Server1 server1; - private Server2 server2; + final private Server0 server0; + final private Server1 server1; + final private Server2 server2; public Servers() { server0 = new Server0(); server1 = new Server1(); server2 = new Server2(); } + + public Servers( + @Nullable Server0 server0, + @Nullable Server1 server1, + @Nullable Server2 server2 + ) { + this.server0 = Objects.requireNonNullElseGet(server0, Server0::new); + this.server1 = Objects.requireNonNullElseGet(server1, Server1::new); + this.server2 = Objects.requireNonNullElseGet(server2, Server2::new); + } + } \ No newline at end of file diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/FooServerservers.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/FooServerservers.java index 85c9cc1ed0b..b846109fa6c 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/FooServerservers.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/FooServerservers.java @@ -1,11 +1,26 @@ package org.openapijsonschematools.client.paths.foo.get.servers; +import org.openapijsonschematools.client.paths.foo.get.servers.FooGetServer0; +import org.openapijsonschematools.client.paths.foo.get.servers.FooGetServer1; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.Objects; + public class FooServerservers { - private FooGetServer0 server0; - private FooGetServer1 server1; + final private FooGetServer0 server0; + final private FooGetServer1 server1; public FooServerservers() { server0 = new FooGetServer0(); server1 = new FooGetServer1(); } + + public FooServerservers( + @Nullable FooGetServer0 server0, + @Nullable FooGetServer1 server1 + ) { + this.server0 = Objects.requireNonNullElseGet(server0, () -> new FooGetServer0(); + this.server1 = Objects.requireNonNullElseGet(server1, () -> new FooGetServer1(); + } + } \ No newline at end of file diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/PetfindbystatusServers.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/PetfindbystatusServers.java index 6f3bc6a94f5..c044fe369d2 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/PetfindbystatusServers.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/PetfindbystatusServers.java @@ -1,11 +1,26 @@ package org.openapijsonschematools.client.paths.petfindbystatus.servers; +import org.openapijsonschematools.client.paths.petfindbystatus.servers.PetfindbystatusServer0; +import org.openapijsonschematools.client.paths.petfindbystatus.servers.PetfindbystatusServer1; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.Objects; + public class PetfindbystatusServers { - private PetfindbystatusServer0 server0; - private PetfindbystatusServer1 server1; + final private PetfindbystatusServer0 server0; + final private PetfindbystatusServer1 server1; public PetfindbystatusServers() { server0 = new PetfindbystatusServer0(); server1 = new PetfindbystatusServer1(); } + + public PetfindbystatusServers( + @Nullable PetfindbystatusServer0 server0, + @Nullable PetfindbystatusServer1 server1 + ) { + this.server0 = Objects.requireNonNullElseGet(server0, () -> new PetfindbystatusServer0(); + this.server1 = Objects.requireNonNullElseGet(server1, () -> new PetfindbystatusServer1(); + } + } \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/servers/Servers.hbs b/src/main/resources/java/src/main/java/packagename/servers/Servers.hbs index 5175db573b1..f64e30e4a02 100644 --- a/src/main/resources/java/src/main/java/packagename/servers/Servers.hbs +++ b/src/main/resources/java/src/main/java/packagename/servers/Servers.hbs @@ -2,9 +2,16 @@ package {{{packageName}}}.{{subpackage}}; {{/with}} +{{#each servers}} +import {{{packageName}}}.{{subpackage}}.{{jsonPathPiece.pascalCase}}; +{{/each}} +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.Objects; + public class {{servers.jsonPathPiece.pascalCase}} { {{#each servers}} - private {{jsonPathPiece.pascalCase}} server{{@index}}; + final private {{jsonPathPiece.pascalCase}} server{{@index}}; {{/each}} public {{servers.jsonPathPiece.pascalCase}}() { @@ -12,4 +19,15 @@ public class {{servers.jsonPathPiece.pascalCase}} { server{{@index}} = new {{jsonPathPiece.pascalCase}}(); {{/each}} } + + public {{servers.jsonPathPiece.pascalCase}}( + {{#each servers}} + @Nullable {{jsonPathPiece.pascalCase}} server{{@index}}{{#unless @last}},{{/unless}} + {{/each}} + ) { + {{#each servers}} + this.server{{@index}} = Objects.requireNonNullElseGet(server{{@index}}, {{jsonPathPiece.pascalCase}}::new); + {{/each}} + } + } \ No newline at end of file From 176a5375177845a3505c0609d37fdaf81ff567c6 Mon Sep 17 00:00:00 2001 From: Justin Black Date: Wed, 24 Jan 2024 14:03:11 -0800 Subject: [PATCH 5/6] ServerInfo classes updated --- .../petstore/java/.openapi-generator/FILES | 8 +- .../client/ServerInfo.java | 91 +++++++++++++++++++ .../client/Servers.java | 31 ------- .../paths/foo/get/FooGetServerInfo.java | 80 ++++++++++++++++ .../paths/foo/get/FooServerservers.java | 26 ------ .../PetfindbystatusServerInfo.java | 80 ++++++++++++++++ .../PetfindbystatusServers.java | 26 ------ .../client/servers/Server.java | 6 ++ .../client/servers/ServerProvider.java | 8 ++ .../client/servers/ServerWithVariables.java | 6 +- .../servers/ServerWithoutVariables.java | 6 +- .../codegen/generators/DefaultGenerator.java | 35 ++++--- .../codegen/generators/Generator.java | 3 +- .../generators/JavaClientGenerator.java | 58 +++++++----- .../openapimodels/CodegenOperation.java | 4 +- .../openapimodels/CodegenPathItem.java | 4 +- .../openapimodels/CodegenServers.java | 30 ++++++ .../main/java/packagename/servers/Server.hbs | 53 +---------- .../java/packagename/servers/ServerInfo.hbs | 83 +++++++++++++++++ .../main/java/packagename/servers/ServerN.hbs | 53 +++++++++++ .../packagename/servers/ServerProvider.hbs | 8 ++ .../servers/ServerWithVariables.hbs | 6 +- .../servers/ServerWithoutVariables.hbs | 6 +- .../main/java/packagename/servers/Servers.hbs | 33 ------- .../DefaultGeneratorRunnerTest.java | 9 +- 25 files changed, 534 insertions(+), 219 deletions(-) create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/ServerInfo.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/Servers.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/FooGetServerInfo.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/FooServerservers.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/PetfindbystatusServerInfo.java delete mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/PetfindbystatusServers.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server.java create mode 100644 samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/ServerProvider.java create mode 100644 src/main/java/org/openapijsonschematools/codegen/generators/openapimodels/CodegenServers.java create mode 100644 src/main/resources/java/src/main/java/packagename/servers/ServerInfo.hbs create mode 100644 src/main/resources/java/src/main/java/packagename/servers/ServerN.hbs create mode 100644 src/main/resources/java/src/main/java/packagename/servers/ServerProvider.hbs delete mode 100644 src/main/resources/java/src/main/java/packagename/servers/Servers.hbs diff --git a/samples/client/petstore/java/.openapi-generator/FILES b/samples/client/petstore/java/.openapi-generator/FILES index 111d171a1f4..b99f93a452c 100644 --- a/samples/client/petstore/java/.openapi-generator/FILES +++ b/samples/client/petstore/java/.openapi-generator/FILES @@ -305,7 +305,7 @@ docs/servers/Server0.md docs/servers/Server1.md docs/servers/Server2.md pom.xml -src/main/java/org/openapijsonschematools/client/Servers.java +src/main/java/org/openapijsonschematools/client/ServerInfo.java src/main/java/org/openapijsonschematools/client/components/headers/int32jsoncontenttypeheader/content/applicationjson/Int32JsonContentTypeHeaderSchema.java src/main/java/org/openapijsonschematools/client/components/headers/numberheader/NumberHeaderSchema.java src/main/java/org/openapijsonschematools/client/components/headers/refcontentschemaheader/content/applicationjson/RefContentSchemaHeaderSchema.java @@ -613,12 +613,12 @@ src/main/java/org/openapijsonschematools/client/paths/fakewildcardresponses/get/ src/main/java/org/openapijsonschematools/client/paths/fakewildcardresponses/get/responses/response3xx/content/applicationjson/Schema.java src/main/java/org/openapijsonschematools/client/paths/fakewildcardresponses/get/responses/response4xx/content/applicationjson/Schema.java src/main/java/org/openapijsonschematools/client/paths/fakewildcardresponses/get/responses/response5xx/content/applicationjson/Schema.java -src/main/java/org/openapijsonschematools/client/paths/foo/get/FooServerservers.java +src/main/java/org/openapijsonschematools/client/paths/foo/get/FooGetServerInfo.java src/main/java/org/openapijsonschematools/client/paths/foo/get/responses/responsedefault/content/applicationjson/Schema.java src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/FooGetServer0.java src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/FooGetServer1.java src/main/java/org/openapijsonschematools/client/paths/foo/get/servers/server1/Variables.java -src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/PetfindbystatusServers.java +src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/PetfindbystatusServerInfo.java src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/get/QueryParameters.java src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/get/parameters/parameter0/Schema0.java src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/servers/PetfindbystatusServer0.java @@ -759,9 +759,11 @@ src/main/java/org/openapijsonschematools/client/schemas/validation/UniqueItemsVa src/main/java/org/openapijsonschematools/client/schemas/validation/UnsetAnyTypeJsonSchema.java src/main/java/org/openapijsonschematools/client/schemas/validation/ValidationData.java src/main/java/org/openapijsonschematools/client/schemas/validation/ValidationMetadata.java +src/main/java/org/openapijsonschematools/client/servers/Server.java src/main/java/org/openapijsonschematools/client/servers/Server0.java src/main/java/org/openapijsonschematools/client/servers/Server1.java src/main/java/org/openapijsonschematools/client/servers/Server2.java +src/main/java/org/openapijsonschematools/client/servers/ServerProvider.java src/main/java/org/openapijsonschematools/client/servers/ServerWithVariables.java src/main/java/org/openapijsonschematools/client/servers/ServerWithoutVariables.java src/main/java/org/openapijsonschematools/client/servers/server0/Variables.java diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/ServerInfo.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/ServerInfo.java new file mode 100644 index 00000000000..fa811efde6b --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/ServerInfo.java @@ -0,0 +1,91 @@ +package org.openapijsonschematools.client; + +import org.openapijsonschematools.client.servers.Server0; +import org.openapijsonschematools.client.servers.Server1; +import org.openapijsonschematools.client.servers.Server2; +import org.openapijsonschematools.client.servers.Server; +import org.openapijsonschematools.client.servers.ServerProvider; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.AbstractMap; +import java.util.Map; +import java.util.Objects; +import java.util.EnumMap; + +public class ServerInfo implements ServerProvider { + final private Servers servers; + final private ServerIndex serverIndex; + + public ServerInfo() { + this.servers = new Servers(); + this.serverIndex = ServerIndex.SERVER_0; + } + + public ServerInfo(Servers servers, ServerIndex serverIndex) { + this.servers = servers; + this.serverIndex = serverIndex; + } + + public static class Servers { + private final EnumMap servers; + + public Servers() { + servers = new EnumMap<>( + Map.ofEntries( + new AbstractMap.SimpleEntry<>( + ServerIndex.SERVER_0, + new Server0() + ), + new AbstractMap.SimpleEntry<>( + ServerIndex.SERVER_1, + new Server1() + ), + new AbstractMap.SimpleEntry<>( + ServerIndex.SERVER_2, + new Server2() + ) + ) + ); + } + + public Servers( + @Nullable Server0 server0, + @Nullable Server1 server1, + @Nullable Server2 server2 + ) { + servers = new EnumMap<>( + Map.ofEntries( + new AbstractMap.SimpleEntry<>( + ServerIndex.SERVER_0, + Objects.requireNonNullElseGet(server0, Server0::new) + ), + new AbstractMap.SimpleEntry<>( + ServerIndex.SERVER_1, + Objects.requireNonNullElseGet(server1, Server1::new) + ), + new AbstractMap.SimpleEntry<>( + ServerIndex.SERVER_2, + Objects.requireNonNullElseGet(server2, Server2::new) + ) + ) + ); + } + + public Server get(ServerIndex serverIndex) { + return servers.get(serverIndex); + } + } + + public enum ServerIndex { + SERVER_0, + SERVER_1, + SERVER_2 + } + + public Server get(@Nullable ServerIndex serverIndex) { + if (serverIndex == null) { + return servers.get(this.serverIndex); + } + return servers.get(serverIndex); + } +} \ No newline at end of file diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/Servers.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/Servers.java deleted file mode 100644 index e5a699b3be6..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/Servers.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.openapijsonschematools.client.servers; - -import org.openapijsonschematools.client.servers.Server0; -import org.openapijsonschematools.client.servers.Server1; -import org.openapijsonschematools.client.servers.Server2; -import org.checkerframework.checker.nullness.qual.Nullable; - -import java.util.Objects; - -public class Servers { - final private Server0 server0; - final private Server1 server1; - final private Server2 server2; - - public Servers() { - server0 = new Server0(); - server1 = new Server1(); - server2 = new Server2(); - } - - public Servers( - @Nullable Server0 server0, - @Nullable Server1 server1, - @Nullable Server2 server2 - ) { - this.server0 = Objects.requireNonNullElseGet(server0, Server0::new); - this.server1 = Objects.requireNonNullElseGet(server1, Server1::new); - this.server2 = Objects.requireNonNullElseGet(server2, Server2::new); - } - -} \ No newline at end of file diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/FooGetServerInfo.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/FooGetServerInfo.java new file mode 100644 index 00000000000..cd44a6232da --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/FooGetServerInfo.java @@ -0,0 +1,80 @@ +package org.openapijsonschematools.client; + +import org.openapijsonschematools.client.paths.foo.get.servers.FooGetServer0; +import org.openapijsonschematools.client.paths.foo.get.servers.FooGetServer1; +import org.openapijsonschematools.client.servers.Server; +import org.openapijsonschematools.client.servers.ServerProvider; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.AbstractMap; +import java.util.Map; +import java.util.Objects; +import java.util.EnumMap; + +public class FooGetServerInfo implements ServerProvider { + final private Servers servers; + final private ServerIndex serverIndex; + + public FooGetServerInfo() { + this.servers = new Servers(); + this.serverIndex = ServerIndex.SERVER_0; + } + + public FooGetServerInfo(Servers servers, ServerIndex serverIndex) { + this.servers = servers; + this.serverIndex = serverIndex; + } + + public static class Servers { + private final EnumMap servers; + + public Servers() { + servers = new EnumMap<>( + Map.ofEntries( + new AbstractMap.SimpleEntry<>( + ServerIndex.SERVER_0, + new FooGetServer0() + ), + new AbstractMap.SimpleEntry<>( + ServerIndex.SERVER_1, + new FooGetServer1() + ) + ) + ); + } + + public Servers( + @Nullable FooGetServer0 server0, + @Nullable FooGetServer1 server1 + ) { + servers = new EnumMap<>( + Map.ofEntries( + new AbstractMap.SimpleEntry<>( + ServerIndex.SERVER_0, + Objects.requireNonNullElseGet(server0, FooGetServer0::new) + ), + new AbstractMap.SimpleEntry<>( + ServerIndex.SERVER_1, + Objects.requireNonNullElseGet(server1, FooGetServer1::new) + ) + ) + ); + } + + public Server get(ServerIndex serverIndex) { + return servers.get(serverIndex); + } + } + + public enum ServerIndex { + SERVER_0, + SERVER_1 + } + + public Server get(@Nullable ServerIndex serverIndex) { + if (serverIndex == null) { + return servers.get(this.serverIndex); + } + return servers.get(serverIndex); + } +} \ No newline at end of file diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/FooServerservers.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/FooServerservers.java deleted file mode 100644 index b846109fa6c..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/FooServerservers.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.openapijsonschematools.client.paths.foo.get.servers; - -import org.openapijsonschematools.client.paths.foo.get.servers.FooGetServer0; -import org.openapijsonschematools.client.paths.foo.get.servers.FooGetServer1; -import org.checkerframework.checker.nullness.qual.Nullable; - -import java.util.Objects; - -public class FooServerservers { - final private FooGetServer0 server0; - final private FooGetServer1 server1; - - public FooServerservers() { - server0 = new FooGetServer0(); - server1 = new FooGetServer1(); - } - - public FooServerservers( - @Nullable FooGetServer0 server0, - @Nullable FooGetServer1 server1 - ) { - this.server0 = Objects.requireNonNullElseGet(server0, () -> new FooGetServer0(); - this.server1 = Objects.requireNonNullElseGet(server1, () -> new FooGetServer1(); - } - -} \ No newline at end of file diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/PetfindbystatusServerInfo.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/PetfindbystatusServerInfo.java new file mode 100644 index 00000000000..16332392b52 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/PetfindbystatusServerInfo.java @@ -0,0 +1,80 @@ +package org.openapijsonschematools.client; + +import org.openapijsonschematools.client.paths.petfindbystatus.servers.PetfindbystatusServer0; +import org.openapijsonschematools.client.paths.petfindbystatus.servers.PetfindbystatusServer1; +import org.openapijsonschematools.client.servers.Server; +import org.openapijsonschematools.client.servers.ServerProvider; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.AbstractMap; +import java.util.Map; +import java.util.Objects; +import java.util.EnumMap; + +public class PetfindbystatusServerInfo implements ServerProvider { + final private Servers servers; + final private ServerIndex serverIndex; + + public PetfindbystatusServerInfo() { + this.servers = new Servers(); + this.serverIndex = ServerIndex.SERVER_0; + } + + public PetfindbystatusServerInfo(Servers servers, ServerIndex serverIndex) { + this.servers = servers; + this.serverIndex = serverIndex; + } + + public static class Servers { + private final EnumMap servers; + + public Servers() { + servers = new EnumMap<>( + Map.ofEntries( + new AbstractMap.SimpleEntry<>( + ServerIndex.SERVER_0, + new PetfindbystatusServer0() + ), + new AbstractMap.SimpleEntry<>( + ServerIndex.SERVER_1, + new PetfindbystatusServer1() + ) + ) + ); + } + + public Servers( + @Nullable PetfindbystatusServer0 server0, + @Nullable PetfindbystatusServer1 server1 + ) { + servers = new EnumMap<>( + Map.ofEntries( + new AbstractMap.SimpleEntry<>( + ServerIndex.SERVER_0, + Objects.requireNonNullElseGet(server0, PetfindbystatusServer0::new) + ), + new AbstractMap.SimpleEntry<>( + ServerIndex.SERVER_1, + Objects.requireNonNullElseGet(server1, PetfindbystatusServer1::new) + ) + ) + ); + } + + public Server get(ServerIndex serverIndex) { + return servers.get(serverIndex); + } + } + + public enum ServerIndex { + SERVER_0, + SERVER_1 + } + + public Server get(@Nullable ServerIndex serverIndex) { + if (serverIndex == null) { + return servers.get(this.serverIndex); + } + return servers.get(serverIndex); + } +} \ No newline at end of file diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/PetfindbystatusServers.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/PetfindbystatusServers.java deleted file mode 100644 index c044fe369d2..00000000000 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/PetfindbystatusServers.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.openapijsonschematools.client.paths.petfindbystatus.servers; - -import org.openapijsonschematools.client.paths.petfindbystatus.servers.PetfindbystatusServer0; -import org.openapijsonschematools.client.paths.petfindbystatus.servers.PetfindbystatusServer1; -import org.checkerframework.checker.nullness.qual.Nullable; - -import java.util.Objects; - -public class PetfindbystatusServers { - final private PetfindbystatusServer0 server0; - final private PetfindbystatusServer1 server1; - - public PetfindbystatusServers() { - server0 = new PetfindbystatusServer0(); - server1 = new PetfindbystatusServer1(); - } - - public PetfindbystatusServers( - @Nullable PetfindbystatusServer0 server0, - @Nullable PetfindbystatusServer1 server1 - ) { - this.server0 = Objects.requireNonNullElseGet(server0, () -> new PetfindbystatusServer0(); - this.server1 = Objects.requireNonNullElseGet(server1, () -> new PetfindbystatusServer1(); - } - -} \ No newline at end of file diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server.java new file mode 100644 index 00000000000..f5159644ccc --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/Server.java @@ -0,0 +1,6 @@ +package org.openapijsonschematools.client.servers; + +public interface Server { + String url(); +} + diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/ServerProvider.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/ServerProvider.java new file mode 100644 index 00000000000..1550bc12f31 --- /dev/null +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/ServerProvider.java @@ -0,0 +1,8 @@ +package org.openapijsonschematools.client.servers; + +import org.checkerframework.checker.nullness.qual.Nullable; + +public interface ServerProvider { + Server get(@Nullable T serverIndex); +} + diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithVariables.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithVariables.java index acdb6d730ed..68748bcfcae 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithVariables.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithVariables.java @@ -2,7 +2,7 @@ import java.util.Map; -public abstract class ServerWithVariables> { +public abstract class ServerWithVariables> implements Server { public final String url; public final T variables; @@ -13,5 +13,9 @@ protected ServerWithVariables(String url, T variables) { } this.url = url; } + + public String url(){ + return url; + } } diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithoutVariables.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithoutVariables.java index 903ac3f4c7d..c41ac2e77e5 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithoutVariables.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithoutVariables.java @@ -1,10 +1,14 @@ package org.openapijsonschematools.client.servers; -public abstract class ServerWithoutVariables { +public abstract class ServerWithoutVariables implements Server { public final String url; protected ServerWithoutVariables(String url) { this.url = url; } + + public String url(){ + return url; + } } diff --git a/src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java b/src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java index 5ace420b7e8..b384a7ba02c 100644 --- a/src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java +++ b/src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java @@ -68,6 +68,7 @@ import org.openapijsonschematools.codegen.generators.openapimodels.CodegenSecurityRequirementValue; import org.openapijsonschematools.codegen.generators.openapimodels.CodegenSecurityScheme; import org.openapijsonschematools.codegen.generators.openapimodels.CodegenServer; +import org.openapijsonschematools.codegen.generators.openapimodels.CodegenServers; import org.openapijsonschematools.codegen.generators.openapimodels.CodegenTag; import org.openapijsonschematools.codegen.generators.openapimodels.CodegenText; import org.openapijsonschematools.codegen.generators.openapimodels.CodegenXml; @@ -2674,7 +2675,7 @@ public CodegenOperation fromOperation(Operation operation, String jsonPath, Link CodegenKey operationId = getOperationId(operation, path, httpMethod); // servers setting - List codegenServers = null; + CodegenServers codegenServers = null; if (operation.getServers() != null && !operation.getServers().isEmpty()) { // use operation-level servers first if defined codegenServers = fromServers(operation.getServers(), jsonPath + "/servers"); @@ -2869,10 +2870,10 @@ public CodegenOperation fromOperation(Operation operation, String jsonPath, Link } operationParameters = new ParameterCollection(allParams, pathParams, queryParams, headerParams, cookieParams); } - ArrayList pathItemQueryParams = null; - ArrayList pathItemPathParams = null; - ArrayList pathItemHeaderParams = null; - ArrayList pathItemCookieParams = null; + ArrayList pathItemQueryParams; + ArrayList pathItemPathParams; + ArrayList pathItemHeaderParams; + ArrayList pathItemCookieParams; ParameterCollection pathItemParams = null; if (!usedPathItemParameters.isEmpty()) { pathItemQueryParams = new ArrayList<>(); @@ -3927,7 +3928,6 @@ private void updatePathsFilepath(String[] pathPieces) { protected void updateServersFilepath(String[] pathPieces) { if (pathPieces.length == 2) { // #/servers - return; } else if (pathPieces.length == 3) { // #/servers/0 String jsonPath = "#/servers/" + pathPieces[2]; @@ -4006,6 +4006,9 @@ public String getSubpackage(String jsonPath) { .collect(Collectors.toList()); String subpackage = String.join(".", finalPathPieces); int lastPeriodIndex = subpackage.lastIndexOf("."); + if (lastPeriodIndex < 1) { + return null; + } return subpackage.substring(1,lastPeriodIndex); } @Override @@ -5046,7 +5049,7 @@ public CodegenPathItem fromPathItem(PathItem pathItem, String jsonPath) { // sort them operations = new TreeMap<>(operations); List specServers = pathItem.getServers(); - ArrayListWithContext servers = fromServers(specServers, jsonPath + "/servers"); + CodegenServers servers = fromServers(specServers, jsonPath + "/servers"); return new CodegenPathItem( summary, @@ -5058,14 +5061,14 @@ public CodegenPathItem fromPathItem(PathItem pathItem, String jsonPath) { } @Override - public ArrayListWithContext fromServers(List servers, String jsonPath) { + public CodegenServers fromServers(List servers, String jsonPath) { if (servers == null) { return null; } - ArrayListWithContext codegenServers = new ArrayListWithContext<>(); - int i = 0; + List codegenServers = new ArrayList<>(); boolean rootServer = jsonPath.equals("#/servers"); - for (Server server : servers) { + for (int i = 0; i < servers.size(); i++) { + Server server = servers.get(i); String serverJsonPath = jsonPath + "/" + i; CodegenKey jsonPathPiece = getKey(String.valueOf(i), "servers", serverJsonPath); CodegenText description = getCodegenText(server.getDescription()); @@ -5079,11 +5082,13 @@ public ArrayListWithContext fromServers(List servers, Str subpackage ); codegenServers.add(cs); - i ++; } - CodegenKey serversJsonPathPiece = getKey("s", "servers", jsonPath); - codegenServers.setJsonPathPiece(serversJsonPathPiece); - return codegenServers; + CodegenKey jsonPathPiece = getKey("s", "servers", jsonPath); + return new CodegenServers( + codegenServers, + jsonPathPiece, + getSubpackage(jsonPath) + ); } @Override diff --git a/src/main/java/org/openapijsonschematools/codegen/generators/Generator.java b/src/main/java/org/openapijsonschematools/codegen/generators/Generator.java index 690d6c8cee3..68a47d42177 100644 --- a/src/main/java/org/openapijsonschematools/codegen/generators/Generator.java +++ b/src/main/java/org/openapijsonschematools/codegen/generators/Generator.java @@ -38,6 +38,7 @@ import org.openapijsonschematools.codegen.generators.models.VendorExtension; import org.openapijsonschematools.codegen.generators.openapimodels.ArrayListWithContext; import org.openapijsonschematools.codegen.generators.openapimodels.CodegenRefInfo; +import org.openapijsonschematools.codegen.generators.openapimodels.CodegenServers; import org.openapijsonschematools.codegen.templating.SupportingFile; import org.openapijsonschematools.codegen.generators.models.CliOption; import org.openapijsonschematools.codegen.generators.openapimodels.CodegenHeader; @@ -158,7 +159,7 @@ public interface Generator { CodegenPathItem fromPathItem(PathItem pathItem, String jsonPath); - ArrayListWithContext fromServers(List servers, String jsonPath); + CodegenServers fromServers(List servers, String jsonPath); CodegenSchema fromServerVariables(Map variables, String jsonPath); diff --git a/src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java b/src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java index 53a507373f1..86bf0fbbe03 100644 --- a/src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java +++ b/src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java @@ -120,8 +120,7 @@ public String toModuleFilename(String name, String jsonPath) { protected void updateServersFilepath(String[] pathPieces) { if (pathPieces.length == 2) { // #/servers - pathPieces[1] = "Servers"; - return; + pathPieces[1] = "ServerInfo"; } else if (pathPieces.length == 3) { // #/servers/0 String jsonPath = "#/servers/" + pathPieces[2]; @@ -137,26 +136,32 @@ protected void updateServersFilepath(String[] pathPieces) { public String getPascalCaseServer(String basename, String jsonPath) { if (jsonPath != null) { String[] pathPieces = jsonPath.split("/"); - if (jsonPath.equals("#/servers")) { - return "Servers"; - } else if (jsonPath.startsWith("#/servers/") && pathPieces.length == 3) { - return "Server"+pathPieces[2]; - } else if (jsonPath.startsWith("#/paths") && pathPieces.length == 4) { - // #/paths/somePath/servers - CodegenKey pathKey = getKey(ModelUtils.decodeSlashes(pathPieces[2]), "paths"); - return pathKey.pascalCase + "Servers"; - } else if (jsonPath.startsWith("#/paths") && pathPieces.length == 5) { - // #/paths/somePath/servers/0 - CodegenKey pathKey = getKey(ModelUtils.decodeSlashes(pathPieces[2]), "paths"); - return pathKey.pascalCase + "Server"+ pathPieces[4]; - } else if (jsonPath.startsWith("#/paths") && pathPieces.length == 5) { - // #/paths/somePath/get/servers + if (jsonPath.startsWith("#/servers")) { + if (pathPieces.length == 2) { + // #/servers + return "ServerInfo"; + } else { + // #/servers/0 + return "Server"+pathPieces[2]; + } + } else if (jsonPath.startsWith("#/paths") && pathPieces.length >= 4 && pathPieces[3].equals("servers")) { CodegenKey pathKey = getKey(ModelUtils.decodeSlashes(pathPieces[2]), "paths"); - return pathKey.pascalCase + StringUtils.capitalize(pathPieces[3]) + "Servers"; - } else if (jsonPath.startsWith("#/paths") && pathPieces.length == 6) { - // #/paths/somePath/get/servers/0 + if (pathPieces.length == 4) { + // #/paths/somePath/servers + return pathKey.pascalCase + "ServerInfo"; + } else { + // #/paths/somePath/servers/0 + return pathKey.pascalCase + "Server"+ pathPieces[4]; + } + } else if (jsonPath.startsWith("#/paths") && pathPieces.length >= 5 && pathPieces[4].equals("servers")) { CodegenKey pathKey = getKey(ModelUtils.decodeSlashes(pathPieces[2]), "paths"); - return pathKey.pascalCase + StringUtils.capitalize(pathPieces[3]) + "Server"+ pathPieces[5]; + if (pathPieces.length == 5) { + // #/paths/somePath/get/servers + return pathKey.pascalCase + StringUtils.capitalize(pathPieces[3]) + "ServerInfo"; + } else { + // #/paths/somePath/get/servers/0 + return pathKey.pascalCase + StringUtils.capitalize(pathPieces[3]) + "Server" + pathPieces[5]; + } } } return "Server" + basename; @@ -2355,6 +2360,15 @@ public void setOpenAPI(OpenAPI openAPI) { super.setOpenAPI(openAPI); List servers = openAPI.getServers(); if (servers != null && !servers.isEmpty()) { + supportingFiles.add(new SupportingFile( + "src/main/java/packagename/servers/Server.hbs", + packagePath() + File.separatorChar + "servers", + "Server.java")); + supportingFiles.add(new SupportingFile( + "src/main/java/packagename/servers/ServerProvider.hbs", + packagePath() + File.separatorChar + "servers", + "ServerProvider.java")); + supportingFiles.add(new SupportingFile( "src/main/java/packagename/servers/ServerWithoutVariables.hbs", packagePath() + File.separatorChar + "servers", @@ -2367,13 +2381,13 @@ public void setOpenAPI(OpenAPI openAPI) { jsonPathTemplateFiles.put( CodegenConstants.JSON_PATH_LOCATION_TYPE.SERVER, new HashMap<>() {{ - put("src/main/java/packagename/servers/Server.hbs", ".java"); + put("src/main/java/packagename/servers/ServerN.hbs", ".java"); }} ); jsonPathTemplateFiles.put( CodegenConstants.JSON_PATH_LOCATION_TYPE.SERVERS, new HashMap<>() {{ - put("src/main/java/packagename/servers/Servers.hbs", ".java"); + put("src/main/java/packagename/servers/ServerInfo.hbs", ".java"); }} ); jsonPathDocTemplateFiles.put( diff --git a/src/main/java/org/openapijsonschematools/codegen/generators/openapimodels/CodegenOperation.java b/src/main/java/org/openapijsonschematools/codegen/generators/openapimodels/CodegenOperation.java index d6b2507d76d..28c9d7b446e 100644 --- a/src/main/java/org/openapijsonschematools/codegen/generators/openapimodels/CodegenOperation.java +++ b/src/main/java/org/openapijsonschematools/codegen/generators/openapimodels/CodegenOperation.java @@ -30,7 +30,7 @@ public class CodegenOperation { public final LinkedHashSet errorWildcardStatusCodes; // values like 4 for 4XX public final CodegenText summary, description; public final LinkedHashSet produces; - public final List servers; + public final CodegenServers servers; public final CodegenRequestBody requestBody; // properties where key is contentType, value is a ref schema, encapsulates imports public final CodegenSchema requestBodySchema; @@ -65,7 +65,7 @@ public CodegenOperation( CodegenText summary, CodegenText description, LinkedHashSet produces, - List servers, + CodegenServers servers, CodegenRequestBody requestBody, ParameterCollection parameters, CodegenSchema pathParametersSchema, diff --git a/src/main/java/org/openapijsonschematools/codegen/generators/openapimodels/CodegenPathItem.java b/src/main/java/org/openapijsonschematools/codegen/generators/openapimodels/CodegenPathItem.java index f38754cb087..83f8fd9b0ff 100644 --- a/src/main/java/org/openapijsonschematools/codegen/generators/openapimodels/CodegenPathItem.java +++ b/src/main/java/org/openapijsonschematools/codegen/generators/openapimodels/CodegenPathItem.java @@ -7,10 +7,10 @@ public class CodegenPathItem { public final CodegenText summary; public final CodegenText description; public final TreeMap operations; - public final List servers; + public final CodegenServers servers; public final List parameters; - public CodegenPathItem(CodegenText summary, CodegenText description, TreeMap operations, List servers, List parameters) { + public CodegenPathItem(CodegenText summary, CodegenText description, TreeMap operations, CodegenServers servers, List parameters) { this.summary = summary; this.description = description; this.operations = operations; diff --git a/src/main/java/org/openapijsonschematools/codegen/generators/openapimodels/CodegenServers.java b/src/main/java/org/openapijsonschematools/codegen/generators/openapimodels/CodegenServers.java new file mode 100644 index 00000000000..0b16dfa7717 --- /dev/null +++ b/src/main/java/org/openapijsonschematools/codegen/generators/openapimodels/CodegenServers.java @@ -0,0 +1,30 @@ +package org.openapijsonschematools.codegen.generators.openapimodels; + +import java.util.AbstractList; +import java.util.Iterator; +import java.util.List; + +/** + * A class to store inline codegenschema definitions + */ +public class CodegenServers extends AbstractList { + public final List servers; + public final CodegenKey jsonPathPiece; + public final String subPackage; + + public CodegenServers(List servers, CodegenKey jsonPathPiece, String subPackage) { + this.servers = servers; + this.jsonPathPiece = jsonPathPiece; + this.subPackage = subPackage; + } + + @Override + public CodegenServer get(int index) { + return this.servers.get(index); + } + + @Override + public int size() { + return this.servers.size(); + } +} diff --git a/src/main/resources/java/src/main/java/packagename/servers/Server.hbs b/src/main/resources/java/src/main/java/packagename/servers/Server.hbs index 931bccba6b9..8d8405a8cd9 100644 --- a/src/main/resources/java/src/main/java/packagename/servers/Server.hbs +++ b/src/main/resources/java/src/main/java/packagename/servers/Server.hbs @@ -1,53 +1,6 @@ -{{#with server}} -package {{{packageName}}}.{{subpackage}}; +package {{{packageName}}}.servers; - {{#with variables}} -import {{{packageName}}}.configurations.JsonSchemaKeywordFlags; -import {{{packageName}}}.configurations.SchemaConfiguration; - {{#neq ../subpackage "servers"}} -import {{{packageName}}}.servers.ServerWithVariables; - {{/neq}} -import {{{packageName}}}.schemas.validation.MapUtils; -import {{{packageName}}}.{{subpackage}}.{{containerJsonPathPiece.pascalCase}}; - -import java.util.AbstractMap; - -public class {{../jsonPathPiece.pascalCase}} extends ServerWithVariables<{{containerJsonPathPiece.pascalCase}}.{{mapOutputJsonPathPiece.pascalCase}}> { - {{#if ../description}} - /* - {{../description.original}} - */ - {{/if}} - - public {{../jsonPathPiece.pascalCase}}() { - super( - "{{../url}}", - {{containerJsonPathPiece.pascalCase}}.{{jsonPathPiece.pascalCase}}.getInstance().validate( - MapUtils.makeMap( - {{#each requiredProperties}} - new AbstractMap.SimpleEntry<>("{{{@key.original}}}", {{../containerJsonPathPiece.pascalCase}}.{{jsonPathPiece.pascalCase}}.getInstance().defaultValue()){{#unless @last}},{{/unless}} - {{/each}} - ), - new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone()) - ) - ); - } - public {{../jsonPathPiece.pascalCase}}({{containerJsonPathPiece.pascalCase}}.{{mapOutputJsonPathPiece.pascalCase}} variables) { - super("{{../url}}", variables); - } +public interface Server { + String url(); } - {{else}} -import {{{packageName}}}.servers.ServerWithoutVariables; -public class {{jsonPathPiece.pascalCase}} extends ServerWithoutVariables { - {{#if ../description}} - /* - {{../description.original}} - */ - {{/if}} - public {{jsonPathPiece.pascalCase}}() { - super("{{url}}"); - } -} - {{/with}} -{{/with}} \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/servers/ServerInfo.hbs b/src/main/resources/java/src/main/java/packagename/servers/ServerInfo.hbs new file mode 100644 index 00000000000..1b69736f8b1 --- /dev/null +++ b/src/main/resources/java/src/main/java/packagename/servers/ServerInfo.hbs @@ -0,0 +1,83 @@ +{{#if servers.subpackage}} +package {{{packageName}}}.{{servers.subpackage}}; +{{else}} +package {{{packageName}}}; +{{/if}} + +{{#each servers}} +import {{{packageName}}}.{{subpackage}}.{{jsonPathPiece.pascalCase}}; +{{/each}} +import {{{packageName}}}.servers.Server; +import {{{packageName}}}.servers.ServerProvider; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.AbstractMap; +import java.util.Map; +import java.util.Objects; +import java.util.EnumMap; + +public class {{servers.jsonPathPiece.pascalCase}} implements ServerProvider<{{servers.jsonPathPiece.pascalCase}}.ServerIndex> { + final private Servers servers; + final private ServerIndex serverIndex; + + public {{servers.jsonPathPiece.pascalCase}}() { + this.servers = new Servers(); + this.serverIndex = ServerIndex.SERVER_0; + } + + public {{servers.jsonPathPiece.pascalCase}}(Servers servers, ServerIndex serverIndex) { + this.servers = servers; + this.serverIndex = serverIndex; + } + + public static class Servers { + private final EnumMap servers; + + public Servers() { + servers = new EnumMap<>( + Map.ofEntries( + {{#each servers}} + new AbstractMap.SimpleEntry<>( + ServerIndex.SERVER_{{@index}}, + new {{jsonPathPiece.pascalCase}}() + ){{#unless @last}},{{/unless}} + {{/each}} + ) + ); + } + + public Servers( + {{#each servers}} + @Nullable {{jsonPathPiece.pascalCase}} server{{@index}}{{#unless @last}},{{/unless}} + {{/each}} + ) { + servers = new EnumMap<>( + Map.ofEntries( + {{#each servers}} + new AbstractMap.SimpleEntry<>( + ServerIndex.SERVER_{{@index}}, + Objects.requireNonNullElseGet(server{{@index}}, {{jsonPathPiece.pascalCase}}::new) + ){{#unless @last}},{{/unless}} + {{/each}} + ) + ); + } + + public Server get(ServerIndex serverIndex) { + return servers.get(serverIndex); + } + } + + public enum ServerIndex { + {{#each servers}} + SERVER_{{@index}}{{#unless @last}},{{/unless}} + {{/each}} + } + + public Server get(@Nullable ServerIndex serverIndex) { + if (serverIndex == null) { + return servers.get(this.serverIndex); + } + return servers.get(serverIndex); + } +} \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/servers/ServerN.hbs b/src/main/resources/java/src/main/java/packagename/servers/ServerN.hbs new file mode 100644 index 00000000000..931bccba6b9 --- /dev/null +++ b/src/main/resources/java/src/main/java/packagename/servers/ServerN.hbs @@ -0,0 +1,53 @@ +{{#with server}} +package {{{packageName}}}.{{subpackage}}; + + {{#with variables}} +import {{{packageName}}}.configurations.JsonSchemaKeywordFlags; +import {{{packageName}}}.configurations.SchemaConfiguration; + {{#neq ../subpackage "servers"}} +import {{{packageName}}}.servers.ServerWithVariables; + {{/neq}} +import {{{packageName}}}.schemas.validation.MapUtils; +import {{{packageName}}}.{{subpackage}}.{{containerJsonPathPiece.pascalCase}}; + +import java.util.AbstractMap; + +public class {{../jsonPathPiece.pascalCase}} extends ServerWithVariables<{{containerJsonPathPiece.pascalCase}}.{{mapOutputJsonPathPiece.pascalCase}}> { + {{#if ../description}} + /* + {{../description.original}} + */ + {{/if}} + + public {{../jsonPathPiece.pascalCase}}() { + super( + "{{../url}}", + {{containerJsonPathPiece.pascalCase}}.{{jsonPathPiece.pascalCase}}.getInstance().validate( + MapUtils.makeMap( + {{#each requiredProperties}} + new AbstractMap.SimpleEntry<>("{{{@key.original}}}", {{../containerJsonPathPiece.pascalCase}}.{{jsonPathPiece.pascalCase}}.getInstance().defaultValue()){{#unless @last}},{{/unless}} + {{/each}} + ), + new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone()) + ) + ); + } + public {{../jsonPathPiece.pascalCase}}({{containerJsonPathPiece.pascalCase}}.{{mapOutputJsonPathPiece.pascalCase}} variables) { + super("{{../url}}", variables); + } +} + {{else}} +import {{{packageName}}}.servers.ServerWithoutVariables; + +public class {{jsonPathPiece.pascalCase}} extends ServerWithoutVariables { + {{#if ../description}} + /* + {{../description.original}} + */ + {{/if}} + public {{jsonPathPiece.pascalCase}}() { + super("{{url}}"); + } +} + {{/with}} +{{/with}} \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/servers/ServerProvider.hbs b/src/main/resources/java/src/main/java/packagename/servers/ServerProvider.hbs new file mode 100644 index 00000000000..6bc8cf80e08 --- /dev/null +++ b/src/main/resources/java/src/main/java/packagename/servers/ServerProvider.hbs @@ -0,0 +1,8 @@ +package {{{packageName}}}.servers; + +import org.checkerframework.checker.nullness.qual.Nullable; + +public interface ServerProvider { + Server get(@Nullable T serverIndex); +} + diff --git a/src/main/resources/java/src/main/java/packagename/servers/ServerWithVariables.hbs b/src/main/resources/java/src/main/java/packagename/servers/ServerWithVariables.hbs index 4eb078b7ca2..973e0d6649a 100644 --- a/src/main/resources/java/src/main/java/packagename/servers/ServerWithVariables.hbs +++ b/src/main/resources/java/src/main/java/packagename/servers/ServerWithVariables.hbs @@ -2,7 +2,7 @@ package {{{packageName}}}.servers; import java.util.Map; -public abstract class ServerWithVariables> { +public abstract class ServerWithVariables> implements Server { public final String url; public final T variables; @@ -13,5 +13,9 @@ public abstract class ServerWithVariables> { } this.url = url; } + + public String url(){ + return url; + } } diff --git a/src/main/resources/java/src/main/java/packagename/servers/ServerWithoutVariables.hbs b/src/main/resources/java/src/main/java/packagename/servers/ServerWithoutVariables.hbs index 06ae0d7e5f0..8adff0b1825 100644 --- a/src/main/resources/java/src/main/java/packagename/servers/ServerWithoutVariables.hbs +++ b/src/main/resources/java/src/main/java/packagename/servers/ServerWithoutVariables.hbs @@ -1,10 +1,14 @@ package {{{packageName}}}.servers; -public abstract class ServerWithoutVariables { +public abstract class ServerWithoutVariables implements Server { public final String url; protected ServerWithoutVariables(String url) { this.url = url; } + + public String url(){ + return url; + } } diff --git a/src/main/resources/java/src/main/java/packagename/servers/Servers.hbs b/src/main/resources/java/src/main/java/packagename/servers/Servers.hbs deleted file mode 100644 index f64e30e4a02..00000000000 --- a/src/main/resources/java/src/main/java/packagename/servers/Servers.hbs +++ /dev/null @@ -1,33 +0,0 @@ -{{#with servers.0}} -package {{{packageName}}}.{{subpackage}}; -{{/with}} - -{{#each servers}} -import {{{packageName}}}.{{subpackage}}.{{jsonPathPiece.pascalCase}}; -{{/each}} -import org.checkerframework.checker.nullness.qual.Nullable; - -import java.util.Objects; - -public class {{servers.jsonPathPiece.pascalCase}} { - {{#each servers}} - final private {{jsonPathPiece.pascalCase}} server{{@index}}; - {{/each}} - - public {{servers.jsonPathPiece.pascalCase}}() { - {{#each servers}} - server{{@index}} = new {{jsonPathPiece.pascalCase}}(); - {{/each}} - } - - public {{servers.jsonPathPiece.pascalCase}}( - {{#each servers}} - @Nullable {{jsonPathPiece.pascalCase}} server{{@index}}{{#unless @last}},{{/unless}} - {{/each}} - ) { - {{#each servers}} - this.server{{@index}} = Objects.requireNonNullElseGet(server{{@index}}, {{jsonPathPiece.pascalCase}}::new); - {{/each}} - } - -} \ No newline at end of file diff --git a/src/test/java/org/openapijsonschematools/codegen/generatorrunner/DefaultGeneratorRunnerTest.java b/src/test/java/org/openapijsonschematools/codegen/generatorrunner/DefaultGeneratorRunnerTest.java index bc43f5aeab4..2584da109d3 100644 --- a/src/test/java/org/openapijsonschematools/codegen/generatorrunner/DefaultGeneratorRunnerTest.java +++ b/src/test/java/org/openapijsonschematools/codegen/generatorrunner/DefaultGeneratorRunnerTest.java @@ -25,6 +25,7 @@ import org.openapijsonschematools.codegen.generators.openapimodels.CodegenSchema; import org.openapijsonschematools.codegen.generators.openapimodels.CodegenServer; import org.openapijsonschematools.codegen.common.ModelUtils; +import org.openapijsonschematools.codegen.generators.openapimodels.CodegenServers; import org.testng.Assert; import org.testng.annotations.Test; @@ -608,11 +609,11 @@ public void testHandlesTrailingSlashInServers() { generator.opts(opts); generator.configureGeneratorProperties(); - List servers = config.fromServers(openAPI.getServers(), "#/servers"); + CodegenServers servers = config.fromServers(openAPI.getServers(), "#/servers"); Map bundle = generator.buildSupportFileBundle( null, null, null, null, null, null, servers, null, null); - LinkedList bundleServers = (LinkedList) bundle.get("servers"); + CodegenServers bundleServers = (CodegenServers) bundle.get("servers"); Assert.assertEquals(bundleServers.get(0).url, ""); Assert.assertEquals(bundleServers.get(1).url, "http://trailingshlash.io:80/v1"); Assert.assertEquals(bundleServers.get(2).url, "http://notrailingslash.io:80/v2"); @@ -634,11 +635,11 @@ public void testHandlesRelativeUrlsInServers() { List files = new ArrayList<>(); - List servers = config.fromServers(openAPI.getServers(), "#/servers"); + CodegenServers servers = config.fromServers(openAPI.getServers(), "#/servers"); Map bundle = generator.buildSupportFileBundle( null, null, null, null, null, null, servers, null, null); - LinkedList bundleServers = (LinkedList) bundle.get("servers"); + CodegenServers bundleServers = (CodegenServers) bundle.get("servers"); Assert.assertEquals(bundleServers.get(0).url, "/relative/url"); } From fdc024c142953524612b04ad9195843d39a292ed Mon Sep 17 00:00:00 2001 From: Justin Black Date: Wed, 24 Jan 2024 16:57:42 -0800 Subject: [PATCH 6/6] Samples regen --- .../java/.openapi-generator/FILES | 4 + .../client/RootServerInfo.java | 73 +++++++++++++++++++ .../configurations/ApiConfiguration.java | 37 ++++++++++ .../client/servers/Server.java | 6 ++ .../client/servers/Server0.java | 2 +- .../client/servers/ServerProvider.java | 8 ++ .../client/servers/ServerWithVariables.java | 6 +- .../servers/ServerWithoutVariables.java | 6 +- .../java/.openapi-generator/FILES | 4 + .../client/RootServerInfo.java | 73 +++++++++++++++++++ .../configurations/ApiConfiguration.java | 37 ++++++++++ .../client/servers/Server.java | 6 ++ .../client/servers/Server0.java | 2 +- .../client/servers/ServerProvider.java | 8 ++ .../client/servers/ServerWithVariables.java | 6 +- .../servers/ServerWithoutVariables.java | 6 +- .../petstore/java/.openapi-generator/FILES | 2 +- .../{ServerInfo.java => RootServerInfo.java} | 14 ++-- .../configurations/ApiConfiguration.java | 59 ++++++++++++--- .../paths/foo/get/FooGetServerInfo.java | 10 ++- .../PetfindbystatusServerInfo.java | 10 ++- .../client/servers/ServerProvider.java | 2 +- .../petstore/python/.openapi-generator/FILES | 4 +- .../paths/foo/get/server_s/__init__.py | 0 .../pet_find_by_status/server_s/__init__.py | 0 .../DefaultGeneratorRunner.java | 18 +++-- .../codegen/generators/DefaultGenerator.java | 17 ++++- .../generators/JavaClientGenerator.java | 4 +- .../openapimodels/CodegenServers.java | 6 +- .../configurations/ApiConfiguration.hbs | 67 ++++++++++++----- .../java/packagename/servers/ServerInfo.hbs | 8 +- .../packagename/servers/ServerProvider.hbs | 2 +- 32 files changed, 439 insertions(+), 68 deletions(-) create mode 100644 samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/RootServerInfo.java create mode 100644 samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/configurations/ApiConfiguration.java create mode 100644 samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/Server.java create mode 100644 samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerProvider.java create mode 100644 samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/RootServerInfo.java create mode 100644 samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/configurations/ApiConfiguration.java create mode 100644 samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/Server.java create mode 100644 samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerProvider.java rename samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/{ServerInfo.java => RootServerInfo.java} (83%) create mode 100644 samples/client/petstore/python/src/petstore_api/paths/foo/get/server_s/__init__.py create mode 100644 samples/client/petstore/python/src/petstore_api/paths/pet_find_by_status/server_s/__init__.py diff --git a/samples/client/3_0_3_unit_test/java/.openapi-generator/FILES b/samples/client/3_0_3_unit_test/java/.openapi-generator/FILES index a593d7f1f54..d5c8ee62cc5 100644 --- a/samples/client/3_0_3_unit_test/java/.openapi-generator/FILES +++ b/samples/client/3_0_3_unit_test/java/.openapi-generator/FILES @@ -88,6 +88,7 @@ docs/components/schemas/UriReferenceFormat.md docs/components/schemas/UriTemplateFormat.md docs/servers/Server0.md pom.xml +src/main/java/org/openapijsonschematools/client/RootServerInfo.java src/main/java/org/openapijsonschematools/client/components/schemas/AdditionalpropertiesAllowsASchemaWhichShouldValidate.java src/main/java/org/openapijsonschematools/client/components/schemas/AdditionalpropertiesAreAllowedByDefault.java src/main/java/org/openapijsonschematools/client/components/schemas/AdditionalpropertiesCanExistByItself.java @@ -175,6 +176,7 @@ src/main/java/org/openapijsonschematools/client/components/schemas/UniqueitemsVa src/main/java/org/openapijsonschematools/client/components/schemas/UriFormat.java src/main/java/org/openapijsonschematools/client/components/schemas/UriReferenceFormat.java src/main/java/org/openapijsonschematools/client/components/schemas/UriTemplateFormat.java +src/main/java/org/openapijsonschematools/client/configurations/ApiConfiguration.java src/main/java/org/openapijsonschematools/client/configurations/JsonSchemaKeywordFlags.java src/main/java/org/openapijsonschematools/client/configurations/SchemaConfiguration.java src/main/java/org/openapijsonschematools/client/exceptions/BaseException.java @@ -277,7 +279,9 @@ src/main/java/org/openapijsonschematools/client/schemas/validation/UniqueItemsVa src/main/java/org/openapijsonschematools/client/schemas/validation/UnsetAnyTypeJsonSchema.java src/main/java/org/openapijsonschematools/client/schemas/validation/ValidationData.java src/main/java/org/openapijsonschematools/client/schemas/validation/ValidationMetadata.java +src/main/java/org/openapijsonschematools/client/servers/Server.java src/main/java/org/openapijsonschematools/client/servers/Server0.java +src/main/java/org/openapijsonschematools/client/servers/ServerProvider.java src/main/java/org/openapijsonschematools/client/servers/ServerWithVariables.java src/main/java/org/openapijsonschematools/client/servers/ServerWithoutVariables.java src/test/java/org/openapijsonschematools/client/configurations/JsonSchemaKeywordFlagsTest.java diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/RootServerInfo.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/RootServerInfo.java new file mode 100644 index 00000000000..d5c2f5cb5fd --- /dev/null +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/RootServerInfo.java @@ -0,0 +1,73 @@ +package org.openapijsonschematools.client; + +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; +import org.openapijsonschematools.client.servers.Server0; +import org.openapijsonschematools.client.servers.Server; +import org.openapijsonschematools.client.servers.ServerProvider; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.AbstractMap; +import java.util.Map; +import java.util.Objects; +import java.util.EnumMap; + +public class RootServerInfo implements ServerProvider { + final private Servers servers; + final private ServerIndex serverIndex; + + public RootServerInfo() { + this.servers = new Servers(); + this.serverIndex = ServerIndex.SERVER_0; + } + + public RootServerInfo(Servers servers, ServerIndex serverIndex) { + this.servers = servers; + this.serverIndex = serverIndex; + } + + public static class Servers { + private final EnumMap servers; + + public Servers() { + servers = new EnumMap<>( + Map.ofEntries( + new AbstractMap.SimpleEntry<>( + ServerIndex.SERVER_0, + new Server0() + ) + ) + ); + } + + public Servers( + @Nullable Server0 server0 + ) { + servers = new EnumMap<>( + Map.ofEntries( + new AbstractMap.SimpleEntry<>( + ServerIndex.SERVER_0, + Objects.requireNonNullElseGet(server0, Server0::new) + ) + ) + ); + } + + public Server get(ServerIndex serverIndex) { + if (servers.containsKey(serverIndex)) { + return get(serverIndex); + } + throw new UnsetPropertyException(serverIndex+" is unset"); + } + } + + public enum ServerIndex { + SERVER_0 + } + + public Server getServer(@Nullable ServerIndex serverIndex) { + if (serverIndex == null) { + return servers.get(this.serverIndex); + } + return servers.get(serverIndex); + } +} \ No newline at end of file diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/configurations/ApiConfiguration.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/configurations/ApiConfiguration.java new file mode 100644 index 00000000000..a580e5a1f9b --- /dev/null +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/configurations/ApiConfiguration.java @@ -0,0 +1,37 @@ +package org.openapijsonschematools.client.configurations; + +import org.openapijsonschematools.client.servers.Server; +import org.openapijsonschematools.client.RootServerInfo; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.Objects; + +public class ApiConfiguration { + private final ServerInfo serverInfo; + + public ApiConfiguration() { + serverInfo = new ServerInfo(); + } + + public ApiConfiguration(ServerInfo serverInfo) { + this.serverInfo = serverInfo; + } + + public static class ServerInfo { + protected final RootServerInfo rootServerInfo; + + public ServerInfo() { + rootServerInfo = new RootServerInfo(); + } + + public ServerInfo( + @Nullable RootServerInfo rootServerInfo + ) { + this.rootServerInfo = Objects.requireNonNullElseGet(rootServerInfo, RootServerInfo::new); + } + } + + public Server getServer(RootServerInfo. @Nullable ServerIndex serverIndex) { + return serverInfo.rootServerInfo.getServer(serverIndex); + } +} \ No newline at end of file diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/Server.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/Server.java new file mode 100644 index 00000000000..f5159644ccc --- /dev/null +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/Server.java @@ -0,0 +1,6 @@ +package org.openapijsonschematools.client.servers; + +public interface Server { + String url(); +} + diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/Server0.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/Server0.java index 9e5175e6180..f74d6d47db4 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/Server0.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/Server0.java @@ -2,7 +2,7 @@ import org.openapijsonschematools.client.servers.ServerWithoutVariables; -class Server0 extends ServerWithoutVariables { +public class Server0 extends ServerWithoutVariables { public Server0() { super("https://someserver.com/v1"); } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerProvider.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerProvider.java new file mode 100644 index 00000000000..8c93a971aae --- /dev/null +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerProvider.java @@ -0,0 +1,8 @@ +package org.openapijsonschematools.client.servers; + +import org.checkerframework.checker.nullness.qual.Nullable; + +public interface ServerProvider { + Server getServer(@Nullable T serverIndex); +} + diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithVariables.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithVariables.java index acdb6d730ed..68748bcfcae 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithVariables.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithVariables.java @@ -2,7 +2,7 @@ import java.util.Map; -public abstract class ServerWithVariables> { +public abstract class ServerWithVariables> implements Server { public final String url; public final T variables; @@ -13,5 +13,9 @@ protected ServerWithVariables(String url, T variables) { } this.url = url; } + + public String url(){ + return url; + } } diff --git a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithoutVariables.java b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithoutVariables.java index 903ac3f4c7d..c41ac2e77e5 100644 --- a/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithoutVariables.java +++ b/samples/client/3_0_3_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithoutVariables.java @@ -1,10 +1,14 @@ package org.openapijsonschematools.client.servers; -public abstract class ServerWithoutVariables { +public abstract class ServerWithoutVariables implements Server { public final String url; protected ServerWithoutVariables(String url) { this.url = url; } + + public String url(){ + return url; + } } diff --git a/samples/client/3_1_0_unit_test/java/.openapi-generator/FILES b/samples/client/3_1_0_unit_test/java/.openapi-generator/FILES index 70bd77d7041..a7cc708c990 100644 --- a/samples/client/3_1_0_unit_test/java/.openapi-generator/FILES +++ b/samples/client/3_1_0_unit_test/java/.openapi-generator/FILES @@ -144,6 +144,7 @@ docs/components/schemas/UuidFormat.md docs/components/schemas/ValidateAgainstCorrectBranchThenVsElse.md docs/servers/Server0.md pom.xml +src/main/java/org/openapijsonschematools/client/RootServerInfo.java src/main/java/org/openapijsonschematools/client/components/schemas/ASchemaGivenForPrefixitems.java src/main/java/org/openapijsonschematools/client/components/schemas/AdditionalItemsAreAllowedByDefault.java src/main/java/org/openapijsonschematools/client/components/schemas/AdditionalpropertiesAreAllowedByDefault.java @@ -287,6 +288,7 @@ src/main/java/org/openapijsonschematools/client/components/schemas/UriReferenceF src/main/java/org/openapijsonschematools/client/components/schemas/UriTemplateFormat.java src/main/java/org/openapijsonschematools/client/components/schemas/UuidFormat.java src/main/java/org/openapijsonschematools/client/components/schemas/ValidateAgainstCorrectBranchThenVsElse.java +src/main/java/org/openapijsonschematools/client/configurations/ApiConfiguration.java src/main/java/org/openapijsonschematools/client/configurations/JsonSchemaKeywordFlags.java src/main/java/org/openapijsonschematools/client/configurations/SchemaConfiguration.java src/main/java/org/openapijsonschematools/client/exceptions/BaseException.java @@ -389,7 +391,9 @@ src/main/java/org/openapijsonschematools/client/schemas/validation/UniqueItemsVa src/main/java/org/openapijsonschematools/client/schemas/validation/UnsetAnyTypeJsonSchema.java src/main/java/org/openapijsonschematools/client/schemas/validation/ValidationData.java src/main/java/org/openapijsonschematools/client/schemas/validation/ValidationMetadata.java +src/main/java/org/openapijsonschematools/client/servers/Server.java src/main/java/org/openapijsonschematools/client/servers/Server0.java +src/main/java/org/openapijsonschematools/client/servers/ServerProvider.java src/main/java/org/openapijsonschematools/client/servers/ServerWithVariables.java src/main/java/org/openapijsonschematools/client/servers/ServerWithoutVariables.java src/test/java/org/openapijsonschematools/client/configurations/JsonSchemaKeywordFlagsTest.java diff --git a/samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/RootServerInfo.java b/samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/RootServerInfo.java new file mode 100644 index 00000000000..d5c2f5cb5fd --- /dev/null +++ b/samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/RootServerInfo.java @@ -0,0 +1,73 @@ +package org.openapijsonschematools.client; + +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; +import org.openapijsonschematools.client.servers.Server0; +import org.openapijsonschematools.client.servers.Server; +import org.openapijsonschematools.client.servers.ServerProvider; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.AbstractMap; +import java.util.Map; +import java.util.Objects; +import java.util.EnumMap; + +public class RootServerInfo implements ServerProvider { + final private Servers servers; + final private ServerIndex serverIndex; + + public RootServerInfo() { + this.servers = new Servers(); + this.serverIndex = ServerIndex.SERVER_0; + } + + public RootServerInfo(Servers servers, ServerIndex serverIndex) { + this.servers = servers; + this.serverIndex = serverIndex; + } + + public static class Servers { + private final EnumMap servers; + + public Servers() { + servers = new EnumMap<>( + Map.ofEntries( + new AbstractMap.SimpleEntry<>( + ServerIndex.SERVER_0, + new Server0() + ) + ) + ); + } + + public Servers( + @Nullable Server0 server0 + ) { + servers = new EnumMap<>( + Map.ofEntries( + new AbstractMap.SimpleEntry<>( + ServerIndex.SERVER_0, + Objects.requireNonNullElseGet(server0, Server0::new) + ) + ) + ); + } + + public Server get(ServerIndex serverIndex) { + if (servers.containsKey(serverIndex)) { + return get(serverIndex); + } + throw new UnsetPropertyException(serverIndex+" is unset"); + } + } + + public enum ServerIndex { + SERVER_0 + } + + public Server getServer(@Nullable ServerIndex serverIndex) { + if (serverIndex == null) { + return servers.get(this.serverIndex); + } + return servers.get(serverIndex); + } +} \ No newline at end of file diff --git a/samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/configurations/ApiConfiguration.java b/samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/configurations/ApiConfiguration.java new file mode 100644 index 00000000000..a580e5a1f9b --- /dev/null +++ b/samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/configurations/ApiConfiguration.java @@ -0,0 +1,37 @@ +package org.openapijsonschematools.client.configurations; + +import org.openapijsonschematools.client.servers.Server; +import org.openapijsonschematools.client.RootServerInfo; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.Objects; + +public class ApiConfiguration { + private final ServerInfo serverInfo; + + public ApiConfiguration() { + serverInfo = new ServerInfo(); + } + + public ApiConfiguration(ServerInfo serverInfo) { + this.serverInfo = serverInfo; + } + + public static class ServerInfo { + protected final RootServerInfo rootServerInfo; + + public ServerInfo() { + rootServerInfo = new RootServerInfo(); + } + + public ServerInfo( + @Nullable RootServerInfo rootServerInfo + ) { + this.rootServerInfo = Objects.requireNonNullElseGet(rootServerInfo, RootServerInfo::new); + } + } + + public Server getServer(RootServerInfo. @Nullable ServerIndex serverIndex) { + return serverInfo.rootServerInfo.getServer(serverIndex); + } +} \ No newline at end of file diff --git a/samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/Server.java b/samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/Server.java new file mode 100644 index 00000000000..f5159644ccc --- /dev/null +++ b/samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/Server.java @@ -0,0 +1,6 @@ +package org.openapijsonschematools.client.servers; + +public interface Server { + String url(); +} + diff --git a/samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/Server0.java b/samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/Server0.java index 9e5175e6180..f74d6d47db4 100644 --- a/samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/Server0.java +++ b/samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/Server0.java @@ -2,7 +2,7 @@ import org.openapijsonschematools.client.servers.ServerWithoutVariables; -class Server0 extends ServerWithoutVariables { +public class Server0 extends ServerWithoutVariables { public Server0() { super("https://someserver.com/v1"); } diff --git a/samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerProvider.java b/samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerProvider.java new file mode 100644 index 00000000000..8c93a971aae --- /dev/null +++ b/samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerProvider.java @@ -0,0 +1,8 @@ +package org.openapijsonschematools.client.servers; + +import org.checkerframework.checker.nullness.qual.Nullable; + +public interface ServerProvider { + Server getServer(@Nullable T serverIndex); +} + diff --git a/samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithVariables.java b/samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithVariables.java index acdb6d730ed..68748bcfcae 100644 --- a/samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithVariables.java +++ b/samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithVariables.java @@ -2,7 +2,7 @@ import java.util.Map; -public abstract class ServerWithVariables> { +public abstract class ServerWithVariables> implements Server { public final String url; public final T variables; @@ -13,5 +13,9 @@ protected ServerWithVariables(String url, T variables) { } this.url = url; } + + public String url(){ + return url; + } } diff --git a/samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithoutVariables.java b/samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithoutVariables.java index 903ac3f4c7d..c41ac2e77e5 100644 --- a/samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithoutVariables.java +++ b/samples/client/3_1_0_unit_test/java/src/main/java/org/openapijsonschematools/client/servers/ServerWithoutVariables.java @@ -1,10 +1,14 @@ package org.openapijsonschematools.client.servers; -public abstract class ServerWithoutVariables { +public abstract class ServerWithoutVariables implements Server { public final String url; protected ServerWithoutVariables(String url) { this.url = url; } + + public String url(){ + return url; + } } diff --git a/samples/client/petstore/java/.openapi-generator/FILES b/samples/client/petstore/java/.openapi-generator/FILES index b99f93a452c..47f55d4319b 100644 --- a/samples/client/petstore/java/.openapi-generator/FILES +++ b/samples/client/petstore/java/.openapi-generator/FILES @@ -305,7 +305,7 @@ docs/servers/Server0.md docs/servers/Server1.md docs/servers/Server2.md pom.xml -src/main/java/org/openapijsonschematools/client/ServerInfo.java +src/main/java/org/openapijsonschematools/client/RootServerInfo.java src/main/java/org/openapijsonschematools/client/components/headers/int32jsoncontenttypeheader/content/applicationjson/Int32JsonContentTypeHeaderSchema.java src/main/java/org/openapijsonschematools/client/components/headers/numberheader/NumberHeaderSchema.java src/main/java/org/openapijsonschematools/client/components/headers/refcontentschemaheader/content/applicationjson/RefContentSchemaHeaderSchema.java diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/ServerInfo.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/RootServerInfo.java similarity index 83% rename from samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/ServerInfo.java rename to samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/RootServerInfo.java index fa811efde6b..b94c5bb4b28 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/ServerInfo.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/RootServerInfo.java @@ -1,5 +1,6 @@ package org.openapijsonschematools.client; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.servers.Server0; import org.openapijsonschematools.client.servers.Server1; import org.openapijsonschematools.client.servers.Server2; @@ -12,16 +13,16 @@ import java.util.Objects; import java.util.EnumMap; -public class ServerInfo implements ServerProvider { +public class RootServerInfo implements ServerProvider { final private Servers servers; final private ServerIndex serverIndex; - public ServerInfo() { + public RootServerInfo() { this.servers = new Servers(); this.serverIndex = ServerIndex.SERVER_0; } - public ServerInfo(Servers servers, ServerIndex serverIndex) { + public RootServerInfo(Servers servers, ServerIndex serverIndex) { this.servers = servers; this.serverIndex = serverIndex; } @@ -72,7 +73,10 @@ public Servers( } public Server get(ServerIndex serverIndex) { - return servers.get(serverIndex); + if (servers.containsKey(serverIndex)) { + return get(serverIndex); + } + throw new UnsetPropertyException(serverIndex+" is unset"); } } @@ -82,7 +86,7 @@ public enum ServerIndex { SERVER_2 } - public Server get(@Nullable ServerIndex serverIndex) { + public Server getServer(@Nullable ServerIndex serverIndex) { if (serverIndex == null) { return servers.get(this.serverIndex); } diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/configurations/ApiConfiguration.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/configurations/ApiConfiguration.java index d1d7dbdda67..d600ad69de9 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/configurations/ApiConfiguration.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/configurations/ApiConfiguration.java @@ -1,12 +1,53 @@ package org.openapijsonschematools.client.configurations; -import org.openapijsonschematools.client.servers.Server0; -import org.openapijsonschematools.client.servers.Server1; -import org.openapijsonschematools.client.servers.Server2; -import org.openapijsonschematools.client.paths.foo.get.servers.FooGetServer0; -import org.openapijsonschematools.client.paths.foo.get.servers.FooGetServer1; -import org.openapijsonschematools.client.paths.petfindbystatus.servers.PetfindbystatusServer0; -import org.openapijsonschematools.client.paths.petfindbystatus.servers.PetfindbystatusServer1; - -public record ApiConfiguration() { +import org.openapijsonschematools.client.servers.Server; +import org.openapijsonschematools.client.RootServerInfo; +import org.openapijsonschematools.client.paths.foo.get.FooGetServerInfo; +import org.openapijsonschematools.client.paths.petfindbystatus.PetfindbystatusServerInfo; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.Objects; + +public class ApiConfiguration { + private final ServerInfo serverInfo; + + public ApiConfiguration() { + serverInfo = new ServerInfo(); + } + + public ApiConfiguration(ServerInfo serverInfo) { + this.serverInfo = serverInfo; + } + + public static class ServerInfo { + protected final RootServerInfo rootServerInfo; + protected final FooGetServerInfo fooGetServerInfo; + protected final PetfindbystatusServerInfo petfindbystatusServerInfo; + + public ServerInfo() { + rootServerInfo = new RootServerInfo(); + fooGetServerInfo = new FooGetServerInfo(); + petfindbystatusServerInfo = new PetfindbystatusServerInfo(); + } + + public ServerInfo( + @Nullable RootServerInfo rootServerInfo, + @Nullable FooGetServerInfo fooGetServerInfo, + @Nullable PetfindbystatusServerInfo petfindbystatusServerInfo + ) { + this.rootServerInfo = Objects.requireNonNullElseGet(rootServerInfo, RootServerInfo::new); + this.fooGetServerInfo = Objects.requireNonNullElseGet(fooGetServerInfo, FooGetServerInfo::new); + this.petfindbystatusServerInfo = Objects.requireNonNullElseGet(petfindbystatusServerInfo, PetfindbystatusServerInfo::new); + } + } + + public Server getServer(RootServerInfo. @Nullable ServerIndex serverIndex) { + return serverInfo.rootServerInfo.getServer(serverIndex); + } + public Server getServer(FooGetServerInfo. @Nullable ServerIndex serverIndex) { + return serverInfo.fooGetServerInfo.getServer(serverIndex); + } + public Server getServer(PetfindbystatusServerInfo. @Nullable ServerIndex serverIndex) { + return serverInfo.petfindbystatusServerInfo.getServer(serverIndex); + } } \ No newline at end of file diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/FooGetServerInfo.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/FooGetServerInfo.java index cd44a6232da..45d598bb653 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/FooGetServerInfo.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/foo/get/FooGetServerInfo.java @@ -1,5 +1,6 @@ -package org.openapijsonschematools.client; +package org.openapijsonschematools.client.paths.foo.get; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.paths.foo.get.servers.FooGetServer0; import org.openapijsonschematools.client.paths.foo.get.servers.FooGetServer1; import org.openapijsonschematools.client.servers.Server; @@ -62,7 +63,10 @@ public Servers( } public Server get(ServerIndex serverIndex) { - return servers.get(serverIndex); + if (servers.containsKey(serverIndex)) { + return get(serverIndex); + } + throw new UnsetPropertyException(serverIndex+" is unset"); } } @@ -71,7 +75,7 @@ public enum ServerIndex { SERVER_1 } - public Server get(@Nullable ServerIndex serverIndex) { + public Server getServer(@Nullable ServerIndex serverIndex) { if (serverIndex == null) { return servers.get(this.serverIndex); } diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/PetfindbystatusServerInfo.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/PetfindbystatusServerInfo.java index 16332392b52..6e499330caf 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/PetfindbystatusServerInfo.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/paths/petfindbystatus/PetfindbystatusServerInfo.java @@ -1,5 +1,6 @@ -package org.openapijsonschematools.client; +package org.openapijsonschematools.client.paths.petfindbystatus; +import org.openapijsonschematools.client.exceptions.UnsetPropertyException; import org.openapijsonschematools.client.paths.petfindbystatus.servers.PetfindbystatusServer0; import org.openapijsonschematools.client.paths.petfindbystatus.servers.PetfindbystatusServer1; import org.openapijsonschematools.client.servers.Server; @@ -62,7 +63,10 @@ public Servers( } public Server get(ServerIndex serverIndex) { - return servers.get(serverIndex); + if (servers.containsKey(serverIndex)) { + return get(serverIndex); + } + throw new UnsetPropertyException(serverIndex+" is unset"); } } @@ -71,7 +75,7 @@ public enum ServerIndex { SERVER_1 } - public Server get(@Nullable ServerIndex serverIndex) { + public Server getServer(@Nullable ServerIndex serverIndex) { if (serverIndex == null) { return servers.get(this.serverIndex); } diff --git a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/ServerProvider.java b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/ServerProvider.java index 1550bc12f31..8c93a971aae 100644 --- a/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/ServerProvider.java +++ b/samples/client/petstore/java/src/main/java/org/openapijsonschematools/client/servers/ServerProvider.java @@ -3,6 +3,6 @@ import org.checkerframework.checker.nullness.qual.Nullable; public interface ServerProvider { - Server get(@Nullable T serverIndex); + Server getServer(@Nullable T serverIndex); } diff --git a/samples/client/petstore/python/.openapi-generator/FILES b/samples/client/petstore/python/.openapi-generator/FILES index 922904e5692..00870b8b466 100644 --- a/samples/client/petstore/python/.openapi-generator/FILES +++ b/samples/client/petstore/python/.openapi-generator/FILES @@ -1328,7 +1328,7 @@ src/petstore_api/paths/foo/get/responses/response_default/__init__.py src/petstore_api/paths/foo/get/responses/response_default/content/__init__.py src/petstore_api/paths/foo/get/responses/response_default/content/application_json/__init__.py src/petstore_api/paths/foo/get/responses/response_default/content/application_json/schema.py -src/petstore_api/paths/foo/get/servers/__init__.py +src/petstore_api/paths/foo/get/server_s/__init__.py src/petstore_api/paths/foo/get/servers/server_0.py src/petstore_api/paths/foo/get/servers/server_1.py src/petstore_api/paths/pet/__init__.py @@ -1366,7 +1366,7 @@ src/petstore_api/paths/pet_find_by_status/get/security/__init__.py src/petstore_api/paths/pet_find_by_status/get/security/security_requirement_object_0.py src/petstore_api/paths/pet_find_by_status/get/security/security_requirement_object_1.py src/petstore_api/paths/pet_find_by_status/get/security/security_requirement_object_2.py -src/petstore_api/paths/pet_find_by_status/servers/__init__.py +src/petstore_api/paths/pet_find_by_status/server_s/__init__.py src/petstore_api/paths/pet_find_by_status/servers/server_0.py src/petstore_api/paths/pet_find_by_status/servers/server_1.py src/petstore_api/paths/pet_find_by_tags/__init__.py diff --git a/samples/client/petstore/python/src/petstore_api/paths/foo/get/server_s/__init__.py b/samples/client/petstore/python/src/petstore_api/paths/foo/get/server_s/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/samples/client/petstore/python/src/petstore_api/paths/pet_find_by_status/server_s/__init__.py b/samples/client/petstore/python/src/petstore_api/paths/pet_find_by_status/server_s/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/src/main/java/org/openapijsonschematools/codegen/generatorrunner/DefaultGeneratorRunner.java b/src/main/java/org/openapijsonschematools/codegen/generatorrunner/DefaultGeneratorRunner.java index 0c37e648662..0aa2aadc00a 100644 --- a/src/main/java/org/openapijsonschematools/codegen/generatorrunner/DefaultGeneratorRunner.java +++ b/src/main/java/org/openapijsonschematools/codegen/generatorrunner/DefaultGeneratorRunner.java @@ -51,6 +51,7 @@ import org.openapijsonschematools.codegen.generators.openapimodels.CodegenSecurityRequirementValue; import org.openapijsonschematools.codegen.generators.openapimodels.CodegenSecurityScheme; import org.openapijsonschematools.codegen.generators.openapimodels.CodegenServer; +import org.openapijsonschematools.codegen.generators.openapimodels.CodegenServers; import org.openapijsonschematools.codegen.generators.openapimodels.CodegenTag; import org.openapijsonschematools.codegen.generators.openapimodels.CodegenText; import org.openapijsonschematools.codegen.templating.DryRunTemplateManager; @@ -1316,7 +1317,7 @@ Map buildSupportFileBundle( TreeMap headers, TreeMap parameters, TreeMap securitySchemes, - List servers, + CodegenServers servers, TreeMap paths, List> security) { @@ -1324,30 +1325,31 @@ Map buildSupportFileBundle( bundle.put("apiPackage", generator.apiPackage()); URL url = URLPathUtils.getServerURL(openAPI, null); + List allServers = new ArrayList<>(); boolean hasServers = false; if (servers != null) { + allServers.add(servers); hasServers = true; - } else if (paths != null) { + } + if (paths != null) { for (CodegenPathItem pathItem: paths.values()) { if (pathItem.servers != null) { + allServers.add(pathItem.servers); hasServers = true; - break; } if (pathItem.operations != null) { for (CodegenOperation operation: pathItem.operations.values()) { if (operation.servers != null) { + allServers.add(operation.servers); hasServers = true; - break; } } - if (hasServers) { - break; - } } } } bundle.put("openAPI", openAPI); + bundle.put("allServers", allServers); bundle.put("scheme", URLPathUtils.getScheme(url, generator)); bundle.put("contextPath", contextPath); bundle.put("requestBodies", requestBodies); @@ -1519,7 +1521,7 @@ public List generate() { // servers String serversJsonPath = "#/servers"; - List servers = generator.fromServers(openAPI.getServers(), serversJsonPath); + CodegenServers servers = generator.fromServers(openAPI.getServers(), serversJsonPath); // paths TreeMap paths = generator.fromPaths(openAPI.getPaths()); generatePaths(files, paths, servers, security); diff --git a/src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java b/src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java index b384a7ba02c..b721f41682f 100644 --- a/src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java +++ b/src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java @@ -3819,6 +3819,10 @@ private void updatePathsFilepath(String[] pathPieces) { } return; } else if (pathPieces[3].equals("parameters")) { + if (pathPieces.length == 4) { + // #/paths/somePath/parameters + return; + } // #/paths/somePath/parameters/0 pathPieces[4] = toParameterFilename(pathPieces[4], null); if (pathPieces.length >= 7 && pathPieces[5].equals("content")) { @@ -3858,10 +3862,14 @@ private void updatePathsFilepath(String[] pathPieces) { pathPieces[5] = "server" + pathPieces[5]; pathPieces[6] = "Variables"; } - } else if (pathPieces[4].equals("security")) { + } else if (pathPieces[4].equals("security") && pathPieces.length > 5) { // #/paths/somePath/get/security/0 pathPieces[5] = toSecurityRequirementObjectFilename(pathPieces[5], null); } else if (pathPieces[4].equals("responses")) { + if (pathPieces.length < 6) { + // #/paths/user_login/get/responses -> length 5 + return; + } // #/paths/user_login/get/responses/200 -> 200 -> response_200 -> length 6 String responseJsonPath = "#/paths/" + path + "/" + pathPieces[3] + "/responses/" + pathPieces[5]; pathPieces[5] = toResponseModuleName(pathPieces[5], responseJsonPath); @@ -3898,6 +3906,9 @@ private void updatePathsFilepath(String[] pathPieces) { } } } else if (pathPieces[4].equals("parameters")) { + if (pathPieces.length == 5) { + return; + } // #/paths/somePath/get/parameters/0 -> length 6 pathPieces[5] = toParameterFilename(pathPieces[5], null); @@ -4855,6 +4866,7 @@ public CodegenKey getKey(String key, String keyType, String sourceJsonPath) { isValid = isValid(usedKey); snakeCaseName = toServerFilename(usedKey, sourceJsonPath); pascalCaseName = getPascalCaseServer(usedKey, sourceJsonPath); + camelCaseName = camelize(pascalCaseName, true); break; } if (pascalCaseName != null) { @@ -5084,10 +5096,11 @@ public CodegenServers fromServers(List servers, String jsonPath) { codegenServers.add(cs); } CodegenKey jsonPathPiece = getKey("s", "servers", jsonPath); + String serversSubpackage = getSubpackage(jsonPath); return new CodegenServers( codegenServers, jsonPathPiece, - getSubpackage(jsonPath) + serversSubpackage ); } diff --git a/src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java b/src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java index 86bf0fbbe03..bf18ca47ca8 100644 --- a/src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java +++ b/src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java @@ -120,7 +120,7 @@ public String toModuleFilename(String name, String jsonPath) { protected void updateServersFilepath(String[] pathPieces) { if (pathPieces.length == 2) { // #/servers - pathPieces[1] = "ServerInfo"; + pathPieces[1] = "RootServerInfo"; } else if (pathPieces.length == 3) { // #/servers/0 String jsonPath = "#/servers/" + pathPieces[2]; @@ -139,7 +139,7 @@ public String getPascalCaseServer(String basename, String jsonPath) { if (jsonPath.startsWith("#/servers")) { if (pathPieces.length == 2) { // #/servers - return "ServerInfo"; + return "RootServerInfo"; } else { // #/servers/0 return "Server"+pathPieces[2]; diff --git a/src/main/java/org/openapijsonschematools/codegen/generators/openapimodels/CodegenServers.java b/src/main/java/org/openapijsonschematools/codegen/generators/openapimodels/CodegenServers.java index 0b16dfa7717..b510d25c00f 100644 --- a/src/main/java/org/openapijsonschematools/codegen/generators/openapimodels/CodegenServers.java +++ b/src/main/java/org/openapijsonschematools/codegen/generators/openapimodels/CodegenServers.java @@ -10,12 +10,12 @@ public class CodegenServers extends AbstractList { public final List servers; public final CodegenKey jsonPathPiece; - public final String subPackage; + public final String subpackage; - public CodegenServers(List servers, CodegenKey jsonPathPiece, String subPackage) { + public CodegenServers(List servers, CodegenKey jsonPathPiece, String subpackage) { this.servers = servers; this.jsonPathPiece = jsonPathPiece; - this.subPackage = subPackage; + this.subpackage = subpackage; } @Override diff --git a/src/main/resources/java/src/main/java/packagename/configurations/ApiConfiguration.hbs b/src/main/resources/java/src/main/java/packagename/configurations/ApiConfiguration.hbs index 2b503dac9e2..a832f9b24f7 100644 --- a/src/main/resources/java/src/main/java/packagename/configurations/ApiConfiguration.hbs +++ b/src/main/resources/java/src/main/java/packagename/configurations/ApiConfiguration.hbs @@ -1,26 +1,53 @@ package {{{packageName}}}.configurations; -{{#if hasServers}} - {{#each servers}} -import {{packageName}}.servers.{{jsonPathPiece.pascalCase}}; +import {{{packageName}}}.servers.Server; +{{#each allServers}} + {{#if subpackage}} +import {{{packageName}}}.{{subpackage}}.{{jsonPathPiece.pascalCase}}; + {{else}} +import {{packageName}}.{{jsonPathPiece.pascalCase}}; + {{/if}} +{{/each}} +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.Objects; + +public class ApiConfiguration { + private final ServerInfo serverInfo; + + public ApiConfiguration() { + serverInfo = new ServerInfo(); + } + + public ApiConfiguration(ServerInfo serverInfo) { + this.serverInfo = serverInfo; + } + + public static class ServerInfo { + {{#each allServers}} + protected final {{jsonPathPiece.pascalCase}} {{jsonPathPiece.camelCase}}; {{/each}} - {{#each paths}} - {{#if servers}} - {{#each servers}} -import {{packageName}}.paths.{{../@key.snakeCase}}.servers.{{jsonPathPiece.pascalCase}}; - {{/each}} - {{/if}} - {{#if operations}} - {{#each operations}} - {{#if servers}} - {{#each servers}} -import {{packageName}}.paths.{{../../@key.snakeCase}}.{{../@key.original}}.servers.{{jsonPathPiece.pascalCase}}; - {{/each}} - {{/if}} - {{/each}} - {{/if}} + + public ServerInfo() { + {{#each allServers}} + {{jsonPathPiece.camelCase}} = new {{jsonPathPiece.pascalCase}}(); {{/each}} -{{/if}} + } -public record ApiConfiguration() { + public ServerInfo( + {{#each allServers}} + @Nullable {{jsonPathPiece.pascalCase}} {{jsonPathPiece.camelCase}}{{#unless @last}},{{/unless}} + {{/each}} + ) { + {{#each allServers}} + this.{{jsonPathPiece.camelCase}} = Objects.requireNonNullElseGet({{jsonPathPiece.camelCase}}, {{jsonPathPiece.pascalCase}}::new); + {{/each}} + } + } + + {{#each allServers}} + public Server getServer({{jsonPathPiece.pascalCase}}. @Nullable ServerIndex serverIndex) { + return serverInfo.{{jsonPathPiece.camelCase}}.getServer(serverIndex); + } + {{/each}} } \ No newline at end of file diff --git a/src/main/resources/java/src/main/java/packagename/servers/ServerInfo.hbs b/src/main/resources/java/src/main/java/packagename/servers/ServerInfo.hbs index 1b69736f8b1..3f73b7eb5a3 100644 --- a/src/main/resources/java/src/main/java/packagename/servers/ServerInfo.hbs +++ b/src/main/resources/java/src/main/java/packagename/servers/ServerInfo.hbs @@ -4,6 +4,7 @@ package {{{packageName}}}.{{servers.subpackage}}; package {{{packageName}}}; {{/if}} +import {{{packageName}}}.exceptions.UnsetPropertyException; {{#each servers}} import {{{packageName}}}.{{subpackage}}.{{jsonPathPiece.pascalCase}}; {{/each}} @@ -64,7 +65,10 @@ public class {{servers.jsonPathPiece.pascalCase}} implements ServerProvider<{{se } public Server get(ServerIndex serverIndex) { - return servers.get(serverIndex); + if (servers.containsKey(serverIndex)) { + return get(serverIndex); + } + throw new UnsetPropertyException(serverIndex+" is unset"); } } @@ -74,7 +78,7 @@ public class {{servers.jsonPathPiece.pascalCase}} implements ServerProvider<{{se {{/each}} } - public Server get(@Nullable ServerIndex serverIndex) { + public Server getServer(@Nullable ServerIndex serverIndex) { if (serverIndex == null) { return servers.get(this.serverIndex); } diff --git a/src/main/resources/java/src/main/java/packagename/servers/ServerProvider.hbs b/src/main/resources/java/src/main/java/packagename/servers/ServerProvider.hbs index 6bc8cf80e08..7ae5f250dda 100644 --- a/src/main/resources/java/src/main/java/packagename/servers/ServerProvider.hbs +++ b/src/main/resources/java/src/main/java/packagename/servers/ServerProvider.hbs @@ -3,6 +3,6 @@ package {{{packageName}}}.servers; import org.checkerframework.checker.nullness.qual.Nullable; public interface ServerProvider { - Server get(@Nullable T serverIndex); + Server getServer(@Nullable T serverIndex); }