/** * Applies this schema rule to take the not required code generation steps. * <p> * The not required rule adds a Nullable annotation if JSR-305 annotations are desired. * * @param nodeName * the name of the schema node for which this "required" rule has * been added * @param node * the "not required" node, having a value <code>false</code> or * <code>no value</code> * @param generatableType * the class or method which may be marked as "not required" * @return the JavaDoc comment attached to the generatableType, which * <em>may</em> have an added not to mark this construct as * not required. */ @Override public JDocCommentable apply(String nodeName, JsonNode node, JDocCommentable generatableType, Schema schema) { // Since NotRequiredRule is executed for all fields that do not have "required" present, // we need to recognize whether the field is part of the RequiredArrayRule. JsonNode requiredArray = schema.getContent().get("required"); if (requiredArray != null) { for (Iterator<JsonNode> iterator = requiredArray.elements(); iterator.hasNext(); ) { String requiredArrayItem = iterator.next().asText(); if (nodeName.equals(requiredArrayItem)) { return generatableType; } } } if (ruleFactory.getGenerationConfig().isIncludeJsr305Annotations() && generatableType instanceof JFieldVar) { generatableType.javadoc().append(NOT_REQUIRED_COMMENT_TEXT); ((JFieldVar) generatableType).annotate(Nullable.class); } return generatableType; }
private void propertyAnnotations(String nodeName, JsonNode node, Schema schema, JDocCommentable generatedJavaConstruct) { if (node.has("title")) { ruleFactory.getTitleRule().apply(nodeName, node.get("title"), generatedJavaConstruct, schema); } if (node.has("javaName")) { ruleFactory.getJavaNameRule().apply(nodeName, node.get("javaName"), generatedJavaConstruct, schema); } if (node.has("description")) { ruleFactory.getDescriptionRule().apply(nodeName, node.get("description"), generatedJavaConstruct, schema); } if (node.has("required")) { ruleFactory.getRequiredRule().apply(nodeName, node.get("required"), generatedJavaConstruct, schema); } else { ruleFactory.getNotRequiredRule().apply(nodeName, node.get("required"), generatedJavaConstruct, schema); } }
/** * Applies this schema rule to take the required code generation steps. * <p> * The required rule simply adds a note to the JavaDoc comment to mark a * property as required. * * @param nodeName * the name of the schema node for which this "required" rule has * been added * @param node * the "required" node, having a value <code>true</code> or * <code>false</code> * @param generatableType * the class or method which may be marked as "required" * @return the JavaDoc comment attached to the generatableType, which * <em>may</em> have an added not to mark this construct as * required. */ @Override public JDocCommentable apply(String nodeName, JsonNode node, JDocCommentable generatableType, Schema schema) { if (node.asBoolean()) { generatableType.javadoc().append("\n(Required)"); if (ruleFactory.getGenerationConfig().isIncludeJsr303Annotations() && generatableType instanceof JFieldVar) { ((JFieldVar) generatableType).annotate(NotNull.class); } if (ruleFactory.getGenerationConfig().isIncludeJsr305Annotations() && generatableType instanceof JFieldVar) { ((JFieldVar) generatableType).annotate(Nonnull.class); } } else { if (ruleFactory.getGenerationConfig().isIncludeJsr305Annotations() && generatableType instanceof JFieldVar) { ((JFieldVar) generatableType).annotate(Nullable.class); } } return generatableType; }
private void setDeprecatedAnnotationAndJavadoc(Object deprecatedProp, JAnnotatable annotatable, JDocCommentable commentable) { if (Boolean.TRUE.equals(deprecatedProp) && annotatable != null) { annotatable.annotate(Deprecated.class); } else if (deprecatedProp instanceof String) { if (commentable != null) { String deprecatedReason = (String)deprecatedProp; commentable.javadoc().addDeprecated().append(deprecatedReason); } if (annotatable != null) { annotatable.annotate(Deprecated.class); } } }
@Override public JDocComment apply(String nodeName, JsonNode node, JDocCommentable generatableType, Schema currentSchema) { JDocComment javaDoc = generatableType.javadoc(); javaDoc.append(String.format("%nCorresponds to the \"%s\" property.", nodeName)); return javaDoc; }
private void addJavaDoc(JDocCommentable docCommentable) { JDocComment javadoc = docCommentable.javadoc(); javadoc.append(REQUIRED_COMMENT_TEXT); }
public Rule<JDocCommentable, JDocComment> getJavaNameRule() { return new JavaNameRule(); }
/** * Applies this schema rule to take the required code generation steps. * <p> * The required rule simply adds a note to the JavaDoc comment to mark a * property as required. * * @param nodeName * the name of the schema node for which this "required" rule has * been added * @param node * the "required" node, having a value <code>true</code> or * <code>false</code> * @param generatableType * the class or method which may be marked as "required" * @return the JavaDoc comment attached to the generatableType, which * <em>may</em> have an added not to mark this construct as * required. */ @Override public JDocComment apply(String nodeName, JsonNode node, JDocCommentable generatableType, Schema schema) { JDocComment javadoc = generatableType.javadoc(); if (node.asBoolean()) { javadoc.append(REQUIRED_COMMENT_TEXT); if (ruleFactory.getGenerationConfig().isIncludeJsr303Annotations() && generatableType instanceof JFieldVar) { ((JFieldVar) generatableType).annotate(NotNull.class); } } return javadoc; }
/** * Applies this schema rule to take the required code generation steps. * <p> * When a title node is found and applied with this rule, the value of the * title is added as a JavaDoc comment. This rule is typically applied to * the generated field, generated getter and generated setter for the * property. * <p> * Note that the title is always inserted at the top of the JavaDoc comment. * * @param nodeName * the name of the property to which this title applies * @param node * the "title" schema node * @param generatableType * comment-able code generation construct, usually a field or * method, which should have this title applied * @return the JavaDoc comment created to contain the title */ @Override public JDocComment apply(String nodeName, JsonNode node, JDocCommentable generatableType, Schema schema) { JDocComment javadoc = generatableType.javadoc(); javadoc.add(0, node.asText() + "\n<p>\n"); return javadoc; }
/** * Applies this schema rule to take the required code generation steps. * <p> * When a description node is found and applied with this rule, the value of * the description is added as a class level JavaDoc comment. * * @param nodeName * the name of the object to which this description applies * @param node * the "description" schema node * @param generatableType * comment-able code generation construct, usually a java class, * which should have this description applied * @return the JavaDoc comment created to contain the description */ @Override public JDocComment apply(String nodeName, JsonNode node, JDocCommentable generatableType, Schema schema) { JDocComment javadoc = generatableType.javadoc(); javadoc.append(node.asText()); return javadoc; }
@Test public void applyAddsTextWhenRequired() throws JClassAlreadyExistsException { JDefinedClass jclass = new JCodeModel()._class(TARGET_CLASS_NAME); ObjectMapper mapper = new ObjectMapper(); BooleanNode descriptionNode = mapper.createObjectNode().booleanNode(true); JDocCommentable result = rule.apply("fooBar", descriptionNode, jclass, null); assertThat(result.javadoc(), sameInstance(jclass.javadoc())); assertThat(result.javadoc().size(), is(1)); assertThat((String) result.javadoc().get(0), is("\n(Required)")); }
@Test public void applySkipsTextWhenNotRequired() throws JClassAlreadyExistsException { JDefinedClass jclass = new JCodeModel()._class(TARGET_CLASS_NAME); ObjectMapper mapper = new ObjectMapper(); BooleanNode descriptionNode = mapper.createObjectNode().booleanNode(false); JDocCommentable result = rule.apply("fooBar", descriptionNode, jclass, null); assertThat(result.javadoc(), sameInstance(jclass.javadoc())); assertThat(result.javadoc().size(), is(0)); }
/** * Provides a rule instance that should be applied when a "description" * declaration is found in the schema. * * @return a schema rule that can handle the "description" declaration. */ public Rule<JDocCommentable, JDocComment> getDescriptionRule() { return new DescriptionRule(); }
/** * Provides a rule instance that should be applied when a "required" * declaration is found in the schema. * * @return a schema rule that can handle the "required" declaration. */ public Rule<JDocCommentable, JDocCommentable> getRequiredRule() { return new RequiredRule(this); }
/** * Provides a rule instance that should be applied when a "required" * declaration is not found in the schema. * * @return a schema rule that can handle the "required" declaration. */ public Rule<JDocCommentable, JDocCommentable> getNotRequiredRule() { return new NotRequiredRule(this); }
/** * Provides a rule instance that should be applied when a "title" * declaration is found in the schema. * * @return a schema rule that can handle the "title" declaration. */ public Rule<JDocCommentable, JDocComment> getTitleRule() { return new TitleRule(); }
/** * Provides a rule instance that should be applied when a "required" * declaration is found in the schema. * * @return a schema rule that can handle the "required" declaration. */ public Rule<JDocCommentable, JDocComment> getRequiredRule() { return new RequiredRule(this); }