[Java][jersey2]Support enum discriminator value in child objects (#7267)

* support enum discriminator value in child (java jersey2)

* update samples

* add tests, use public
This commit is contained in:
William Cheng
2020-08-27 11:42:48 +08:00
committed by GitHub
parent f11b2e6772
commit 8c1f6fcdc1
13 changed files with 231 additions and 9 deletions

View File

@@ -2401,6 +2401,12 @@ components:
properties:
name:
type: string
pet_type:
default: ChildCat
enum:
- ChildCat
type: string
x-enum-as-string: true
type: object
securitySchemes:
petstore_auth:

View File

@@ -7,6 +7,15 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**name** | **String** | | [optional]
**petType** | [**String**](#String) | | [optional]
## Enum: String
Name | Value
---- | -----
CHILDCAT | "ChildCat"

View File

@@ -7,6 +7,15 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**name** | **String** | | [optional]
**petType** | [**String**](#String) | | [optional]
## Enum: String
Name | Value
---- | -----
CHILDCAT | "ChildCat"

View File

@@ -32,6 +32,8 @@ import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.openapitools.client.model.ChildCatAllOf;
import org.openapitools.client.model.ParentPet;
import java.util.Set;
import java.util.HashSet;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.openapitools.client.JSON;
@@ -40,7 +42,8 @@ import org.openapitools.client.JSON;
* ChildCat
*/
@JsonPropertyOrder({
ChildCat.JSON_PROPERTY_NAME
ChildCat.JSON_PROPERTY_NAME,
ChildCat.JSON_PROPERTY_PET_TYPE
})
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "pet_type", visible = true)
@@ -49,6 +52,9 @@ public class ChildCat extends ParentPet {
public static final String JSON_PROPERTY_NAME = "name";
private String name;
public static final String JSON_PROPERTY_PET_TYPE = "pet_type";
private String petType = "ChildCat";
public ChildCat name(String name) {
this.name = name;
@@ -73,6 +79,42 @@ public class ChildCat extends ParentPet {
this.name = name;
}
public static final Set<String> PET_TYPE_VALUES = new HashSet<>(Arrays.asList(
"ChildCat"
));
public ChildCat petType(String petType) {
if (!PET_TYPE_VALUES.contains(petType)) {
throw new IllegalArgumentException(petType + " is invalid. Possible values for petType: " + String.join(", ", PET_TYPE_VALUES));
}
this.petType = petType;
return this;
}
/**
* Get petType
* @return petType
**/
@javax.annotation.Nullable
@ApiModelProperty(value = "")
@JsonProperty(JSON_PROPERTY_PET_TYPE)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public String getPetType() {
return petType;
}
public void setPetType(String petType) {
if (!PET_TYPE_VALUES.contains(petType)) {
throw new IllegalArgumentException(petType + " is invalid. Possible values for petType: " + String.join(", ", PET_TYPE_VALUES));
}
this.petType = petType;
}
/**
* A container for additional, undeclared properties.
* This is a holder for any undeclared properties as specified with
@@ -123,14 +165,15 @@ public class ChildCat extends ParentPet {
return false;
}
ChildCat childCat = (ChildCat) o;
return Objects.equals(this.name, childCat.name)&&
return Objects.equals(this.name, childCat.name) &&
Objects.equals(this.petType, childCat.petType)&&
Objects.equals(this.additionalProperties, childCat.additionalProperties) &&
super.equals(o);
}
@Override
public int hashCode() {
return Objects.hash(name, super.hashCode(), additionalProperties);
return Objects.hash(name, petType, super.hashCode(), additionalProperties);
}
@@ -140,6 +183,7 @@ public class ChildCat extends ParentPet {
sb.append("class ChildCat {\n");
sb.append(" ").append(toIndentedString(super.toString())).append("\n");
sb.append(" name: ").append(toIndentedString(name)).append("\n");
sb.append(" petType: ").append(toIndentedString(petType)).append("\n");
sb.append(" additionalProperties: ").append(toIndentedString(additionalProperties)).append("\n");
sb.append("}");
return sb.toString();

View File

@@ -24,6 +24,8 @@ import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.annotation.JsonValue;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.Set;
import java.util.HashSet;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.openapitools.client.JSON;
@@ -32,13 +34,17 @@ import org.openapitools.client.JSON;
* ChildCatAllOf
*/
@JsonPropertyOrder({
ChildCatAllOf.JSON_PROPERTY_NAME
ChildCatAllOf.JSON_PROPERTY_NAME,
ChildCatAllOf.JSON_PROPERTY_PET_TYPE
})
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class ChildCatAllOf {
public static final String JSON_PROPERTY_NAME = "name";
private String name;
public static final String JSON_PROPERTY_PET_TYPE = "pet_type";
private String petType = "ChildCat";
public ChildCatAllOf name(String name) {
this.name = name;
@@ -64,6 +70,42 @@ public class ChildCatAllOf {
}
public static final Set<String> PET_TYPE_VALUES = new HashSet<>(Arrays.asList(
"ChildCat"
));
public ChildCatAllOf petType(String petType) {
if (!PET_TYPE_VALUES.contains(petType)) {
throw new IllegalArgumentException(petType + " is invalid. Possible values for petType: " + String.join(", ", PET_TYPE_VALUES));
}
this.petType = petType;
return this;
}
/**
* Get petType
* @return petType
**/
@javax.annotation.Nullable
@ApiModelProperty(value = "")
@JsonProperty(JSON_PROPERTY_PET_TYPE)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public String getPetType() {
return petType;
}
public void setPetType(String petType) {
if (!PET_TYPE_VALUES.contains(petType)) {
throw new IllegalArgumentException(petType + " is invalid. Possible values for petType: " + String.join(", ", PET_TYPE_VALUES));
}
this.petType = petType;
}
/**
* Return true if this ChildCat_allOf object is equal to o.
*/
@@ -76,12 +118,13 @@ public class ChildCatAllOf {
return false;
}
ChildCatAllOf childCatAllOf = (ChildCatAllOf) o;
return Objects.equals(this.name, childCatAllOf.name);
return Objects.equals(this.name, childCatAllOf.name) &&
Objects.equals(this.petType, childCatAllOf.petType);
}
@Override
public int hashCode() {
return Objects.hash(name);
return Objects.hash(name, petType);
}
@@ -90,6 +133,7 @@ public class ChildCatAllOf {
StringBuilder sb = new StringBuilder();
sb.append("class ChildCatAllOf {\n");
sb.append(" name: ").append(toIndentedString(name)).append("\n");
sb.append(" petType: ").append(toIndentedString(petType)).append("\n");
sb.append("}");
return sb.toString();
}

View File

@@ -66,6 +66,18 @@ public class JSONComposedSchemaTest {
}
}
/**
* Test to ensure the setter will throw IllegalArgumentException
*/
@Test(expected = IllegalArgumentException.class)
public void testEnumDiscriminator() throws Exception {
ChildCat cc = new ChildCat();
cc.setPetType("ChildCat");
assertEquals("ChildCat", cc.getPetType());
cc.setPetType("WrongValue");
}
/**
* Test to ensure the getter will throw ClassCastException
*/