Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/goodforgod/dummymapper

IntelliJ IDEA plugin for mapping Java class to JSON/AVRO/GraphQL.
https://github.com/goodforgod/dummymapper

avro avro-schema code-tools graphql graphql-schema intellij intellij-idea intellij-plugin java json json-mapping json-schema kotlin mapping mapping-tools plugin pojo-to-avro pojo-to-graphql pojo-to-json

Last synced: 3 months ago
JSON representation

IntelliJ IDEA plugin for mapping Java class to JSON/AVRO/GraphQL.

Awesome Lists containing this project

README

        

# DummyMapper 🗎

![Java CI](https://github.com/GoodforGod/DummyMapper/workflows/CI%20Master/badge.svg)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=GoodforGod_DummyMapper&metric=alert_status)](https://sonarcloud.io/dashboard?id=GoodforGod_DummyMapper)
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=GoodforGod_DummyMapper&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=GoodforGod_micronaut-arangodb)
[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=GoodforGod_DummyMapper&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=GoodforGod_DummyMapper)

[Intellij IDEA plugin](https://plugins.jetbrains.com/plugin/14911-dummymapper-json-avro-graphql-/)
for mapping Java or Kotlin Classes to format like [JSON](#json)/[AVRO](#avro-schema)/[GraphQL](#graphql)/etc.

![](https://media.giphy.com/media/VelLohLlWETYZBNWlt/giphy.gif)

## Content
- [Installation](#installation)
- [Format Support](#format-support)
- [Json](#json)
- [Json Schema](#json-schema)
- [Avro Schema](#avro-schema)
- [GraphQL](#graphql)
- [Limitations](#limitations)

## Installation

- Install using [Intellij Plugin marketplace](https://plugins.jetbrains.com/plugin/14911-dummymapper-json-avro-graphql-/):
- Navigate to [site](https://plugins.jetbrains.com/plugin/14911-dummymapper-json-avro-graphql-/) and click *install* button there.
- Using IDE built-in plugin system on:
- **[Windows]** File > Settings > Plugins > Marketplace > Search for "DummyMapper" > Install
- **[MacOS]** Preferences > Settings > Plugins > Marketplace > Search for "DummyMapper" > Install
- Manually:
- Download the [latest release](https://github.com/goodforgod/DummyMapper/releases/latest), compile with *./gradlew buildPlugin* and install it manually using File > Settings > Plugins > ⚙️ > Install Plugin from Disk...

## Format Support

Plugin allow mapping Java or Kotlin Classes to different formats,
this section describes information about supported formats and their options.

All format examples will be showed according to this class as example.
Keep in mind that mapping is based on class **fields**, not *getters\setters*.

```java
public class User {

private class Credential {
private String id;
private long issued;
}

private UUID id;
private String name;
private List roles;
private Credential credential;
}
```

Supports also Kotlin classes:
```kotlin
data class ExampleData(val surname: String, val number: Int, val simple: ExampleSimple)

class ExampleSimple {

var en: ExampleEnum? = null
var name: String? = null
var data: ExampleData? = null
var surnames: List? = null
var numbers: Map? = null
var names: Set? = null
}
```

### Json

Plugin uses [DummyMaker library](https://github.com/GoodforGod/dummymaker) for generating classes and then converting to JSON format.

Allow mapping class as JSON example with fields filled with
random values as if class example was serialized to JSON format.
- Mapping options.. > Map as JSON

```json
{
"id" : "76fbb591-8e95-4df7-94ad-5d0606709141",
"name" : "Alan",
"roles" : [ "herbalist", "recycling-officer", "exploration-geologist" ],
"credential" : {
"id" : "a7ec6315-4292-454c-ac69-e61a7a36e07d",
"issued" : 1565743683
}
}
```

Also allow mapping class as array of JSONs.
- Mapping options.. > Map as JSON Array

You can specify number of entries to generate in array.

```json
[
{
"id": "526686d0-8d84-4b85-a751-77fa7a157a69",
"name": "Rachel",
"roles": [ "fitness-centre-manager", "plant-breeder", "television-production-assistant" ],
"credential": {
"id": "94f349c9-405a-4921-8dbf-8abfacc28cf1",
"issued": 1548018277
}
}
]
```

![](https://media.giphy.com/media/VelLohLlWETYZBNWlt/giphy.gif)

#### Annotations Support

Annotations from [Jackson](https://www.baeldung.com/jackson-annotations) are supported when serializing to JSON,
but keep in mind that only *fields* used for serialization, not *getters*.

However, [Jackson annotations](https://www.baeldung.com/jackson-annotations) from respected fields *getters*
will be applied during serialization if found, but such behavior is not guarantied to be always correct.

Example for *User* class:
```java
public class User {

private UUID id;
@JsonProperty(value = "firstName")
private String name;
private String surname;
@JsonIgnore
private List roles;

@JsonIgnore
public String getSurname() {
return surname;
}
}
```

Mapping *User* class as JSON will result in (most of [Jackson annotations](https://www.baeldung.com/jackson-annotations)
and their parameters are supported):
```json
{
"id": "975e80ed-b95d-46b2-9338-519ff7083dd3",
"firstName": "Alfred"
}
```

### Json Schema

Allow mapping class as JSON Schema, 3 different drafts are available:
- [Draft 2020-12](https://json-schema.org/draft/2020-12/release-notes.html)
- [Draft 2019-09](https://json-schema.org/draft/2019-09/release-notes.html)
- [Draft 7](https://json-schema.org/draft-07/json-schema-release-notes.html)
- [Draft 6](https://json-schema.org/draft-06/json-schema-release-notes.html)

Mapping is under:
- Mapping options.. > Map as JSON Schema

```json
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "object",
"properties": {
"credential": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"issued": {
"type": "integer"
}
}
},
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"roles": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
```

![](https://media.giphy.com/media/YMY6Rd9fxUkIU4BoKF/giphy.gif)

### Avro Schema

Allow mapping class as AVRO Schema for [version 1.9.2](https://avro.apache.org/docs/1.9.2/)

There is option for two different AVRO Schema builders like:
- [Jackson](https://github.com/FasterXML/jackson-dataformats-binary/tree/master/avro)
- [Apache](https://avro.apache.org/docs/1.9.2/gettingstartedjava.html)

Mapping is under:
- Mapping options.. > Map as AVRO Schema

```json
{
"type" : "record",
"name" : "User",
"namespace" : "io.goodforgod.dummymapper",
"fields" : [ {
"name" : "id",
"type" : {
"type" : "record",
"name" : "UUID",
"namespace" : "java.util",
"fields" : [ ]
}
}, {
"name" : "name",
"type" : "string"
}, {
"name" : "roles",
"type" : {
"type" : "array",
"items" : "string",
"java-class" : "java.util.List"
}
}, {
"name" : "credential",
"type" : {
"type" : "record",
"name" : "Credential",
"fields" : [ {
"name" : "id",
"type" : "string"
}, {
"name" : "issued",
"type" : "long"
} ]
}
} ]
}
```

#### Jackson Annotation Support

[Jackson annotations](https://www.baeldung.com/jackson-annotations) are supported when serializing to AVRO Schema,
but keep in mind that only *fields* used for serialization, not *getters*.

Most of [Jackson annotations](https://www.baeldung.com/jackson-annotations)
and their parameters are supported.

Example on how to mark field as required (Option *Required By Default* apply such transformation for all fields if selected):
```java
public class User {

@JsonProperty(required = true)
private UUID id;
private String name;
private String surname;
}
```

#### Apache Annotation Support

Annotations from [Apache Avro](https://github.com/apache/avro/tree/master/lang/java/avro/src/main/java/org/apache/avro/reflect) library supported when serializing to AVRO Schema.

### GraphQL

Allow mapping class as GraphQL query with [version v14](https://www.graphql-java.com/documentation/v14)

Mapping is under:
- Mapping options.. > Map as GraphQL

```text
schema {
query: Query
}

type Credential {
id: String
issued: Long!
}

#Query root
type Query {
credential: Credential
id: UUID
name: String
roles: [String]
}

#Long type
scalar Long

#Unrepresentable type
scalar UNREPRESENTABLE

#UUID String
scalar UUID
```

#### Annotation Support

Annotations from [GraphQL SPQR](https://github.com/leangen/graphql-spqr/tree/master/src/main/java/io/leangen/graphql/annotations) library supported when serializing to GraphQL.

## Limitations

There some limitations for plugin when mapping classes:
- *Enums* are not supported and thus will be displayed as *String* fields, except for *Map as JSON* where enum values will be generated correctly.
This issue is due to library used in plugin unable to create Java Enums.
- Getters, Setters not used for any mapping, *only annotations from getters* are used for mapping (depends on operation)
if found. Standard Java naming convention expected from *getters* to be found.
- Annotations Class Params are not supported. Thus, most of annotation parameters like String, Boolean, etc. are supported, but Class or other complex parameters not supported.

## Licence

This project licensed under the Apache 2.0 - see the [LICENSE](LICENSE) file for details.