$ref JSON Pointer tokens are decoded with URLSearchParams: "+" becomes a space and "&" cuts the token

Abierto Apto para principiantes
#4,316 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
84/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
javascript, node.js

Línea de trabajo

Empieza en src/resolver/specmap/lib/refs.js e inspecciona unescapeJsonPointerToken y escapeJsonPointerToken. Lee la regla de decodificación de fragmentos de RFC 6901 citada en la issue y, después, busca las pruebas del resolver para referencias JSON Pointer y añade casos para tokens que contengan +, & y =. La tarea estará terminada cuando esas referencias se resuelvan a las claves exactas sin errores y las pruebas existentes de punteros sigan pasando.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Q&A (please complete the following information)
  • OS: Linux (Debian 12)
  • Environment: Node.js v26.13.0
  • Method of installation: npm
  • Swagger-Client version: 3.39.0 (same code on master 841924914d)
  • Swagger/OpenAPI version: OpenAPI 3.0.3 (and Swagger 2.0; the 3.1 resolver is fine)
Content & configuration

Swagger/OpenAPI definition and Swagger-Client usage, as one script:

import Swagger from "swagger-client";

const spec = {
  openapi: "3.0.3",
  info: { title: "t", version: "1" },
  paths: {
    "/a": {
      get: {
        responses: {
          200: {
            description: "ok",
            content: {
              "application/problem+json": {
                schema: { type: "object", properties: { title: { type: "string" } } },
              },
            },
          },
        },
      },
    },
    "/b": {
      get: {
        responses: {
          200: {
            description: "ok",
            content: {
              "application/json": {
                schema: {
                  $ref: "#/paths/~1a/get/responses/200/content/application~1problem+json/schema",
                },
              },
            },
          },
        },
      },
    },
  },
};

const r = await Swagger.resolve({ spec });
console.log(r.errors.map((e) => e.message));

for (const [k, alt] of [["x+y", "x y"], ["a&b", "a"], ["q=1&r=2", "q=1"]]) {
  const res = await Swagger.resolve({
    spec: { a: { [k]: { v: "right" }, [alt]: { v: "WRONG" } }, b: { $ref: "#/a/" + k } },
  });
  console.log(k, res.spec.b.v, res.errors.length);
}
Describe the bug you're encountering

The specmap resolver decodes JSON Pointer tokens with URLSearchParams, which applies form decoding. A $ref to an application/problem+json media type cannot be resolved, and keys containing +, & or = resolve to a different key.

To reproduce...

Steps to reproduce the behavior:

  1. npm i [email protected]
  2. Run the script above with node repro.mjs

Output:

[
  'Could not resolve reference: #/paths/~1a/get/responses/200/content/application~1problem+json/schema'
]
x+y WRONG 0
a&b WRONG 0
q=1&r=2 WRONG 0
Expected behavior

No errors, and x+y right 0, a&b right 0, q=1&r=2 right 0. In a URI fragment, +, & and = are literal and tokens are only percent-decoded (https://datatracker.ietf.org/doc/html/rfc6901#section-6).

Screenshots

Not applicable.

Additional context or thoughts

unescapeJsonPointerToken / escapeJsonPointerToken in src/resolver/specmap/lib/refs.js use new URLSearchParams(...), which also unescapes ~1/~0 before percent-decoding. decodeURIComponent followed by the ~1/~0 replacement (and the reverse for escaping) would fix it. The current escape and unescape round-trip with each other, so the existing tests do not catch this.

Credit: https://ucsbplse.github.io/

Lenguaje dominante
JavaScript
Estrellas
2.7k
Forks
764
Merge medio
1 d 10 h
PR fusionados (30 d)
8

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de swagger-api/swagger-client

Todos los issues de swagger-api/swagger-client

Issues similares

Más issues de JavaScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.