Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix handling of URI variables #82

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions lib/src/core/content_serdes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,19 @@ class ContentSerdes {
}

void _validateValue(Object? value, DataSchema? dataSchema) {
final dataSchemaJson = dataSchema?.rawJson;
if (dataSchemaJson == null) {
// TODO(JKRhb): The process of validating values according to a dataschema
// needs to be reworked.
const filteredKeys = ['uriVariables'];

final filteredDataSchemaJson = dataSchema?.rawJson?.entries
.where((element) => !filteredKeys.contains(element.key));
if (filteredDataSchemaJson == null) {
return;
}
final schema =
JsonSchema.create(dataSchemaJson, schemaVersion: SchemaVersion.draft7);
final schema = JsonSchema.create(
Map.fromEntries(filteredDataSchemaJson),
schemaVersion: SchemaVersion.draft7,
);
if (!schema.validate(value).isValid) {
throw ContentSerdesException('JSON Schema validation failed.');
}
Expand Down
27 changes: 8 additions & 19 deletions lib/src/definitions/form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -318,34 +318,23 @@ class Form {
return null;
}

if (affordanceUriVariables.isEmpty) {
throw UriVariableException(
'The Form href $href contains URI '
'variables but the TD does not provide a uriVariables definition.',
if (uriVariables != null) {
// Perform additional validation
_validateUriVariables(
hrefUriVariables,
affordanceUriVariables,
uriVariables,
);
}

if (uriVariables == null) {
throw ValidationException(
'The Form href $href contains URI variables '
'but no values were provided as InteractionOptions.',
);
}

// Perform additional validation
_validateUriVariables(
hrefUriVariables,
affordanceUriVariables,
uriVariables,
);

// As "{" and "}" are "percent encoded" due to Uri.parse(), we need to
// revert the encoding first before we can insert the values.
final decodedHref = Uri.decodeFull(href.toString());

// Everything should be okay at this point, we can simply insert the values
// and return the result.
final newHref = Uri.parse(UriTemplate(decodedHref).expand(uriVariables));
final newHref =
Uri.parse(UriTemplate(decodedHref).expand(uriVariables ?? {}));
return _copy(newHref);
}
}
Expand Down