Skip to content

Commit 7e4fa70

Browse files
authored
Update allOf to not always be nullable (#12861)
* added nonNullableVars * added return property to operation * added return property to operation * build samples * added inner enum * build samples * allOf no longer always nullable * added a comment
1 parent 17f9a28 commit 7e4fa70

File tree

12 files changed

+33
-86
lines changed

12 files changed

+33
-86
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3506,7 +3506,10 @@ protected void updatePropertyForAnyType(CodegenProperty property, Schema p) {
35063506
if (Boolean.FALSE.equals(p.getNullable())) {
35073507
LOGGER.warn("Schema '{}' is any type, which includes the 'null' value. 'nullable' cannot be set to 'false'", p.getName());
35083508
}
3509-
property.isNullable = true;
3509+
ComposedSchema composedSchema = p instanceof ComposedSchema
3510+
? (ComposedSchema) p
3511+
: null;
3512+
property.isNullable = property.isNullable || composedSchema == null || composedSchema.getAllOf() == null || composedSchema.getAllOf().size() == 0;
35103513
if (languageSpecificPrimitives.contains(property.dataType)) {
35113514
property.isPrimitiveType = true;
35123515
}

samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Model/AllOfWithSingleRef.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public partial class AllOfWithSingleRef : IEquatable<AllOfWithSingleRef>, IVali
3333
/// <summary>
3434
/// Gets or Sets SingleRefType
3535
/// </summary>
36-
[DataMember(Name="SingleRefType", EmitDefaultValue=true)]
36+
[DataMember(Name="SingleRefType", EmitDefaultValue=false)]
3737
public SingleRefType? SingleRefType { get; set; }
3838
/// <summary>
3939
/// Initializes a new instance of the <see cref="AllOfWithSingleRef" /> class.
@@ -42,7 +42,6 @@ public partial class AllOfWithSingleRef : IEquatable<AllOfWithSingleRef>, IVali
4242
/// <param name="singleRefType">singleRefType.</param>
4343
public AllOfWithSingleRef(string username = default(string), SingleRefType? singleRefType = default(SingleRefType?))
4444
{
45-
this.SingleRefType = singleRefType;
4645
this.Username = username;
4746
this.SingleRefType = singleRefType;
4847
}

samples/client/petstore/java/feign/src/main/java/org/openapitools/client/model/AllOfWithSingleRef.java

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@
2323
import io.swagger.annotations.ApiModel;
2424
import io.swagger.annotations.ApiModelProperty;
2525
import org.openapitools.client.model.SingleRefType;
26-
import org.openapitools.jackson.nullable.JsonNullable;
27-
import com.fasterxml.jackson.annotation.JsonIgnore;
28-
import org.openapitools.jackson.nullable.JsonNullable;
29-
import java.util.NoSuchElementException;
3026
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
3127
import com.fasterxml.jackson.annotation.JsonTypeName;
3228

@@ -43,7 +39,7 @@ public class AllOfWithSingleRef {
4339
private String username;
4440

4541
public static final String JSON_PROPERTY_SINGLE_REF_TYPE = "SingleRefType";
46-
private JsonNullable<SingleRefType> singleRefType = JsonNullable.<SingleRefType>undefined();
42+
private SingleRefType singleRefType;
4743

4844
public AllOfWithSingleRef() {
4945
}
@@ -76,8 +72,8 @@ public void setUsername(String username) {
7672

7773

7874
public AllOfWithSingleRef singleRefType(SingleRefType singleRefType) {
79-
this.singleRefType = JsonNullable.<SingleRefType>of(singleRefType);
8075

76+
this.singleRefType = singleRefType;
8177
return this;
8278
}
8379

@@ -87,26 +83,18 @@ public AllOfWithSingleRef singleRefType(SingleRefType singleRefType) {
8783
**/
8884
@javax.annotation.Nullable
8985
@ApiModelProperty(value = "")
90-
@JsonIgnore
91-
92-
public SingleRefType getSingleRefType() {
93-
return singleRefType.orElse(null);
94-
}
95-
9686
@JsonProperty(JSON_PROPERTY_SINGLE_REF_TYPE)
9787
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
9888

99-
public JsonNullable<SingleRefType> getSingleRefType_JsonNullable() {
89+
public SingleRefType getSingleRefType() {
10090
return singleRefType;
10191
}
102-
103-
@JsonProperty(JSON_PROPERTY_SINGLE_REF_TYPE)
104-
public void setSingleRefType_JsonNullable(JsonNullable<SingleRefType> singleRefType) {
105-
this.singleRefType = singleRefType;
106-
}
10792

93+
94+
@JsonProperty(JSON_PROPERTY_SINGLE_REF_TYPE)
95+
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
10896
public void setSingleRefType(SingleRefType singleRefType) {
109-
this.singleRefType = JsonNullable.<SingleRefType>of(singleRefType);
97+
this.singleRefType = singleRefType;
11098
}
11199

112100

@@ -120,23 +108,12 @@ public boolean equals(Object o) {
120108
}
121109
AllOfWithSingleRef allOfWithSingleRef = (AllOfWithSingleRef) o;
122110
return Objects.equals(this.username, allOfWithSingleRef.username) &&
123-
equalsNullable(this.singleRefType, allOfWithSingleRef.singleRefType);
124-
}
125-
126-
private static <T> boolean equalsNullable(JsonNullable<T> a, JsonNullable<T> b) {
127-
return a == b || (a != null && b != null && a.isPresent() && b.isPresent() && Objects.deepEquals(a.get(), b.get()));
111+
Objects.equals(this.singleRefType, allOfWithSingleRef.singleRefType);
128112
}
129113

130114
@Override
131115
public int hashCode() {
132-
return Objects.hash(username, hashCodeNullable(singleRefType));
133-
}
134-
135-
private static <T> int hashCodeNullable(JsonNullable<T> a) {
136-
if (a == null) {
137-
return 1;
138-
}
139-
return a.isPresent() ? Arrays.deepHashCode(new Object[]{a.get()}) : 31;
116+
return Objects.hash(username, singleRefType);
140117
}
141118

142119
@Override

samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/model/AllOfWithSingleRef.java

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@
2323
import io.swagger.annotations.ApiModel;
2424
import io.swagger.annotations.ApiModelProperty;
2525
import org.openapitools.client.model.SingleRefType;
26-
import org.openapitools.jackson.nullable.JsonNullable;
27-
import com.fasterxml.jackson.annotation.JsonIgnore;
28-
import org.openapitools.jackson.nullable.JsonNullable;
29-
import java.util.NoSuchElementException;
3026
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
3127
import com.fasterxml.jackson.annotation.JsonTypeName;
3228

@@ -43,7 +39,7 @@ public class AllOfWithSingleRef {
4339
private String username;
4440

4541
public static final String JSON_PROPERTY_SINGLE_REF_TYPE = "SingleRefType";
46-
private JsonNullable<SingleRefType> singleRefType = JsonNullable.<SingleRefType>undefined();
42+
private SingleRefType singleRefType;
4743

4844
public AllOfWithSingleRef() {
4945
}
@@ -76,8 +72,8 @@ public void setUsername(String username) {
7672

7773

7874
public AllOfWithSingleRef singleRefType(SingleRefType singleRefType) {
79-
this.singleRefType = JsonNullable.<SingleRefType>of(singleRefType);
8075

76+
this.singleRefType = singleRefType;
8177
return this;
8278
}
8379

@@ -87,26 +83,18 @@ public AllOfWithSingleRef singleRefType(SingleRefType singleRefType) {
8783
**/
8884
@javax.annotation.Nullable
8985
@ApiModelProperty(value = "")
90-
@JsonIgnore
91-
92-
public SingleRefType getSingleRefType() {
93-
return singleRefType.orElse(null);
94-
}
95-
9686
@JsonProperty(JSON_PROPERTY_SINGLE_REF_TYPE)
9787
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
9888

99-
public JsonNullable<SingleRefType> getSingleRefType_JsonNullable() {
89+
public SingleRefType getSingleRefType() {
10090
return singleRefType;
10191
}
102-
103-
@JsonProperty(JSON_PROPERTY_SINGLE_REF_TYPE)
104-
public void setSingleRefType_JsonNullable(JsonNullable<SingleRefType> singleRefType) {
105-
this.singleRefType = singleRefType;
106-
}
10792

93+
94+
@JsonProperty(JSON_PROPERTY_SINGLE_REF_TYPE)
95+
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
10896
public void setSingleRefType(SingleRefType singleRefType) {
109-
this.singleRefType = JsonNullable.<SingleRefType>of(singleRefType);
97+
this.singleRefType = singleRefType;
11098
}
11199

112100

@@ -120,23 +108,12 @@ public boolean equals(Object o) {
120108
}
121109
AllOfWithSingleRef allOfWithSingleRef = (AllOfWithSingleRef) o;
122110
return Objects.equals(this.username, allOfWithSingleRef.username) &&
123-
equalsNullable(this.singleRefType, allOfWithSingleRef.singleRefType);
124-
}
125-
126-
private static <T> boolean equalsNullable(JsonNullable<T> a, JsonNullable<T> b) {
127-
return a == b || (a != null && b != null && a.isPresent() && b.isPresent() && Objects.deepEquals(a.get(), b.get()));
111+
Objects.equals(this.singleRefType, allOfWithSingleRef.singleRefType);
128112
}
129113

130114
@Override
131115
public int hashCode() {
132-
return Objects.hash(username, hashCodeNullable(singleRefType));
133-
}
134-
135-
private static <T> int hashCodeNullable(JsonNullable<T> a) {
136-
if (a == null) {
137-
return 1;
138-
}
139-
return a.isPresent() ? Arrays.deepHashCode(new Object[]{a.get()}) : 31;
116+
return Objects.hash(username, singleRefType);
140117
}
141118

142119
@Override

samples/client/petstore/php/OpenAPIClient-php/lib/Model/AllOfWithSingleRef.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class AllOfWithSingleRef implements ModelInterface, ArrayAccess, \JsonSerializab
8080
*/
8181
protected static array $openAPINullables = [
8282
'username' => false,
83-
'single_ref_type' => true
83+
'single_ref_type' => false
8484
];
8585

8686
/**
@@ -336,14 +336,7 @@ public function setSingleRefType($single_ref_type)
336336
{
337337

338338
if (is_null($single_ref_type)) {
339-
array_push($this->openAPINullablesSetToNull, 'single_ref_type');
340-
} else {
341-
$nullablesSetToNull = $this->getOpenAPINullablesSetToNull();
342-
$index = array_search('single_ref_type', $nullablesSetToNull);
343-
if ($index !== FALSE) {
344-
unset($nullablesSetToNull[$index]);
345-
$this->setOpenAPINullablesSetToNull($nullablesSetToNull);
346-
}
339+
throw new \InvalidArgumentException('non-nullable single_ref_type cannot be null');
347340
}
348341

349342
$this->container['single_ref_type'] = $single_ref_type;

samples/client/petstore/ruby-faraday/lib/petstore/models/all_of_with_single_ref.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ def self.openapi_types
4343
# List of attributes with nullable: true
4444
def self.openapi_nullable
4545
Set.new([
46-
:'single_ref_type'
4746
])
4847
end
4948

samples/client/petstore/ruby/lib/petstore/models/all_of_with_single_ref.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ def self.openapi_types
4343
# List of attributes with nullable: true
4444
def self.openapi_nullable
4545
Set.new([
46-
:'single_ref_type'
4746
])
4847
end
4948

samples/client/petstore/typescript-fetch/builds/default-v3.0/models/AllOfWithSingleRef.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export interface AllOfWithSingleRef {
3737
* @type {SingleRefType}
3838
* @memberof AllOfWithSingleRef
3939
*/
40-
singleRefType?: SingleRefType | null;
40+
singleRefType?: SingleRefType;
4141
}
4242

4343
/**

samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/all_of_with_single_ref.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class AllOfWithSingleRef {
5555
@override
5656
int get hashCode =>
5757
username.hashCode +
58-
(singleRefType == null ? 0 : singleRefType.hashCode);
58+
singleRefType.hashCode;
5959

6060
factory AllOfWithSingleRef.fromJson(Map<String, dynamic> json) => _$AllOfWithSingleRefFromJson(json);
6161

samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/model/all_of_with_single_ref.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class _$AllOfWithSingleRefSerializer implements StructuredSerializer<AllOfWithSi
5252
result
5353
..add(r'SingleRefType')
5454
..add(serializers.serialize(object.singleRefType,
55-
specifiedType: const FullType.nullable(SingleRefType)));
55+
specifiedType: const FullType(SingleRefType)));
5656
}
5757
return result;
5858
}
@@ -76,8 +76,7 @@ class _$AllOfWithSingleRefSerializer implements StructuredSerializer<AllOfWithSi
7676
break;
7777
case r'SingleRefType':
7878
final valueDes = serializers.deserialize(value,
79-
specifiedType: const FullType.nullable(SingleRefType)) as SingleRefType?;
80-
if (valueDes == null) continue;
79+
specifiedType: const FullType(SingleRefType)) as SingleRefType;
8180
result.singleRefType = valueDes;
8281
break;
8382
}

0 commit comments

Comments
 (0)