Clear unknown keys from environment variable maps on save

Also fix the previous code for clearing unknown keys to use
a JSON pointer so that it functions correctly when the JSON path
is more than one level deep.

Fixes https://gitlab.com/kicad/code/kicad/-/issues/18734
This commit is contained in:
Jon Evans 2024-11-02 14:53:23 -04:00
parent 6c17f7d706
commit 09652efec5
3 changed files with 9 additions and 4 deletions

View File

@ -120,7 +120,7 @@ COMMON_SETTINGS::COMMON_SETTINGS() :
m_params.emplace_back( new PARAM<int>( "auto_backup.min_interval", m_params.emplace_back( new PARAM<int>( "auto_backup.min_interval",
&m_Backup.min_interval, 300 ) ); &m_Backup.min_interval, 300 ) );
m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "environment.vars", auto envVarsParam = m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "environment.vars",
[&]() -> nlohmann::json [&]() -> nlohmann::json
{ {
nlohmann::json ret = {}; nlohmann::json ret = {};
@ -216,6 +216,7 @@ COMMON_SETTINGS::COMMON_SETTINGS() :
} }
}, },
{} ) ); {} ) );
envVarsParam->SetClearUnknownKeys();
m_params.emplace_back( new PARAM<bool>( "input.focus_follow_sch_pcb", m_params.emplace_back( new PARAM<bool>( "input.focus_follow_sch_pcb",
&m_Input.focus_follow_sch_pcb, false ) ); &m_Input.focus_follow_sch_pcb, false ) );

View File

@ -468,10 +468,12 @@ bool JSON_SETTINGS::SaveToFile( const wxString& aDirectory, bool aForce )
for( PARAM_BASE* param : m_params ) for( PARAM_BASE* param : m_params )
{ {
if( PARAM_WXSTRING_MAP* stringMap = dynamic_cast<PARAM_WXSTRING_MAP*>( param ) ) if( param->ClearUnknownKeys() )
{ {
if( stringMap->ClearUnknownKeys() ) nlohmann::json_pointer p
toSave[ stringMap->GetJsonPath() ] = nlohmann::json( {} ); = JSON_SETTINGS_INTERNALS::PointerFromString( param->GetJsonPath() );
toSave[p] = nlohmann::json( {} );
} }
} }

View File

@ -77,6 +77,8 @@ public:
*/ */
bool ClearUnknownKeys() const { return m_clearUnknownKeys; } bool ClearUnknownKeys() const { return m_clearUnknownKeys; }
void SetClearUnknownKeys( bool aSet = true ) { m_clearUnknownKeys = aSet; }
protected: protected:
std::string m_path; ///< Address of the param in the json files std::string m_path; ///< Address of the param in the json files
bool m_readOnly; ///< Indicates param pointer should never be overwritten bool m_readOnly; ///< Indicates param pointer should never be overwritten