Skip to content

luci-base: reimplement the add method of CBIJSONConfig#8347

Open
emzet wants to merge 1 commit intoopenwrt:masterfrom
emzet:bugfix/luci-base-form
Open

luci-base: reimplement the add method of CBIJSONConfig#8347
emzet wants to merge 1 commit intoopenwrt:masterfrom
emzet:bugfix/luci-base-form

Conversation

@emzet
Copy link
Copy Markdown

@emzet emzet commented Feb 24, 2026

This PR fixes a bug in the calculation of section_id in the add method of CBIJSONConfig when using nested sections (subsections).

When using the JSONMap, data are retrieved via a JSON file or a custom JS object. Internally, the object is converted into a UCI-like format. Its top-level keys are treated as UCI section types, while the object or array-of-object values are treated as section content.

const data = {
  "first_section": [
    {
      "title": "my first section first item"
    },
    {
      "title": "my first section second item"
    },
  ],
  "second_section2": [
    {
      "title": "my second section first item"
    }
  ]
};

Section types are stored in the main data object with key:value pairs (just plain JS object). Keys are generated during init method and stored in a flat structure. Indexing increments globally, regardless of the section type.

{
  "first_section0": {
    "title": "my first section first item",
    ".name": "first_section0",
    ".type": "first_section",
    ".anonymous": true,
    ".index": 0
  },
  "first_section1": {
    "title": "my first section second item",
    ".name": "first_section1",
    ".type": "first_section",
    ".anonymous": true,
    ".index": 1
  },
  "second_section2": {
    "title": "my second section first item",
    ".name": "second_section2",
    ".type": "second_section",
    ".anonymous": true,
    ".index": 2
  }
}

The problem in the original code occurred when using nested sections, e.g., a GridSection inside another GridSection. In this case, adding a new section (via the UI "add" button) would compute the wrong section_id, causing it to always return the last section instead of creating a new blank one.

const formMap = new form.JSONMap(data);
const section = formMap.section(form.GridSection, 'first_section');

section.tab('tab1', _('Tab 1'));
section.tab('tab2', _('Tab 2'));

option = section.taboption('tab1', form.Value, 'title', _('Title in parent Grid'));
option = section.taboption('tab2', form.SectionValue, '_title', form.GridSection, 'second_section');

const subsection = option.subsection;

option = subsection.option(form.Value, 'title', _('Title in nested Grid'));

Changes in this PR fixes this problem:

  • renamed next_index to max_index to make its purpose clearer
  • next_index is now computed by incrementing the max_index found in data, regardless of section type
  • removed num_sections_type as it is no longer used
  • the next_index value is now used both for creating the section_id and as the value of .index, ensuring uniqueness

@emzet emzet force-pushed the bugfix/luci-base-form branch from a14a31b to d1704cb Compare February 24, 2026 00:56
@emzet emzet changed the title fix: add method of CBIJSONConfig fix: reimplement the add method of CBIJSONConfig Feb 24, 2026
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@emzet emzet force-pushed the bugfix/luci-base-form branch from d1704cb to fa3977b Compare February 24, 2026 21:41
@emzet emzet changed the title fix: reimplement the add method of CBIJSONConfig luci-base: reimplement the add method of CBIJSONConfig Feb 24, 2026
@github-actions

This comment has been minimized.

fix the calculation of section_id in the add method of CBIJSONConfig

Signed-off-by: Marek Zavacky <zavacky.m@gmail.com>
@emzet emzet force-pushed the bugfix/luci-base-form branch from fa3977b to c94fb37 Compare February 27, 2026 01:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant