Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/JsonSchemaGen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as Option from "effect/Option"
import * as Layer from "effect/Layer"
import * as Arr from "effect/Array"
import { pipe } from "effect/Function"
import { identifier, nonEmptyString, toComment } from "./Utils"
import { identifier, nonEmptyString, toComment, decodeRefTokens, refLastToken } from "./Utils"
import * as Struct from "effect/Struct"

const make = Effect.gen(function* () {
Expand Down Expand Up @@ -135,7 +135,7 @@ const make = Effect.gen(function* () {

if ("$ref" in root) {
addRefs(root, undefined, false)
return identifier(root.$ref.split("/").pop()!)
return identifier(refLastToken(root.$ref))
} else {
addRefs(root, "properties" in root ? name : undefined)
store.set(name, root)
Expand Down Expand Up @@ -278,7 +278,7 @@ const make = Effect.gen(function* () {
if (!schema.$ref.startsWith("#")) {
return Option.none()
}
const name = identifier(schema.$ref.split("/").pop()!)
const name = identifier(refLastToken(schema.$ref))
return Option.some(transformer.onRef({ importName, name }))
} else if ("properties" in schema) {
return toSource(
Expand Down Expand Up @@ -789,7 +789,7 @@ function resolveRef(
if (!schema.$ref.startsWith("#")) {
return
}
const path = schema.$ref.slice(2).split("/")
const path = decodeRefTokens(schema.$ref)
const name = identifier(path[path.length - 1])

let current = context
Expand Down
4 changes: 2 additions & 2 deletions src/OpenApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as Layer from "effect/Layer"
import * as JsonSchemaGen from "./JsonSchemaGen.js"
import type * as JsonSchema from "@effect/platform/OpenApiJsonSchema"
import type { DeepMutable } from "effect/Types"
import { camelize, identifier, nonEmptyString, toComment } from "./Utils.js"
import { camelize, identifier, nonEmptyString, toComment, decodeRefTokens } from "./Utils.js"
import { convertObj } from "swagger2openapi"
import * as Context from "effect/Context"
import * as Option from "effect/Option"
Expand Down Expand Up @@ -91,7 +91,7 @@ export const make = Effect.gen(function* () {
const operations: Array<ParsedOperation> = []

function resolveRef(ref: string) {
const parts = ref.split("/").slice(1)
const parts = decodeRefTokens(ref)
let current: any = spec
for (const part of parts) {
current = current[part]
Expand Down
24 changes: 24 additions & 0 deletions src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,27 @@ export const toComment = Option.match({
* ${description.replace(/\*\//g, " * /").split("\n").join("\n* ")}
*/\n`,
})

// Decode an OpenAPI $ref JSON Pointer fragment into path tokens.
// Handles RFC3986 percent-decoding and RFC6901 JSON Pointer escapes (~0/~1).
export const decodeRefTokens = (ref: string): ReadonlyArray<string> => {
if (!ref) return []
let fragment = ref.startsWith("#") ? ref.slice(1) : ref
if (fragment.startsWith("/")) fragment = fragment.slice(1)
if (fragment.length === 0) return []
return fragment.split("/").map((raw) => {
let token = raw
try {
token = decodeURIComponent(raw)
} catch {
// leave as-is if not a valid percent-encoded sequence
}
// Unescape JSON Pointer tokens per RFC6901
return token.replace(/~1/g, "/").replace(/~0/g, "~")
})
}

export const refLastToken = (ref: string): string => {
const tokens = decodeRefTokens(ref)
return tokens.length > 0 ? tokens[tokens.length - 1] : ref
}