Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rebase with original #1

Merged
merged 27 commits into from
Jul 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
5d94cf7
Added new rule for creating the inner class of XYZBuilder on generate…
dewthefifth Dec 2, 2018
d4a1b13
[maven-release-plugin] prepare release jsonschema2pojo-1.0.0
joelittlejohn Dec 13, 2018
fb12b98
[maven-release-plugin] prepare for next development iteration
joelittlejohn Dec 13, 2018
bbc96b9
[release] updating README.md for 1.0.0
joelittlejohn Dec 14, 2018
8a06edf
[release] Update README.md, CHANGELOG.md and CONTRIBUTORS.md for 1.0.0
joelittlejohn Dec 14, 2018
f14ffbb
Added inheritance support to the InnerBuilder implementation
dewthefifth Jan 21, 2019
cc4b978
Updated configurations to remove 'chainable setters' as a property
dewthefifth Jan 24, 2019
797dad0
Added code to invoke non-standard consturctors from the InnerClassBui…
dewthefifth Jan 24, 2019
feee8ab
Removed duplicate copyright notices
dewthefifth Jan 24, 2019
eeb11ae
Added additional documentation to the ant tasks
dewthefifth Jan 24, 2019
32df886
Fixed an issue with the ConstructorRule that caused the Builder const…
dewthefifth Jan 28, 2019
3361f44
Added integration tests for the inner builder classes
dewthefifth Feb 3, 2019
0b5b0e1
Merge pull request #953 from dewthefifth/features/builders-as-inner-c…
joelittlejohn Feb 5, 2019
73f0900
check schema required array before is required flag
newmen Mar 26, 2019
3eaf980
add tests to check required/optional getters
newmen Mar 26, 2019
64b9e5c
fix mistake of attribute name
newmen Mar 26, 2019
8207053
Merge pull request #964 from newmen/fix-required-or-optional
joelittlejohn Mar 27, 2019
f3ce504
[maven-release-plugin] prepare release jsonschema2pojo-1.0.1
joelittlejohn Apr 1, 2019
5e22b74
[maven-release-plugin] prepare for next development iteration
joelittlejohn Apr 1, 2019
8c628ac
[release] updating README.md for 1.0.1
joelittlejohn Apr 13, 2019
23ace8f
[release] CHANGELOG.md and CONTRIBUTORS.md for 1.0.1
joelittlejohn Apr 13, 2019
709c95b
Update README.md
joelittlejohn Apr 13, 2019
343939b
Dependency upgrade
joelittlejohn Apr 14, 2019
67ccf83
Dependency upgrade
joelittlejohn Apr 14, 2019
0552b80
Add support for including JsonTypeInfo Annotation (#967)
ddcruver Apr 23, 2019
9123019
Fix #975: Add support for root level enum titles and descriptions. (#…
john-tipper May 1, 2019
9f60132
977 add digits rule (#978)
john-tipper May 13, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
# Changelog

## 1.0.1
* Check schema required array before is required flag ([#964](https://github.com/joelittlejohn/jsonschema2pojo/pull/964))
* Features/builders as inner classes ([#953](https://github.com/joelittlejohn/jsonschema2pojo/pull/953))

## 1.0.0
* Don't register output directory as a set of files, since this breaks Gradle 5.0+ ([#940](https://github.com/joelittlejohn/jsonschema2pojo/pull/940))
* **Builder method should be named consistently with getter/setter ([#905](https://github.com/joelittlejohn/jsonschema2pojo/issues/905))**
* Objects inside array with name ending with "List" are generated as "SomeNameList" ([#780](https://github.com/joelittlejohn/jsonschema2pojo/issues/780))

## 1.0.0-beta1
* Add 'formatTypeMapping' config option to allow overriding types used for formats ([#923](https://github.com/joelittlejohn/jsonschema2pojo/pull/923))
* **Enable development of context-aware rules. ([#917](https://github.com/joelittlejohn/jsonschema2pojo/pull/917))**
* **Modify Rule#apply signature to include parent node (allows context-aware rules) ([#917](https://github.com/joelittlejohn/jsonschema2pojo/pull/917))**
* Add 'javaOptional' extension rule to allow individual fields to use Java Optional on getter ([#913](https://github.com/joelittlejohn/jsonschema2pojo/pull/913))
* Allow schema title to be used as class name (new useTitleAsClassname config option) ([#908](https://github.com/joelittlejohn/jsonschema2pojo/issues/908))
* Incorrect @Nullable JSR305 annotations generated when using 'required' array ([#906](https://github.com/joelittlejohn/jsonschema2pojo/issues/906))
Expand Down
3 changes: 3 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,6 @@
* Klevis Ramo
* Martin Bramwell
* Jan Oopkaup
* Michał Szymborski
* Duane Zamrok
* Gleb Averchuk
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# jsonschema2pojo [![Build Status](https://travis-ci.org/joelittlejohn/jsonschema2pojo.svg?branch=master)](https://travis-ci.org/joelittlejohn/jsonschema2pojo) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.jsonschema2pojo/jsonschema2pojo/badge.svg)](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22org.jsonschema2pojo%22)

Current stable: **0.5.1**<br>
Current unstable: **1.0.0-beta1**<br>

_jsonschema2pojo_ generates Java types from JSON Schema (or example JSON) and can annotate those types for data-binding with Jackson 1.x, Jackson 2.x or Gson.

**_*Note:*_ there are breaking changes between 0.5.1 and 1.0.0. Check the [change log](https://github.com/joelittlejohn/jsonschema2pojo/blob/master/CHANGELOG.md). Anything marked in bold in the 1.0.0 alpha, beta and final release is a breaking change.**

### [Try jsonschema2pojo online](http://jsonschema2pojo.org/)<br>or `brew install jsonschema2pojo`

You can use jsonschema2pojo as a Maven plugin, an Ant task, a command line utility, a Gradle plugin or embedded within your own Java app. The [Getting Started](https://github.com/joelittlejohn/jsonschema2pojo/wiki/Getting-Started) guide will show you how.
Expand All @@ -14,7 +13,7 @@ A very simple Maven example:
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>0.5.1</version>
<version>1.0.1</version>
<configuration>
<sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
<targetPackage>com.example.types</targetPackage>
Expand All @@ -33,9 +32,9 @@ Useful pages:
* **[Getting started](https://github.com/joelittlejohn/jsonschema2pojo/wiki/Getting-Started)**
* **[How to contribute](https://github.com/joelittlejohn/jsonschema2pojo/blob/master/CONTRIBUTING.md)**
* [Reference](https://github.com/joelittlejohn/jsonschema2pojo/wiki/Reference)
* [Latest Javadocs](https://joelittlejohn.github.io/jsonschema2pojo/javadocs/0.5.1/)
* [Documentation for the Maven plugin](https://joelittlejohn.github.io/jsonschema2pojo/site/0.5.1/generate-mojo.html)
* [Documentation for the Ant task](https://joelittlejohn.github.io/jsonschema2pojo/site/0.5.1/Jsonschema2PojoTask.html)
* [Latest Javadocs](https://joelittlejohn.github.io/jsonschema2pojo/javadocs/1.0.1/)
* [Documentation for the Maven plugin](https://joelittlejohn.github.io/jsonschema2pojo/site/1.0.1/generate-mojo.html)
* [Documentation for the Ant task](https://joelittlejohn.github.io/jsonschema2pojo/site/1.0.1/Jsonschema2PojoTask.html)

Project resources:
* [Downloads](https://github.com/joelittlejohn/jsonschema2pojo/releases)
Expand Down
2 changes: 1 addition & 1 deletion jsonschema2pojo-ant/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>jsonschema2pojo</artifactId>
<groupId>org.jsonschema2pojo</groupId>
<version>1.0.0-beta2-SNAPSHOT</version>
<version>1.0.2-SNAPSHOT</version>
</parent>

<artifactId>jsonschema2pojo-ant</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public class Jsonschema2PojoTask extends Task implements GenerationConfig {

private boolean generateBuilders;

private boolean includeTypeInfo = false;

private boolean useInnerClassBuilders = false;

private boolean includeConstructors = false;

private boolean usePrimitives;
Expand Down Expand Up @@ -300,6 +304,22 @@ public void setGenerateBuilders(boolean generateBuilders) {
this.generateBuilders = generateBuilders;
}

/**
* Sets the 'includeTypeInfo' property of this class.
*
* @param includeTypeInfo
* Whether to use include json type info information to class. Often required to support polymorphic type deserialization.
* <p>
* By default the type information is stored in the @class property, this can be overridden in the deserializationClassProperty of the schema.
* <p>
* Default: <code>false</code>.
*
* @see <a href="Jackson Polymorphic Deserialization">https://github.com/FasterXML/jackson-docs/wiki/JacksonPolymorphicDeserialization</a>
*/
public void setIncludeTypeInfo(boolean includeTypeInfo) {
this.includeTypeInfo = includeTypeInfo;
}

/**
* Sets the 'usePrimitives' property of this class.
*
Expand Down Expand Up @@ -890,6 +910,12 @@ public boolean isGenerateBuilders() {
return generateBuilders;
}

@Override
public boolean isIncludeTypeInfo()
{
return includeTypeInfo;
}

@Override
public boolean isUsePrimitives() {
return usePrimitives;
Expand Down Expand Up @@ -1201,5 +1227,18 @@ public Language getTargetLanguage() {
public Map<String, String> getFormatTypeMapping() {
return formatTypeMapping;
}


@Override
public boolean isUseInnerClassBuilders() {
return useInnerClassBuilders;
}

/**
* Sets the 'useInnerClassBuilders' property of this class
*
* @param useInnerClassBuilders determines whether builders will be chainable setters or embedded classes when {@link #isGenerateBuilders()} used
*/
public void setUseInnerClassBuilders(boolean useInnerClassBuilders) {
this.useInnerClassBuilders = useInnerClassBuilders;
}
}
13 changes: 13 additions & 0 deletions jsonschema2pojo-ant/src/site/Jsonschema2PojoTask.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,19 @@ <h3>Parameters</h3>
</td>
<td align="center" valign="top">No (default <code>false</code>)</td>
</tr>
<tr>
<td valign="top">includeTypeInfo</td>
<td valign="top">
<p>Whether to include json type information; often required to support polymorphic type handling.</p>
<p>By default the type information is stored in the @class property, this can be overridden in the deserializationClassProperty of the schema.</p>
</td>
<td align="center" valign="top">No (default <code>false</code>)</td>
</tr>
<tr>
<td valign="top">useInnerClassBuilders</td>
<td valign="top">Determines whether builders will be chainable setters or embedded classes when generateBuilders is used.</td>
<td align="center" valign="top">No (default <code>false</code>)</td>
</tr>
<tr>
<td valign="top">includeGetters</td>
<td valign="top">Whether to include getters or to omit these accessor methods and create public fields instead.</td>
Expand Down
2 changes: 1 addition & 1 deletion jsonschema2pojo-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>jsonschema2pojo</artifactId>
<groupId>org.jsonschema2pojo</groupId>
<version>1.0.0-beta2-SNAPSHOT</version>
<version>1.0.2-SNAPSHOT</version>
</parent>

<artifactId>jsonschema2pojo-cli</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public class Arguments implements GenerationConfig {
@Parameter(names = { "-b", "--generate-builders" }, description = "Generate builder-style methods as well as setters")
private boolean generateBuilderMethods = false;

@Parameter(names = { "--include-type-info" }, description = "Include json type info; required to support polymorphic type handling. https://github.com/FasterXML/jackson-docs/wiki/JacksonPolymorphicDeserialization")
private boolean includeTypeInfo = false;

@Parameter(names = { "--use-inner-class-builders" }, description = "Generate an inner class with builder-style methods")
private boolean useInnerClassBuilders = false;

@Parameter(names = { "-c", "--generate-constructors" }, description = "Generate constructors")
private boolean generateConstructors = false;

Expand Down Expand Up @@ -278,6 +284,17 @@ public boolean isGenerateBuilders() {
return generateBuilderMethods;
}

@Override
public boolean isIncludeTypeInfo()
{
return includeTypeInfo;
}

@Override
public boolean isUseInnerClassBuilders() {
return useInnerClassBuilders;
}

@Override
public boolean isUsePrimitives() {
return usePrimitives;
Expand Down
2 changes: 1 addition & 1 deletion jsonschema2pojo-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>jsonschema2pojo</artifactId>
<groupId>org.jsonschema2pojo</groupId>
<version>1.0.0-beta2-SNAPSHOT</version>
<version>1.0.2-SNAPSHOT</version>
</parent>

<artifactId>jsonschema2pojo-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public AbstractAnnotator(GenerationConfig generationConfig) {
this.generationConfig = generationConfig;
}

@Override
public void typeInfo(JDefinedClass clazz, JsonNode schema) {

}

@Override
public void propertyOrder(JDefinedClass clazz, JsonNode propertiesNode) {
}
Expand Down Expand Up @@ -91,6 +96,11 @@ public boolean isAdditionalPropertiesSupported() {
public void additionalPropertiesField(JFieldVar field, JDefinedClass clazz, String propertyName) {
}

@Override
public boolean isPolymorphicDeserializationSupported(JsonNode node) {
return false;
}

public GenerationConfig getGenerationConfig() {
return generationConfig;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright © 2010-2017 Nokia
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jsonschema2pojo;

import com.fasterxml.jackson.databind.JsonNode;
import com.sun.codemodel.JDefinedClass;

public abstract class AbstractTypeInfoAwareAnnotator extends AbstractAnnotator
{
public AbstractTypeInfoAwareAnnotator(GenerationConfig generationConfig) {
super(generationConfig);
}

@Override
public void typeInfo(JDefinedClass clazz, JsonNode node) {
if(getGenerationConfig().isIncludeTypeInfo()) {
// Have per-schema JavaTypeInfo configuration override what is defined in generation config; backward comparability
if (node.has("deserializationClassProperty")) {
String annotationName = node.get("deserializationClassProperty").asText();
addJsonTypeInfoAnnotation(clazz, annotationName);
} else {
addJsonTypeInfoAnnotation(clazz, "@class");
}
} else {
// per-schema JsonTypeInfo configuration
if (node.has("deserializationClassProperty")) {
String annotationName = node.get("deserializationClassProperty").asText();
addJsonTypeInfoAnnotation(clazz, annotationName);
}
}
}

@Override
public boolean isPolymorphicDeserializationSupported(JsonNode node) {
return getGenerationConfig().isIncludeTypeInfo() || node.has("deserializationClassProperty");
}

abstract protected void addJsonTypeInfoAnnotation(JDefinedClass clazz, String propertyName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@
*/
public interface Annotator {

/**
* Add the necessary annotation to dictate correct type information during
* serialization and deserialization; often required with polymorphic types.
*
* @see <a
* href="https://github.com/FasterXML/jackson-docs/wiki/JacksonPolymorphicDeserialization">Jackson Docs - Polymorphic Type Handling</a>
*
* @param clazz
* a generated pojo class, that is serialized to JSON
* @param schema
* the object schema associated with this clazz
*/
void typeInfo(JDefinedClass clazz, JsonNode schema);

/**
* Add the necessary annotation to dictate correct property order during
* serialization
Expand Down Expand Up @@ -195,4 +209,6 @@ public interface Annotator {
void timeField(JFieldVar field, JDefinedClass clazz, JsonNode propertyNode);

void additionalPropertiesField(JFieldVar field, JDefinedClass clazz, String propertyName);

boolean isPolymorphicDeserializationSupported(JsonNode node);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ public CompositeAnnotator(Annotator... annotators) {
this.annotators = annotators;
}

@Override
public void typeInfo(JDefinedClass clazz, JsonNode node) {
for (Annotator annotator : annotators) {
annotator.typeInfo(clazz, node);
}
}

@Override
public void propertyOrder(JDefinedClass clazz, JsonNode propertiesNode) {
for (Annotator annotator : annotators) {
Expand Down Expand Up @@ -130,7 +137,17 @@ public void additionalPropertiesField(JFieldVar field, JDefinedClass clazz, Stri
}
}

@Override
@Override
public boolean isPolymorphicDeserializationSupported(JsonNode node) {
for (Annotator annotator : annotators) {
if (!annotator.isPolymorphicDeserializationSupported(node)) {
return false;
}
}
return true;
}

@Override
public void dateTimeField(JFieldVar field, JDefinedClass clazz, JsonNode propertyNode) {
for (Annotator annotator : annotators) {
annotator.dateTimeField(field, clazz, propertyNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ public boolean isGenerateBuilders() {
return false;
}

/**
* @return <code>false</code>
*/
@Override
public boolean isIncludeTypeInfo()
{
return false;
}

/**
* @return <code>false</code>
*/
Expand Down Expand Up @@ -444,5 +453,7 @@ public Language getTargetLanguage() {
public Map<String, String> getFormatTypeMapping() {
return Collections.emptyMap();
}


@Override
public boolean isUseInnerClassBuilders() { return false; }
}
Loading