@@ -2,6 +2,7 @@ import {default as $RefParser, getJsonSchemaRefParserDefaultOptions} from '@apid
22import asyncapi from '@asyncapi/specs'
33import { CLIError } from '@oclif/core/errors'
44import { safeStringify } from '@stoplight/yaml'
5+ import debug from 'debug'
56import {
67 JSONSchema4 ,
78 JSONSchema4Array ,
@@ -11,7 +12,7 @@ import {
1112 JSONSchema7 ,
1213} from 'json-schema'
1314import { createRequire } from 'node:module'
14- import path from 'node:path'
15+ import { default as nodePath } from 'node:path'
1516
1617// Used to require JSON files
1718const require = createRequire ( import . meta. url )
@@ -203,11 +204,23 @@ class API {
203204 return ( output || this . location ) . endsWith ( '.json' ) ? 'json' : 'yaml'
204205 }
205206
207+ isMainRefPath ( path : string ) : boolean {
208+ // $refs from json-schema-ref-parser lib returns posix style
209+ // paths. We need to make sure we compare all paths in posix style
210+ // independently of the platform runtime.
211+ const resolvedAbsLocation = nodePath
212+ . resolve ( this . location )
213+ . split ( nodePath ?. win32 ?. sep )
214+ . join ( nodePath ?. posix ?. sep ?? '/' )
215+
216+ return path === this . location || path === resolvedAbsLocation
217+ }
218+
206219 resolveContent ( values : SpecSchema ) : [ string , APIDefinition ] {
207220 let mainReference : JSONSchemaWithRaw = { parsed : { } , raw : '' }
208221
209222 for ( const [ absPath , reference ] of Object . entries ( values ) ) {
210- if ( absPath === this . location || absPath === path . resolve ( this . location ) ) {
223+ if ( this . isMainRefPath ( absPath ) ) {
211224 // $refs.values is not properly typed so we need to force it
212225 // with the resulting type of our custom defined parser
213226 mainReference = reference as JSONSchemaWithRaw
@@ -230,6 +243,9 @@ class API {
230243 const { parsed, raw} = mainReference
231244
232245 if ( ! parsed || ! raw || ! ( parsed instanceof Object ) || ! ( 'info' in parsed ) ) {
246+ debug ( 'bump-cli:definition' ) (
247+ `Main location (${ this . location } ) not found or empty (within ${ JSON . stringify ( Object . keys ( values ) ) } )` ,
248+ )
233249 throw new UnsupportedFormat ( "Definition needs to be an object with at least an 'info' key" )
234250 }
235251
@@ -246,11 +262,17 @@ class API {
246262 const refUrl = this . url ( absPath )
247263
248264 if (
249- / ^ \/ / . test ( absPath ) || // Unix style filesystem path
250- / ^ [ A - Z a - z ] + : \\ / . test ( absPath ) || // Windows style filesystem path
251- ( / ^ h t t p s ? : \/ \/ / . test ( absPath ) && definitionUrl . hostname === refUrl . hostname ) // Same domain URLs
265+ ( refUrl . hostname === '' ) && ( // filesystem path
266+ / ^ \/ / . test ( absPath ) || // Unix style
267+ / ^ [ A - Z a - z ] + : [ \\ \/ ] / . test ( absPath ) // Windows style
268+ ) || (
269+ / ^ h t t p s ? : \/ \/ / . test ( absPath ) &&
270+ definitionUrl . hostname === refUrl . hostname // Same domain URLs
271+ )
252272 ) {
253- return path . relative ( path . dirname ( this . location ) , absPath )
273+ const relativeLocation = nodePath . relative ( nodePath . dirname ( this . location ) , absPath )
274+ debug ( 'bump-cli:definition' ) ( `Resolved relative $ref location: ${ relativeLocation } ` )
275+ return relativeLocation
254276 }
255277
256278 return absPath
0 commit comments