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

Aperta Adatta ai principianti
#4,316 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
84/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
javascript, node.js

Direzione di ricerca

Inizia da src/resolver/specmap/lib/refs.js ed esamina unescapeJsonPointerToken e escapeJsonPointerToken. Leggi la regola di decodifica dei frammenti di RFC 6901 citata nell’issue, poi trova i test del resolver per i riferimenti JSON Pointer e aggiungi casi per i token che contengono +, & e =. Il lavoro è completato quando questi riferimenti vengono risolti nelle chiavi esatte senza errori e i test esistenti dei puntatori continuano a passare.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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/

Lingua principale
JavaScript
Stelle
2.7k
Fork
764
Merge medio
1g 10h
PR unite (30g)
8

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di swagger-api/swagger-client

Tutte le issue di swagger-api/swagger-client

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.