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

Fix Schema annotation with nullable and "null" example or defaultValue #4344

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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 @@ -2191,6 +2191,10 @@ protected void resolveSchemaMembers(Schema schema, Annotated a, Annotation[] ann
if (StringUtils.isNotBlank(format) && StringUtils.isBlank(schema.getFormat())) {
schema.format(format);
}
Boolean nullable = resolveNullable(a, annotations, schemaAnnotation);
if (nullable != null) {
schema.nullable(nullable);
}
Object defaultValue = resolveDefaultValue(a, annotations, schemaAnnotation);
if (defaultValue != null) {
schema.setDefault(defaultValue);
Expand All @@ -2203,10 +2207,6 @@ protected void resolveSchemaMembers(Schema schema, Annotated a, Annotation[] ann
if (readOnly != null) {
schema.readOnly(readOnly);
}
Boolean nullable = resolveNullable(a, annotations, schemaAnnotation);
if (nullable != null) {
schema.nullable(nullable);
}
BigDecimal multipleOf = resolveMultipleOf(a, annotations, schemaAnnotation);
if (multipleOf != null) {
schema.multipleOf(multipleOf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ public void serialize(

if (StringUtils.isBlank(value.get$ref())) {

if (value.getExampleSetFlag() && value.getExample() == null) {
if (value.getExampleSetFlag() && value.getExample() == null || value.getDefaultSetFlag() && value.getDefault() == null) {
jgen.writeStartObject();
defaultSerializer.unwrappingSerializer(null).serialize(value, jgen, provider);
jgen.writeNullField("example");
if (value.getExampleSetFlag() && value.getExample() == null) {
jgen.writeNullField("example");
}
if (value.getDefaultSetFlag() && value.getDefault() == null) {
jgen.writeNullField("default");
}
jgen.writeEndObject();
} else {
defaultSerializer.serialize(value, jgen, provider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public abstract class Schema31Mixin {
@JsonIgnore
public abstract boolean getExampleSetFlag();

@JsonIgnore
public abstract boolean getDefaultSetFlag();

@JsonInclude(value = JsonInclude.Include.NON_NULL, content = JsonInclude.Include.ALWAYS)
public abstract Object getExample();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public abstract class SchemaConverterMixin {
@JsonIgnore
public abstract boolean getExampleSetFlag();

@JsonIgnore
public abstract boolean getDefaultSetFlag();

@JsonInclude(value = JsonInclude.Include.NON_NULL, content = JsonInclude.Include.ALWAYS)
public abstract Object getExample();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public abstract class SchemaMixin {
@JsonIgnore
public abstract boolean getExampleSetFlag();

@JsonIgnore
public abstract boolean getDefaultSetFlag();

@JsonInclude(value = JsonInclude.Include.NON_NULL, content = JsonInclude.Include.ALWAYS)
public abstract Object getExample();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,9 @@ public static Optional<Schema> getSchemaFromAnnotation(io.swagger.v3.oas.annotat
if (StringUtils.isNotBlank(schema.type())) {
schemaObject.setType(schema.type());
}
if (schema.nullable()) {
schemaObject.setNullable(schema.nullable());
}
if (StringUtils.isNotBlank(schema.defaultValue())) {
schemaObject.setDefault(schema.defaultValue());
}
Expand Down Expand Up @@ -487,9 +490,6 @@ public static Optional<Schema> getSchemaFromAnnotation(io.swagger.v3.oas.annotat
String filteredMinimum = schema.minimum().replace(Constants.COMMA, StringUtils.EMPTY);
schemaObject.setMinimum(new BigDecimal(filteredMinimum));
}
if (schema.nullable()) {
schemaObject.setNullable(schema.nullable());
}
if (StringUtils.isNotBlank(schema.title())) {
schemaObject.setTitle(schema.title());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.swagger.v3.core.resolving;

import io.swagger.v3.core.converter.ModelConverters;
import io.swagger.v3.core.matchers.SerializationMatchers;
import io.swagger.v3.core.resolving.resources.TestObjectTicket4339;
import org.testng.annotations.Test;

public class Ticket4339Test {
@Test
public void testNullableWithNull() {
SerializationMatchers.assertEqualsToYaml(ModelConverters.getInstance().readAll(TestObjectTicket4339.class), "Address:\n" +
" type: object\n" +
" properties:\n" +
" streetNumber:\n" +
" type: integer\n" +
" format: int32\n" +
" nullable: true\n" +
" example: null\n" +
" default: null\n" +
"TestObjectTicket4339:\n" +
" type: object\n" +
" properties:\n" +
" string:\n" +
" type: string\n" +
" nullable: true\n" +
" example: null\n" +
" default: null\n" +
" integer:\n" +
" type: integer\n" +
" format: int32\n" +
" nullable: true\n" +
" example: null\n" +
" default: null\n" +
" number:\n" +
" type: number\n" +
" nullable: true\n" +
" example: null\n" +
" default: null\n" +
" aBoolean:\n" +
" type: boolean\n" +
" nullable: true\n" +
" example: null\n" +
" default: null\n" +
" address:\n" +
" $ref: '#/components/schemas/Address'\n" +
" testObjectTicket4339List:\n" +
" type: array\n" +
" nullable: true\n" +
" items:\n" +
" $ref: '#/components/schemas/TestObjectTicket4339'\n" +
" example: null\n" +
" default: null\n");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.swagger.v3.core.resolving.resources;

import io.swagger.v3.core.oas.models.Address;
import io.swagger.v3.oas.annotations.media.Schema;

import java.math.BigDecimal;
import java.util.List;

public class TestObjectTicket4339 {
// type: string
@Schema(nullable = true, example = "null", defaultValue = "null")
public String string;

// type: integer
@Schema(nullable = true, example = "null", defaultValue = "null")
public Integer integer;

// type: number
@Schema(nullable = true, example = "null", defaultValue = "null")
public BigDecimal number;

// type: boolean
@Schema(nullable = true, example = "null", defaultValue = "null")
public Boolean aBoolean;

// type: object
@Schema(nullable = true, example = "null", defaultValue = "null")
public Address address;

// type: array
@Schema(nullable = true, example = "null", defaultValue = "null")
public List<TestObjectTicket4339> testObjectTicket4339List;
}
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,9 @@ static abstract class SortedSchemaMixin {
@JsonIgnore
public abstract boolean getExampleSetFlag();

@JsonIgnore
public abstract boolean getDefaultSetFlag();

@JsonInclude(value = JsonInclude.Include.NON_NULL, content = JsonInclude.Include.ALWAYS)
public abstract Object getExample();

Expand Down Expand Up @@ -753,6 +756,9 @@ static abstract class SortedSchemaMixin31 {
@JsonIgnore
public abstract boolean getExampleSetFlag();

@JsonIgnore
public abstract boolean getDefaultSetFlag();

@JsonInclude(value = JsonInclude.Include.NON_NULL, content = JsonInclude.Include.ALWAYS)
public abstract Object getExample();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ public static abstract class SortedSchemaMixin {
@JsonIgnore
public abstract boolean getExampleSetFlag();

@JsonIgnore
public abstract boolean getDefaultSetFlag();

@JsonInclude(value = JsonInclude.Include.NON_NULL, content = JsonInclude.Include.ALWAYS)
public abstract Object getExample();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public class Schema<T> {
private Discriminator discriminator = null;

private boolean exampleSetFlag;
private boolean defaultSetFlag;

/**
* @since 2.2.0 (OpenAPI 3.1.0)
Expand Down Expand Up @@ -778,7 +779,15 @@ public T getDefault() {
}

public void setDefault(Object _default) {
this._default = cast(_default);
if (SpecVersion.V30.equals(specVersion) && Boolean.TRUE.equals(nullable) && (_default == null || "null".equals(_default.toString()))) {
this._default = null;
defaultSetFlag = true;
} else {
this._default = cast(_default);
if (!(_default != null && this._default == null)) {
defaultSetFlag = true;
}
}
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -1341,9 +1350,14 @@ public Object getExample() {
}

public void setExample(Object example) {
this.example = cast(example);
if (!(example != null && this.example == null)) {
if (SpecVersion.V30.equals(specVersion) && Boolean.TRUE.equals(nullable) && (example == null || "null".equals(example.toString()))) {
this.example = null;
exampleSetFlag = true;
} else {
this.example = cast(example);
if (!(example != null && this.example == null)) {
exampleSetFlag = true;
}
}
}

Expand Down Expand Up @@ -1425,7 +1439,20 @@ public void setExampleSetFlag(boolean exampleSetFlag) {
}

/**
* returns true if default setter has been invoked
* Used to flag explicit setting to null of default (vs missing field) while deserializing from json/yaml string
*
* @return boolean defaultSetFlag
**/
public boolean getDefaultSetFlag() {
return defaultSetFlag;
}

public void setDefaultSetFlag(boolean defaultSetFlag) {
this.defaultSetFlag = defaultSetFlag;
}

/**
* @since 2.2.0 (OpenAPI 3.1.0)
*/
@OpenAPI31
Expand Down Expand Up @@ -2100,7 +2127,7 @@ protected String toIndentedString(java.lang.Object o) {
}

public Schema _default(T _default) {
this._default = _default;
setDefault(_default);
return this;
}

Expand Down