pwall567 / json-kotlin-gradle Goto Github PK
View Code? Open in Web Editor NEWGradle JSON Schema code generation plugin
Gradle JSON Schema code generation plugin
Hi,
I'm trying to execute the gradle plugin but is trowing the following error: "Error creating output directory - build/generated-sources/kotlin/com/app". What could it be?
I've used a clean kotlin project from intelliJ.
Without the plugin using CodeGenerator() it's working fine with the shared json schema.
Also I've tried "outputDir.set(file("src/main/schema/example"))" but it will only generate:
data class Test3(
val warehouse: BigDecimal? = null,
val retail: BigDecimal? = null
)
expected:
/*
* Test.kt
*
* This code was generated by json-kotlin-schema-codegen - JSON Schema Code Generator
* See https://github.com/pwall567/json-kotlin-schema-codegen
*
* It is not advisable to modify generated code as any modifications will be lost
* when the generation process is re-run.
*/
import java.math.BigDecimal
import java.math.BigInteger
data class Test(
/** Product identifier */
val id: BigInteger,
/** Name of the product */
val name: String,
val price: BigDecimal,
val tags: List<String>? = null,
val stock: Test3? = null,
val stock2: Test3? = null
) {
data class Test3(
val warehouse: BigDecimal? = null,
val retail: BigDecimal? = null
)
}
build.gradle.kts:
import net.pwall.json.kotlin.codegen.gradle.JSONSchemaCodegenPlugin
import net.pwall.json.kotlin.codegen.gradle.JSONSchemaCodegen
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
buildscript {
repositories {
mavenLocal()
jcenter()
}
dependencies {
classpath("net.pwall.json:json-kotlin-gradle:0.34")
}
}
apply<JSONSchemaCodegenPlugin>()
configure<JSONSchemaCodegen> {
packageName.set("com.app")
inputFile.set(file("src/main/schema/example/example2.json")) // probably in src/main/resources/...
// outputDir.set(file("src/main/schema/example"))
pointer.set("/definitions") // a JSON Pointer to the group of definitions within the file
generatorComment.set("comment...")
classMappings { // configure specific class mappings if required
byFormat("java.time.Duration", "duration")
}
schemaExtensions { // configure extension keyword uses if required
patternValidation("x-type", "account-number", Regex("^[0-9]{4,10}$"))
}
}
plugins {
kotlin("jvm") version "1.5.10"
}
group = "me.arpel4"
version = "1.0-SNAPSHOT"
dependencies {
implementation("net.pwall.json:json-kotlin-schema-codegen:0.35")
implementation(kotlin("stdlib-jdk8"))
}
tasks.withType<KotlinCompile>() {
kotlinOptions.jvmTarget = "11"
}
repositories {
mavenCentral()
}
json-schema:
{
"$schema": "http://json-schema.org/draft/2019-09/schema",
"$id": "http://pwall.net/test",
"title": "Product",
"type": "object",
"required": ["id", "name", "price"],
"properties": {
"id": {
"type": "number",
"format": "bigint",
"description": "Product identifier"
},
"name": {
"type": "string",
"description": "Name of the product"
},
"price": {
"type": "number"
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
},
"stock": {
"$ref": "#/definitions/test3"
},
"stock2": {
"$ref": "#/definitions/test3"
}
},
"definitions": {
"test3": {
"type": "object",
"properties": {
"warehouse": {
"type": "number"
},
"retail": {
"type": "number"
}
}
}
}
}
´´´
Hi,
in my src/main/resources/codegen-config.json
I have set companionObject: true
. When generating the code, no Companion Objects are generated. However, the package name setting in the same codegen-config.json
works as expected.
What could be the reason?
I am new to the Kotlin and Gradle world and I have problems to get a small test project working.
Could you please provide an example project?
Currently, if you enable Gradle configuration cache it fails with:
Execution failed for task ':generate'.
> Extension of type 'JSONSchemaCodegen' does not exist. Currently registered extension types: [ExtraPropertiesExtension]
Please honor the feature
I have a few requirements for converting existing JSON schema to Kotlin classes in my organization that I thought I’d raise here. I couldn’t find an immediate solution for them and wanted to see if you have any suggestions?
Both 2 and 3 are possible using the jsonschema2pojo library that my organization is currently using to generate POJOs. Using javaInterfaces
for 2 and existingJavaType
for 3. I believe these are not standards in the JSON Schema (Draft 07) so I understand if these are not something that are currently possible and won't be part of the plan for your repos.
It’s possible that the json-kotlin-schema-codegen or json-kotlin-schema libraries may be a better place for asking you about these requirements, but I wanted to open just one issue for easier communication.
Your libraries have been very useful, thank you for creating them!
Publishing this to the Gradle Plugin Portal would enable the resolution of this plugin from within the plugins
block, instead of using buildscript
This could have saved me considerable time debugging. Thank you!
I have an issue with generating data classes from multiple json schema files that reference each other. The idea is to have a shared schema from which other schemas can reuse.
I've tried a couple of options but have ran out of ideas. I'm using the following files:
shared.json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "#/shared/SharedA",
"shared": {
"SharedA": {
"type": "object",
"properties": {
"foo": {
"type": "string"
}
}
}
}
}
a.json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "#/a/A",
"a": {
"A": {
"type": "object",
"properties": {
"baz": {
"type": "string"
},
"shared": {
"$ref": "shared.json#/shared/SharedA"
}
}
}
}
}
This works but generates "ugly" data classes. The classes even get names based on the filename, not from the json schema.
Gradle definition:
configure<JSONSchemaCodegen> {
packageName.set("com.example.generated")
inputFile.set(file("src/main/resources/schema"))
outputDir.set(file("build/generated"))
}
Generates this:
data class A(
val baz: String? = null,
val shared: Shared? = null
) {
data class Shared(
val foo: String? = null
)
}
Notice that the shared field contains an inner class named Shared instead of SharedA. It also generates another file with the Shared class definition. The inner class is redundant! Not very nice imho.
The following Gradle definition doesn't work at all.
configure<JSONSchemaCodegen> {
packageName.set("com.example.generated")
inputFile.set(file("src/main/resources/schema"))
outputDir.set(file("build/generated"))
pointer.set("/a”)
}
Gives this error:
> Error reading schema file - /Users/magnus/code/example/src/main/resources/schema
It seems like it trying to read the directory as a file.
Doesn't work at all.
configure<JSONSchemaCodegen> {
packageName.set("com.example.generated")
inputFile.set(file("src/main/resources/schema/a.json"))
outputDir.set(file("build/generated"))
pointer.set("/a”)
}
Gives this error:
> Error reading schema file - https:/pwall.net/shared.json
Here the code generator doesn't understand that the reference to shared.json
is a local file but adds a https prefix to it. I've tried to add file://
but failed to make it work. Even if it had worked I wonder how the plugin would handle the two difference json pointers?
This works and creates three nice files as expected, but the schema becomes a "monolith". It defies the purpose of having decoupled definitions imho.
When using the json pointer I get nice data classes, i.e. one kotlin source file per definition, but it only allows one file! It seems there is lacking an option to specify multiple files with a json pointer for each file. Or how do I solve this issue?
Hi,
First of all big thanks for the great library!
I am not sure if this is really an issue with the plugin or more how I use it in my setup.
I have gradle subproject "model" where the only thing I have is the json schema files in src/main/resources/schema and no other sources.
build.gradle.kts:
configure<JSONSchemaCodegen> {
packageName.set("com.example")
inputs {
inputComposite {
file.set(file("src/main/resources/schema/sample_schema.json"))
pointer.set("/definitions")
}
}
}
sourceSets.main {
java.srcDirs("build/generated-sources/kotlin")
}
Then in another subproject "service" I have the following:
dependencies {
implementation(project(":model"))
}
In the top level settings.gradle.kts I have:
rootProject.name = "example"
include("model")
include("service")
when I execute:
./gradlew clean build
I always get errors in "service" for Unresolved reference to classes which should have been generated from the src/main/resources/schema
And indeed the build/generated-sources
folder in "model" is missing even though the build and the jars in build/libs
are generated
indicating there is some problem with the build not depending on the generated task.
The same issue is happening when I run:
./gradlew co360-model:build
The strange thing is if I run the build task from Intellij gradle window the generateKotlin task is executed and the generated-sources are build. I am not able to understand what Intellij is executing under the hood.
Any hints are highly appreciated.
Best regards,
Rumen
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.