Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public {{>sealed}}class {{classname}} {{#parent}}extends {{{.}}}{{/parent}} {{#v
@JsonCreator
public {{classname}}(
{{#requiredVars}}
@JsonProperty(required = {{required}}, value = "{{baseName}}") {{>beanValidatedType}} {{name}}{{^-last}},{{/-last}}
@JsonProperty(required = {{required}}, value = "{{baseName}}") {{#useJspecify}}{{>nullable_var_annotations}}{{/useJspecify}}{{#lambda.jSpecifyDatatype}}{{>beanValidatedType}}{{/lambda.jSpecifyDatatype}} {{name}}{{^-last}},{{/-last}}
{{/requiredVars}}
) {
{{#parent}}
Expand All @@ -141,7 +141,7 @@ public {{>sealed}}class {{classname}} {{#parent}}extends {{{.}}}{{/parent}} {{#v
@JsonbCreator
public {{classname}}(
{{#requiredVars}}
@JsonbProperty("{{baseName}}") {{>beanValidatedType}} {{name}}{{^-last}},{{/-last}}
@JsonbProperty("{{baseName}}") {{#useJspecify}}{{>nullable_var_annotations}}{{/useJspecify}}{{#lambda.jSpecifyDatatype}}{{>beanValidatedType}}{{/lambda.jSpecifyDatatype}} {{name}}{{^-last}},{{/-last}}
{{/requiredVars}}
) {
{{#parent}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2690,6 +2690,14 @@ public void testUseJspecify() throws Exception {
"public java.math.@Nullable BigDecimal getNumber() {");
assertFileNotContains(model, "public Date getDt() {");

// required properties that are nullable are passed through the @JsonCreator constructor,
// so its parameters need the annotation too, or callers cannot pass null under @NullMarked
final Path requiredAndNullable = Paths.get(output.toPath() + "/src/gen/java/org/openapitools/model/RequiredAndNullable.java");
assertFileContains(requiredAndNullable,
"@JsonProperty(required = true, value = \"str\") @Nullable String str",
"@JsonProperty(required = true, value = \"list\") @Nullable List<String> _list",
"@JsonProperty(required = true, value = \"onlyRequired\") String onlyRequired");

// @NullMarked is what makes the unannotated types mean non-null
assertFileContains(Paths.get(output.toPath() + "/src/gen/java/org/openapitools/model/package-info.java"),
"@org.jspecify.annotations.NullMarked");
Expand Down Expand Up @@ -2728,6 +2736,38 @@ public void testUseJspecifyDisabledByDefault() throws Exception {
assertFileNotContains(Paths.get(output.toPath() + "/pom.xml"), "jspecify");
}

/**
* The JSON-B @JsonbCreator constructor takes the required properties as well, so nullable
* ones must be annotated there just like in the Jackson @JsonCreator constructor.
*/
@Test
public void testUseJspecifyWithJsonbCreator() throws Exception {
Map<String, Object> properties = new HashMap<>();
properties.put(USE_JSPECIFY, true);
properties.put(CodegenConstants.SERIALIZATION_LIBRARY, SERIALIZATION_LIBRARY_JSONB);
properties.put(AbstractJavaJAXRSServerCodegen.USE_JAKARTA_EE, true);
properties.put(INTERFACE_ONLY, true);

File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("jaxrs-spec")
.setAdditionalProperties(properties)
.setValidateSpec(false)
.setInputSpec("src/test/resources/3_0/java/jspecify.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));

new DefaultGenerator().opts(configurator.toClientOptInput()).generate();

final Path requiredAndNullable = Paths.get(output.toPath() + "/src/gen/java/org/openapitools/model/RequiredAndNullable.java");
assertFileContains(requiredAndNullable,
"@JsonbCreator",
"@JsonbProperty(\"str\") @Nullable String str",
"@JsonbProperty(\"list\") @Nullable List<String> _list",
"@JsonbProperty(\"onlyRequired\") String onlyRequired");
}


/**
* jaxrs-cxf-cdi extends this generator but uses its own template directory, which has no
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public RequiredAndNullable() {

@JsonCreator
public RequiredAndNullable(
@JsonProperty(required = true, value = "str") String str,
@JsonProperty(required = true, value = "file") File _file,
@JsonProperty(required = true, value = "color") String color,
@JsonProperty(required = true, value = "str") @Nullable String str,
@JsonProperty(required = true, value = "file") @Nullable File _file,
@JsonProperty(required = true, value = "color") @Nullable String color,
@JsonProperty(required = true, value = "onlyRequired") String onlyRequired,
@JsonProperty(required = true, value = "list") List<String> _list
@JsonProperty(required = true, value = "list") @Nullable List<String> _list
) {
this.str = str;
this._file = _file;
Expand Down
Loading