Skip to content

fix(ibge/uf): accept numeric IBGE codes in mapUfToUfCode - #823

Closed
mateussiqueira wants to merge 1 commit into
BrasilAPI:mainfrom
mateussiqueira:fix/ibge-uf-numeric-code
Closed

mateussiqueira wants to merge 1 commit into
BrasilAPI:mainfrom
mateussiqueira:fix/ibge-uf-numeric-code

Conversation

@mateussiqueira

Copy link
Copy Markdown
Contributor

Problema

O endpoint /api/ibge/uf/v1/:code foi projetado para aceitar tanto sigla (PI) quanto código numérico IBGE (22), mas retorna 404 quando um código numérico é passado.

Causa raiz

getUfEstimatePopulationByCode chama mapUfToUfCode internamente, que só aceitava siglas (PI, SC). Quando recebia 22, não encontrava no mapa e lançava NotFoundError$, fazendo o Promise.all` no handler rejeitar e a request inteira retornar 404.

Solução

Adiciona um reverse lookup (CODE_TO_UF) derivado do UF_CODE_MAPPER. Quando a entrada não é encontrada como sigla mas é um valor numérico, faz a busca reversa para resolver a sigla correspondente e retorna o código numérico esperado.

Closes #798

getUfEstimatePopulationByCode calls mapUfToUfCode internally, which only
handled state abbreviations (PI, SC). When a numeric code like '22' was
passed, it threw NotFoundError and caused the entire Promise.all in the
handler to reject with 404.

Add a reverse lookup (CODE_TO_UF) so numeric codes are resolved to their
state abbreviation first, then mapped to the expected numeric code.

Fixes BrasilAPI#798
@vercel

vercel Bot commented Jun 22, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
brasilapi Error Error Jun 22, 2026 5:26pm

Request Review

@vercel

vercel Bot commented Jun 22, 2026

Copy link
Copy Markdown

Deployment failed with the following error:

The `vercel.json` schema validation failed with the following message: should NOT have additional property `public`

Learn More: https://vercel.com/docs/concepts/projects/project-configuration

@sonarqubecloud

Copy link
Copy Markdown

@lucianopf

Copy link
Copy Markdown
Member

Oi @mateussiqueira! Obrigado pela contribuição! 🙏

Esse mesmo bug (IBGE UF por código numérico) tem 3 PRs concorrentes: #799, #820 e #823. Após compararmos, decidimos seguir com o #820, que tem o fix enxuto e cobre com teste unitário dedicado (tests/map-uf-to-uf-code.test.js) + ajuste dos E2E. A abordagem aqui (CODE_TO_UF reverso) funciona, mas ficamos com a solução que já valida o comportamento em teste automatizado.

Fechando este PR em favor do #820. Se discordar, comenta aqui que a gente reavalia! 💪

This branch had an error being deployed

1 failed deployment
Preview — 27b8a72e Deployed Jun 22, 2026 by vercel[bot]
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.

[IBGE UF] Endpoint retorna 404 quando código numérico é usado (ex: /ibge/uf/v1/22)

2 participants