Skip to content

Commit c773918

Browse files
committed
Fix generic components not accepting specific types
1 parent c692ab6 commit c773918

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

lib/preprocess/parameterproperty/ParameterPropertyHandlerRange.ts

+18-8
Original file line numberDiff line numberDiff line change
@@ -224,15 +224,25 @@ export class ParameterPropertyHandlerRange implements IParameterPropertyHandler
224224
// For the defined generic type instances, apply them into the instance so they can be checked later
225225
value.properties.genericTypeInstances = paramRange.properties.genericTypeInstances
226226
.map(genericTypeInstance => {
227-
if (!genericTypeInstance.property.parameterRangeGenericType) {
228-
throw new ErrorResourcesContext(`Invalid generic type instance in a ParameterRangeGenericComponent was detected: missing parameterRangeGenericType property.`, { parameter: param, genericTypeInstance, value });
227+
// If we have a generic param type reference, instantiate them based on the current generics context
228+
if (genericTypeInstance.isA('ParameterRangeGenericTypeReference')) {
229+
if (!genericTypeInstance.property.parameterRangeGenericType) {
230+
throw new ErrorResourcesContext(`Invalid generic type instance in a ParameterRangeGenericComponent was detected: missing parameterRangeGenericType property.`, {
231+
parameter: param,
232+
genericTypeInstance,
233+
value,
234+
});
235+
}
236+
return this.objectLoader.createCompactedResource({
237+
type: 'ParameterRangeGenericTypeReference',
238+
parameterRangeGenericType: genericTypeInstance.property.parameterRangeGenericType.value,
239+
parameterRangeGenericBindings: genericsContext
240+
.bindings[genericTypeInstance.property.parameterRangeGenericType.value],
241+
});
229242
}
230-
return this.objectLoader.createCompactedResource({
231-
type: 'ParameterRangeGenericTypeReference',
232-
parameterRangeGenericType: genericTypeInstance.property.parameterRangeGenericType.value,
233-
parameterRangeGenericBindings: genericsContext
234-
.bindings[genericTypeInstance.property.parameterRangeGenericType.value],
235-
});
243+
244+
// For all other param types, return the as-is
245+
return genericTypeInstance;
236246
});
237247
}
238248
return this.hasParamValueValidType(value, param, paramRange.property.component, genericsContext);

test/unit/preprocess/parameterproperty/ParameterPropertyHandlerRange-test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,35 @@ describe('ParameterPropertyHandlerRange', () => {
12731273
)).toThrow(/^The value ".*" with types "ex:SomeType1" for parameter ".*" is not of required range type "\(ex:SomeType2\)<ex:GEN_T>"/u);
12741274
});
12751275

1276+
it('should handle a generic component with a direct type value', () => {
1277+
const value = handler.captureType(
1278+
objectLoader.createCompactedResource({
1279+
'@type': [ 'ex:SomeType1' ],
1280+
}),
1281+
objectLoader.createCompactedResource({
1282+
range: {
1283+
'@type': 'ParameterRangeGenericComponent',
1284+
component: 'ex:SomeType1',
1285+
genericTypeInstances: [
1286+
{
1287+
'@id': 'ex:SomeType2',
1288+
},
1289+
],
1290+
},
1291+
}),
1292+
genericsContext,
1293+
);
1294+
expect(value).toBeTruthy();
1295+
expectOutputProperties(value, objectLoader.createCompactedResource({
1296+
'@type': [ 'ex:SomeType1' ],
1297+
genericTypeInstances: [
1298+
{
1299+
'@id': 'ex:SomeType2',
1300+
},
1301+
],
1302+
}));
1303+
});
1304+
12761305
it('should throw on a generic component with config that already has manual generics set', () => {
12771306
genericsContext = new GenericsContext(objectLoader, [
12781307
objectLoader.createCompactedResource('ex:GEN_T'),

0 commit comments

Comments
 (0)