Code Monkey home page Code Monkey logo

json-kotlin-gradle's People

Contributors

pwall567 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

json-kotlin-gradle's Issues

error: "Error creating output directory - build/generated-sources/kotlin/com/app"

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"
        }
      }
    }
  }
}
´´´

No Companion Objects generated

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?

Provide example project

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?

Help with requirements for generating Kotlin classes

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?

  1. Add @serializable as an annotation to all generated classes.
  2. Have a generated class implement an existing interface.
  3. Force a property to use Map<Any, Any> as a type.

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!

Publish to the Gradle Plugin Portal

Publishing this to the Gradle Plugin Portal would enable the resolution of this plugin from within the plugins block, instead of using buildscript

Multiple files not working with json pointer

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"
        }
      }
    }
  }
}

Option 1: Specify directory only

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.

Option 2: Specify directory and json pointer

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.

Option 3: Specify schema file with json pointer

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?

Option 4: Put everything in one file with the same json pointer

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?

Using the generated-sources from another module

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.