canonical_string refactoring + fix typing in middleware comment