Skip to content

Commit

Permalink
Merge pull request #3100 from swagger-api/ticket-3098
Browse files Browse the repository at this point in the history
fix #3098 - fix null enum item handling
  • Loading branch information
frantuma authored Feb 1, 2019
2 parents 4ca50d4 + d3bfc5e commit cab9b4e
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -285,4 +286,75 @@ public void testDeserializeRefCallback() throws Exception {
OpenAPI oas = Yaml.mapper().readValue(yaml, OpenAPI.class);
assertEquals(oas.getPaths().get("/simplecallback").getGet().getCallbacks().get("testCallback1").get$ref(), "#/components/callbacks/Callback");
}

@Test(description = "Deserialize null enum item")
public void testNullEnumItem() throws Exception {
String yaml = "openapi: 3.0.1\n" +
"paths:\n" +
" /:\n" +
" get:\n" +
" tags:\n" +
" - MyTag\n" +
" summary: Operation Summary\n" +
" description: Operation Description\n" +
" operationId: operationId\n" +
" parameters:\n" +
" - name: subscriptionId\n" +
" in: query\n" +
" schema:\n" +
" type: string\n" +
" responses:\n" +
" default:\n" +
" description: default response\n" +
" content:\n" +
" '*/*': {}\n" +
"components:\n" +
" schemas:\n" +
" UserStatus:\n" +
" type: integer\n" +
" description: some int values with null\n" +
" format: int32\n" +
" enum:\n" +
" - 1\n" +
" - 2\n" +
" - null\n";

OpenAPI oas = Yaml.mapper().readValue(yaml, OpenAPI.class);

assertEquals(oas.getComponents().getSchemas().get("UserStatus").getEnum(), Arrays.asList(1, 2, null));

yaml = "openapi: 3.0.1\n" +
"paths:\n" +
" /:\n" +
" get:\n" +
" tags:\n" +
" - MyTag\n" +
" summary: Operation Summary\n" +
" description: Operation Description\n" +
" operationId: operationId\n" +
" parameters:\n" +
" - name: subscriptionId\n" +
" in: query\n" +
" schema:\n" +
" type: string\n" +
" responses:\n" +
" default:\n" +
" description: default response\n" +
" content:\n" +
" '*/*': {}\n" +
"components:\n" +
" schemas:\n" +
" UserStatus:\n" +
" type: string\n" +
" description: some int values with null\n" +
" enum:\n" +
" - 1\n" +
" - 2\n" +
" - null\n";

oas = Yaml.mapper().readValue(yaml, OpenAPI.class);

assertEquals(oas.getComponents().getSchemas().get("UserStatus").getEnum(), Arrays.asList("1", "2", null));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,14 @@ public String getStringValues() {
public Double getDoubleValues() {
return 1.0;
}

@Schema(description = "some int values", allowableValues = {"1", "2"})
public int getIntAllowableValues() {
return 2;
}

@Schema(description = "some int values with null", allowableValues = {"1", "2", "null"})
public int getIntAllowableValuesWithNull() {
return 2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,12 @@ public void modelWithRangesTest() {
assertEquals(doubleValues.getMaximum(), new BigDecimal("8.0"));
assertEquals(doubleValues.getExclusiveMaximum(), Boolean.TRUE);
assertNull(doubleValues.getExclusiveMinimum());

final IntegerSchema intAllowableValues = (IntegerSchema) properties.get("intAllowableValues");
assertEquals(intAllowableValues.getEnum(), Arrays.asList(1, 2));

final IntegerSchema intAllowableValuesWithNull = (IntegerSchema) properties.get("intAllowableValuesWithNull");
assertEquals(intAllowableValuesWithNull.getEnum(), Arrays.asList(1, 2, null));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.swagger.v3.core.oas.models.Manufacturers;
import io.swagger.v3.core.oas.models.ReadOnlyModel;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.Yaml;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.DateSchema;
Expand Down Expand Up @@ -348,4 +349,22 @@ public void testIssue2064Ip() throws Exception {
assertEquals(ip.getMultipleOf(), new BigDecimal("3.0"));

}

@Test
public void testEnumWithNull() throws Exception {
String yaml =
"type: integer\n" +
"description: some int values with null\n" +
"format: int32\n" +
"enum: \n" +
"- 1\n" +
"- 2\n" +
"- null\n";

final Schema model = Yaml.mapper().readValue(yaml, Schema.class);

assertEquals(model.getEnum(), Arrays.asList(1, 2, null));
SerializationMatchers.assertEqualsToYaml(model, yaml);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,8 @@ public void setPhone(String phone) {
}

@XmlElement(name = "userStatus")
//@Schema(description = "User Status", type = "string", allowableValues = {"1-registered", "2-active", "3-closed"})
@Schema(
description = "User Status",
allowableValues = "1-registered,2-active,3-closed",
extensions = {
@Extension(name = "x-userStatus", properties = {
@ExtensionProperty(name = "name", value = "Josh")}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ public void setPhone(String phone) {
}

@XmlElement(name = "userStatus")
//@Schema(description = "User Status", type = "string", allowableValues = {"1-registered", "2-active", "3-closed"})
@Schema(description = "User Status", allowableValues = "1-registered,2-active,3-closed")
@Schema(description = "User Status") //, allowableValues = {"1","2","3"})
public int getUserStatus() {
return userStatus;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,10 @@ public void setEnum(List<T> _enum) {
}

public void addEnumItemObject(T _enumItem) {
T casted = cast(_enumItem);
if (casted != null) {
if (this._enum == null) {
this._enum = new ArrayList<>();
}
this._enum.add(casted);
if (this._enum == null) {
this._enum = new ArrayList<>();
}
this._enum.add(cast(_enumItem));
}

/**
Expand Down

0 comments on commit cab9b4e

Please sign in to comment.