diff --git a/test/externalRefs.test.ts b/test/externalRefs.test.ts index ab8ea80..3e2b4be 100644 --- a/test/externalRefs.test.ts +++ b/test/externalRefs.test.ts @@ -12,28 +12,68 @@ describe('External $ref', () => { silent: true, }); - const externalDefinitionSchema = await import( - path.resolve(outputPath, 'components/schemas/ExternalDefinition') + // $ref: './external-definition.yaml#/components/schemas/Foo1' + const externalDefinitionWithRefSchema = await import( + path.resolve(outputPath, 'components/schemas/ExternalDefinitionWithRef') ); - expect(externalDefinitionSchema.default).toEqual({ - description: 'External Foo 1 description', + expect(externalDefinitionWithRefSchema.default).toEqual({ + description: 'External Foo description', type: ['string', 'null'], enum: ['yes', 'no', null], }); - const localDefinitionReferencingExternalSchema = await import( + // $ref: './external-definition-whole-document.yaml' + const externalDefinitionWholeDocumentSchema = await import( + path.resolve( + outputPath, + 'components/schemas/ExternalDefinitionWholeDocument', + ) + ); + + expect(externalDefinitionWholeDocumentSchema.default).toEqual({ + description: 'External definition whole document', + type: ['string', 'null'], + enum: ['yes', 'no', null], + }); + + // $ref: './external-definition-nested-refs.yaml#/components/schemas/BarFromRef' + const externalDefinitionNestedRefsSchema = await import( + path.resolve( + outputPath, + 'components/schemas/ExternalDefinitionNestedRefs', + ) + ); + + expect(externalDefinitionNestedRefsSchema.default).toEqual({ + description: 'External Bar description', + type: ['string', 'null'], + enum: ['yes', 'no', null], + }); + + // Local definition referencing external schemas + const localDefinitionReferencingExternalSchemas = await import( path.resolve( outputPath, 'components/schemas/LocalDefinitionReferencingExternal', ) ); - expect(localDefinitionReferencingExternalSchema.default).toEqual({ + expect(localDefinitionReferencingExternalSchemas.default).toEqual({ type: 'object', properties: { - remoteDefinition: { - description: 'External Foo 1 description', + externalDefinitionWithRef: { + description: 'External Foo description', + type: ['string', 'null'], + enum: ['yes', 'no', null], + }, + externalDefinitionWholeDocument: { + description: 'External definition whole document', + type: ['string', 'null'], + enum: ['yes', 'no', null], + }, + externalDefinitionNestedRefs: { + description: 'External Bar description', type: ['string', 'null'], enum: ['yes', 'no', null], }, diff --git a/test/fixtures/external-ref/external-definition-nested-refs.yaml b/test/fixtures/external-ref/external-definition-nested-refs.yaml new file mode 100644 index 0000000..ff0bf70 --- /dev/null +++ b/test/fixtures/external-ref/external-definition-nested-refs.yaml @@ -0,0 +1,9 @@ +openapi: 3.0.3 +info: + title: title + description: description + version: 1.0.0 +components: + schemas: + BarFromRef: + $ref: './external-definition.yaml#/components/schemas/Bar' diff --git a/test/fixtures/external-ref/external-definition-whole-document.yaml b/test/fixtures/external-ref/external-definition-whole-document.yaml new file mode 100644 index 0000000..a681fa9 --- /dev/null +++ b/test/fixtures/external-ref/external-definition-whole-document.yaml @@ -0,0 +1,6 @@ +description: External definition whole document +type: string +nullable: true +enum: + - yes + - no diff --git a/test/fixtures/external-ref/external-definition-1.yaml b/test/fixtures/external-ref/external-definition.yaml similarity index 69% rename from test/fixtures/external-ref/external-definition-1.yaml rename to test/fixtures/external-ref/external-definition.yaml index 7b95f44..a71a1b7 100644 --- a/test/fixtures/external-ref/external-definition-1.yaml +++ b/test/fixtures/external-ref/external-definition.yaml @@ -5,15 +5,15 @@ info: version: 1.0.0 components: schemas: - Foo1: - description: External Foo 1 description + Foo: + description: External Foo description type: string nullable: true enum: - yes - no - Bar1: - description: External Bar 1 description + Bar: + description: External Bar description type: string nullable: true enum: diff --git a/test/fixtures/external-ref/specs.yaml b/test/fixtures/external-ref/specs.yaml index 5f60181..858a348 100644 --- a/test/fixtures/external-ref/specs.yaml +++ b/test/fixtures/external-ref/specs.yaml @@ -8,7 +8,15 @@ components: LocalDefinitionReferencingExternal: type: object properties: - remoteDefinition: - $ref: '#/components/schemas/ExternalDefinition' - ExternalDefinition: - $ref: './external-definition-1.yaml#/components/schemas/Foo1' + externalDefinitionWithRef: + $ref: '#/components/schemas/ExternalDefinitionWithRef' + externalDefinitionWholeDocument: + $ref: '#/components/schemas/ExternalDefinitionWholeDocument' + externalDefinitionNestedRefs: + $ref: '#/components/schemas/ExternalDefinitionNestedRefs' + ExternalDefinitionWithRef: + $ref: './external-definition.yaml#/components/schemas/Foo' + ExternalDefinitionWholeDocument: + $ref: './external-definition-whole-document.yaml' + ExternalDefinitionNestedRefs: + $ref: './external-definition-nested-refs.yaml#/components/schemas/BarFromRef'