Skip to content

Commit

Permalink
Fix joelittlejohn#975: Add support for root level enum titles and des…
Browse files Browse the repository at this point in the history
…criptions.
  • Loading branch information
john-tipper committed Apr 30, 2019
1 parent f3ce504 commit ecd7b09
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ public JType apply(String nodeName, JsonNode node, JsonNode parent, JClassContai
addEnumConstants(node.path("enum"), _enum, node.path("javaEnumNames"), backingType);
addFactoryMethod(_enum, backingType);

if (node.has("title")) {
ruleFactory.getTitleRule().apply(nodeName, node.get("title"), node, _enum, schema);
}

if (node.has("description")) {
ruleFactory.getDescriptionRule().apply(nodeName, node.get("description"), node, _enum, schema);
}

return _enum;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* 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.integration;

import com.thoughtworks.qdox.JavaDocBuilder;
import com.thoughtworks.qdox.model.JavaClass;
import org.jsonschema2pojo.integration.util.Jsonschema2PojoRule;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;

/*
Enums are treated differently to schemas of type object and we want to ensure that a description
added to root-level enums is added to the javadoc.
*/
public class DescriptionEnumIT {

@ClassRule public static Jsonschema2PojoRule schemaRule = new Jsonschema2PojoRule();

private static JavaClass classWithDescription;

@BeforeClass
public static void generateClasses() throws IOException {

schemaRule.generateAndCompile("/schema/description/descriptionEnum.json", "com.example");
File generatedJavaFile = schemaRule.generated("com/example/DescriptionEnum.java");

JavaDocBuilder javaDocBuilder = new JavaDocBuilder();
javaDocBuilder.addSource(generatedJavaFile);

classWithDescription = javaDocBuilder.getClassByName("com.example.DescriptionEnum");
}

@Test
public void descriptionAppearsInEnumJavadoc() {

String javaDocComment = classWithDescription.getComment();

assertThat(javaDocComment, containsString("A description for this enum"));

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* 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.integration;

import com.thoughtworks.qdox.JavaDocBuilder;
import com.thoughtworks.qdox.model.JavaClass;
import org.jsonschema2pojo.integration.util.Jsonschema2PojoRule;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;

/*
Enums are treated differently to schemas of type object and we want to ensure that a title
added to root-level enums is added to the javadoc.
*/
public class TitleEnumIT {
@ClassRule public static Jsonschema2PojoRule classSchemaRule = new Jsonschema2PojoRule();

private static JavaClass classWithTitle;

@BeforeClass
public static void generateClasses() throws IOException {

classSchemaRule.generateAndCompile("/schema/title/titleEnum.json", "com.example");
File generatedJavaFile = classSchemaRule.generated("com/example/TitleEnum.java");

JavaDocBuilder javaDocBuilder = new JavaDocBuilder();
javaDocBuilder.addSource(generatedJavaFile);

classWithTitle = javaDocBuilder.getClassByName("com.example.TitleEnum");
}

@Test
public void descriptionAppearsInEnumJavadoc() {

String javaDocComment = classWithTitle.getComment();

assertThat(javaDocComment, containsString("A title for this enum"));

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"description" : "A description for this enum",
"type":"string",
"enum": ["one", "two"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"title" : "A title for this enum",
"type":"string",
"enum": ["one", "two"]
}

0 comments on commit ecd7b09

Please sign in to comment.