Skip to content

Commit c4dab24

Browse files
spacethermichaelpro1
authored andcommitted
Fixes issue 5876 (OpenAPITools#5977)
* Fixes getParentName function * Updates getChildrenMap to not throw a NPE * Updates test * Runs ensure up to date
1 parent ea0c9d2 commit c4dab24

File tree

6 files changed

+12
-23
lines changed

6 files changed

+12
-23
lines changed

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,9 +1104,9 @@ public static Header getHeader(OpenAPI openAPI, String name) {
11041104
public static Map<String, List<String>> getChildrenMap(OpenAPI openAPI) {
11051105
Map<String, Schema> allSchemas = getSchemas(openAPI);
11061106

1107-
// FIXME: The collect here will throw NPE if a spec document has only a single oneOf hierarchy.
11081107
Map<String, List<Entry<String, Schema>>> groupedByParent = allSchemas.entrySet().stream()
11091108
.filter(entry -> isComposedSchema(entry.getValue()))
1109+
.filter(entry -> getParentName((ComposedSchema) entry.getValue(), allSchemas)!=null)
11101110
.collect(Collectors.groupingBy(entry -> getParentName((ComposedSchema) entry.getValue(), allSchemas)));
11111111

11121112
return groupedByParent.entrySet().stream()
@@ -1165,14 +1165,6 @@ public static String getParentName(ComposedSchema composedSchema, Map<String, Sc
11651165
int nullSchemaChildrenCount = 0;
11661166
boolean hasAmbiguousParents = false;
11671167
List<String> refedWithoutDiscriminator = new ArrayList<>();
1168-
String schemaName = "";
1169-
for (String thisSchemaName : allSchemas.keySet()) {
1170-
Schema sc = allSchemas.get(thisSchemaName);
1171-
if (isComposedSchema(sc) && (ComposedSchema) sc == composedSchema) {
1172-
schemaName = thisSchemaName;
1173-
break;
1174-
}
1175-
}
11761168

11771169
if (interfaces != null && !interfaces.isEmpty()) {
11781170
for (Schema schema : interfaces) {
@@ -1189,10 +1181,7 @@ public static String getParentName(ComposedSchema composedSchema, Map<String, Sc
11891181
} else {
11901182
// not a parent since discriminator.propertyName is not set
11911183
hasAmbiguousParents = true;
1192-
boolean isNotExtractedInlineSchema = !parentName.equals(schemaName+"_allOf");
1193-
if (isNotExtractedInlineSchema) {
1194-
refedWithoutDiscriminator.add(parentName);
1195-
}
1184+
refedWithoutDiscriminator.add(parentName);
11961185
}
11971186
} else {
11981187
// not a ref, doing nothing, except counting the number of times the 'null' type

modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,8 +612,8 @@ public void testAllOfSingleRefWithOwnPropsNoDiscriminator() {
612612
Schema schema = openAPI.getComponents().getSchemas().get("MessageEventCoreWithTimeListEntries");
613613
codegen.setOpenAPI(openAPI);
614614
CodegenModel model = codegen.fromModel("MessageEventCoreWithTimeListEntries", schema);
615-
Assert.assertEquals(model.parent, "MessageEventCore");
616-
Assert.assertEquals(model.allParents, Collections.singletonList("MessageEventCore"));
615+
Assert.assertEquals(model.parent, null);
616+
Assert.assertEquals(model.allParents, null);
617617
}
618618

619619
@Test

samples/client/petstore/python-experimental/docs/Child.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
## Properties
44
Name | Type | Description | Notes
55
------------ | ------------- | ------------- | -------------
6-
**inter_net** | **bool** | | [optional]
76
**radio_waves** | **bool** | | [optional]
87
**tele_vision** | **bool** | | [optional]
8+
**inter_net** | **bool** | | [optional]
99

1010
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
1111

samples/client/petstore/python-experimental/docs/Parent.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
## Properties
44
Name | Type | Description | Notes
55
------------ | ------------- | ------------- | -------------
6-
**tele_vision** | **bool** | | [optional]
76
**radio_waves** | **bool** | | [optional]
7+
**tele_vision** | **bool** | | [optional]
88

99
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
1010

samples/client/petstore/python-experimental/petstore_api/models/child.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,19 @@ def openapi_types():
8484
and the value is attribute type.
8585
"""
8686
return {
87-
'inter_net': (bool,), # noqa: E501
8887
'radio_waves': (bool,), # noqa: E501
8988
'tele_vision': (bool,), # noqa: E501
89+
'inter_net': (bool,), # noqa: E501
9090
}
9191

9292
@staticmethod
9393
def discriminator():
9494
return None
9595

9696
attribute_map = {
97-
'inter_net': 'interNet', # noqa: E501
9897
'radio_waves': 'radioWaves', # noqa: E501
9998
'tele_vision': 'teleVision', # noqa: E501
99+
'inter_net': 'interNet', # noqa: E501
100100
}
101101

102102
required_properties = set([
@@ -127,9 +127,9 @@ def __init__(self, _check_type=True, _from_server=False, _path_to_item=(), _conf
127127
deserializing a file_type parameter.
128128
If passed, type conversion is attempted
129129
If omitted no type conversion is done.
130-
inter_net (bool): [optional] # noqa: E501
131130
radio_waves (bool): [optional] # noqa: E501
132131
tele_vision (bool): [optional] # noqa: E501
132+
inter_net (bool): [optional] # noqa: E501
133133
"""
134134

135135
self._data_store = {}

samples/client/petstore/python-experimental/petstore_api/models/parent.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,17 @@ def openapi_types():
8484
and the value is attribute type.
8585
"""
8686
return {
87-
'tele_vision': (bool,), # noqa: E501
8887
'radio_waves': (bool,), # noqa: E501
88+
'tele_vision': (bool,), # noqa: E501
8989
}
9090

9191
@staticmethod
9292
def discriminator():
9393
return None
9494

9595
attribute_map = {
96-
'tele_vision': 'teleVision', # noqa: E501
9796
'radio_waves': 'radioWaves', # noqa: E501
97+
'tele_vision': 'teleVision', # noqa: E501
9898
}
9999

100100
required_properties = set([
@@ -125,8 +125,8 @@ def __init__(self, _check_type=True, _from_server=False, _path_to_item=(), _conf
125125
deserializing a file_type parameter.
126126
If passed, type conversion is attempted
127127
If omitted no type conversion is done.
128-
tele_vision (bool): [optional] # noqa: E501
129128
radio_waves (bool): [optional] # noqa: E501
129+
tele_vision (bool): [optional] # noqa: E501
130130
"""
131131

132132
self._data_store = {}

0 commit comments

Comments
 (0)