Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/pwall567/json-kotlin-maven

Maven JSON Schema code generation plugin
https://github.com/pwall567/json-kotlin-maven

json-schema kotlin maven-plugin

Last synced: 16 days ago
JSON representation

Maven JSON Schema code generation plugin

Awesome Lists containing this project

README

        

# json-kotlin-maven

[![Build Status](https://github.com/pwall567/json-kotlin-maven/actions/workflows/build.yml/badge.svg)](https://github.com/pwall567/json-kotlin-maven/actions/workflows/build.yml)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Kotlin](https://img.shields.io/static/v1?label=Kotlin&message=v1.9.24&color=7f52ff&logo=kotlin&logoColor=7f52ff)](https://github.com/JetBrains/kotlin/releases/tag/v1.9.24)
[![Maven Central](https://img.shields.io/maven-central/v/net.pwall.json/json-kotlin-maven?label=Maven%20Central)](https://search.maven.org/search?q=g:%22net.pwall.json%22%20AND%20a:%22json-kotlin-maven%22)

Maven JSON Schema code generation plugin.

## Background

The [`json-kotlin-schema-codegen`](https://github.com/pwall567/json-kotlin-schema-codegen) project provides a means of
generating Kotlin or Java classes (or TypeScript interfaces) from [JSON Schema](https://json-schema.org/) object
descriptions.
The `json-kotlin-maven` plugin simplifies the use of this code generation mechanism from
[Maven](https://maven.apache.org/).

## To Use

First, ensure you have a `pluginRepository` entry for Maven Central:
```xml


central
Maven Central
https://repo1.maven.org/maven2/


```

Then, in the plugins section:
```xml

net.pwall.json
json-kotlin-maven
0.111


generate-sources

codegen




```
And to have the generated source files compiled:
```xml
target/generated-sources/kotlin
```
(this shows the use of the default output directory; if it is changed by the use of the `outputDir` parameter, this line
will also change.)

The plugin follows the principle of "convention over configuration".
The default location for the schema file or files to be input to the generation process is:
```
«project root»/src/main/resources/schema
```
and the default location for the
[config file](https://github.com/pwall567/json-kotlin-schema-codegen/blob/main/CONFIG.md) (if required) is:
```
«project root»/src/main/resources/codegen-config.json
```
If your files are in these default locations, the above additions to `pom.xml` will be all you will need.

If you wish to specify the location of your schema or config files, or if you wish to use any of the other plugin
customisation options, you may include a `configuration` section in the `plugin` declaration as follows:
```xml

net.pwall.json
json-kotlin-maven
0.111

path/to/your/config.json
path/to/your/schema/file/or/files
your.package.name
/$defs
Test comment 12345



generate-sources

codegen




```

If any setting in this configuration block conflicts with the equivalent setting in the config file, this configuration
block (in `pom.xml`) takes precedence.

## Configuration Options

### `configFile`

If the config file is not in the default location of `src/main/resources/codegen-config.json`, the location of the file
may be specified using the `configFile` property:
```xml
path/to/your/config.json
```

### `inputFile`

The input to the code generation process may be a single file or a directory tree containing a number of schema files.
By default, the plugin will process all files in the `src/main/resources/schema` directory (and subdirectories), but to
specify an alternative file or directory, use:
```xml
path/to/your/schema/file/or/files
```

### `pointer`

One of the common uses of the code generator is to generate a set of classes from a composite file.
For example, an OpenAPI file may contain a number of schema definitions for the request and response objects of the API,
or a JSON Schema file may contain a set of definitions in the `$defs` section.

To specify this form of usage to the plugin, the configuration block must contain:
```xml
path/to/your/schema/file/or/files
/pointer/to/definitions
```

For example, to process the entire set of schema definitions in an OpenAPI file:
```xml
src/main/resources/openapi/openapi.yaml
/components/schemas
```

### `outputDir`

The default output directory is `target/generated-sources/«language»`, where `«language»` is `kotlin`, `java` or
`ts` (for TypeScript), depending on the target language.
To specify a different output directory, use:
```xml
path/to/your/output/directory
```

### `language`

The default target language is Kotlin (naturally enough, for a project written in Kotlin and primarily intended to
target Kotlin).
To specify Java output:
```xml
java
```
The value of this setting must be `kotlin`, `java` or `typescript`.

This setting may be specified in the config file, and that is the recommended practice.

### `packageName`

By default, classes will be generated without a `package` directive.
To supply a package name:
```xml
com.example.model
```

This setting may be specified in the config file, and that is the recommended practice.

### `comment`

The `comment` allows the provision of a line of text to be added to the comment block output to the start of each
generated file:
```xml
This was generated from Schema version 1.0
```

This setting may be specified in the config file, and that is the recommended practice.

Peter Wall

2024-10-28