Skip to content

API blocks creation of child region with same name as top-level region, but UI allows it #19302

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

Open
mjoshionemind opened this issue Apr 24, 2025 · 7 comments
Assignees
Labels
severity: low Does not significantly disrupt application functionality, or a workaround is available status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application

Comments

@mjoshionemind
Copy link

Deployment Type

Self-hosted

NetBox Version

v4.2.3

Python Version

3.10

Steps to Reproduce

  1. Go to the NetBox UI and navigate to the Regions section.
    Create a new region:Name: Hong Kong, Slug: HK

Image

  1. After creation, create a child region from the UI with the following details:
    Name: Hong Kong,Slug: HK (or any valid unique slug),Parent: Hong Kong (the region created in step 1)

Image

  1. Observe that this is allowed and saved successfully.
  2. Now, attempt the same operation using the API:
    Make a POST request to /api/dcim/regions/ with this payload:
    {
    "name": "Hong Kong",
    "slug": "hk-kong",
    "parent": <id_of_Hong_Kong_region_created_in _step_1>
    }
    Image

Expected Behavior

The child region with the same name (Hong Kong) but a different slug should be created successfully under the parent Hong Kong, matching the behavior observed in the UI.

Observed Behavior

The API returns a 400 Bad Request with an error indicating that the name must be unique or conflicts with the parent, even though the UI allows it.

@mjoshionemind mjoshionemind added status: needs triage This issue is awaiting triage by a maintainer type: bug A confirmed report of unexpected behavior in the application labels Apr 24, 2025
@jeremystretch
Copy link
Member

@yjain18111 @rraj9921 @yash-pal1 I notice you each gave this a 👍 immediately after it was opened. Would one of you like to own it?

@jnovinger jnovinger added status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation severity: low Does not significantly disrupt application functionality, or a workaround is available and removed status: needs triage This issue is awaiting triage by a maintainer labels Apr 24, 2025
@arthanson arthanson self-assigned this Apr 25, 2025
@arthanson arthanson added status: accepted This issue has been accepted for implementation and removed status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation labels Apr 25, 2025
@arthanson
Copy link
Collaborator

Tried this on NetBox v4.2.8 and it is working fine now, looks like it was fixed, also the error message that was referenced is no longer even in the code. Closing.

@abhi1693
Copy link
Member

abhi1693 commented Apr 25, 2025

@arthanson I check on v4.2.8 as well as develop branches and this issue exist in both. The error is most likely from DRF rather than netbox code. I suspect something with unique constraint is not working as expected. In any case, this issue must be reopened as the last working version for this issue is v3.7.8.

Image

Image

@abhi1693
Copy link
Member

Here's a minimal code to reproduce this issue

from dcim.api.serializers import RegionSerializer
serializer=RegionSerializer(data={"name": "Hong Kong","slug": "hk-kong","parent": 21})
serializer.is_valid() # This returns False
serializer.errors # {'name': [ErrorDetail(string='region with this name already exists.', code='unique')]}

@abhi1693
Copy link
Member

I found the issue is being caused by https://github.com/netbox-community/netbox/blob/main/netbox/dcim/models/sites.py#L49. If you comment this constraint, the API works just as the UI.

@arthanson Can you re-confirm?

@arthanson arthanson reopened this Apr 25, 2025
@arthanson
Copy link
Collaborator

arthanson commented Apr 25, 2025

@abhi1693 that's weird, your example code works fine on mine and I had originally tried this via the REST API and it worked.

>>> from dcim.api.serializers import RegionSerializer
>>> serializer=RegionSerializer(data={"name": "Hong Kong","slug": "hk-kong","parent": 83})
>>> serializer.is_valid()
True
>>> obj = Region.objects.get(id=83)
>>> obj.name, obj.slug
('hong kong', 'HK')

@mjoshionemind
Copy link
Author

mjoshionemind commented Apr 25, 2025

@arthanson It looks like you're creating the child region as "Hong Kong" (capitalized), while the parent is "hong kong" (lowercase). There’s a difference between the two:

  1. Parent region: "Hong Kong"
    Child region: "hong kong"

Image
2. Parent region: "Hong Kong"
Child region: "Hong Kong"

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
severity: low Does not significantly disrupt application functionality, or a workaround is available status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application
Projects
None yet
Development

No branches or pull requests

5 participants