Skip to content

Commit b978e04

Browse files
authored
test(core): add regression coverage for cross-file $ref schema exports (#1107) (#3381)
* test(core): add regression coverage for cross-file $ref schema exports (#1107) * test(core): document cross-file $ref intent in issue-1107 spec
1 parent 3e1664e commit b978e04

9 files changed

Lines changed: 163 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Generated by orval v8.11.0 🍺
3+
* Do not edit manually.
4+
* Issue 1107 - cross-file $ref type alias exports
5+
* OpenAPI spec version: 1.0.0
6+
*/
7+
import axios from 'axios';
8+
import type { AxiosRequestConfig, AxiosResponse } from 'axios';
9+
10+
import type { Pets } from './model';
11+
12+
export const listPets = (
13+
options?: AxiosRequestConfig,
14+
): Promise<AxiosResponse<Pets>> => {
15+
return axios.get(`/pets`, options);
16+
};
17+
18+
export type ListPetsResult = AxiosResponse<Pets>;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Generated by orval v8.11.0 🍺
3+
* Do not edit manually.
4+
* Issue 1107 - cross-file $ref type alias exports
5+
* OpenAPI spec version: 1.0.0
6+
*/
7+
8+
export interface Error {
9+
code: number;
10+
message: string;
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Generated by orval v8.11.0 🍺
3+
* Do not edit manually.
4+
* Issue 1107 - cross-file $ref type alias exports
5+
* OpenAPI spec version: 1.0.0
6+
*/
7+
8+
export * from './error';
9+
export * from './pet';
10+
export * from './pets';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Generated by orval v8.11.0 🍺
3+
* Do not edit manually.
4+
* Issue 1107 - cross-file $ref type alias exports
5+
* OpenAPI spec version: 1.0.0
6+
*/
7+
8+
export interface Pet {
9+
id: number;
10+
name: string;
11+
tag?: string;
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Generated by orval v8.11.0 🍺
3+
* Do not edit manually.
4+
* Issue 1107 - cross-file $ref type alias exports
5+
* OpenAPI spec version: 1.0.0
6+
*/
7+
import type { Pet } from './pet';
8+
9+
export type Pets = Pet[];

tests/api-generation.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,27 @@ test('vue-query issue-1026 keeps header params out of the query key getter', asy
209209
expect(content).toContain('headers?: MaybeRef<GetSomeEndpointHeaders>');
210210
expect(content).toContain('headers = unref(headers);');
211211
});
212+
213+
test('default issue-1107 emits exports for schemas defined via cross-file $ref', async () => {
214+
// Regression for #1107: a top-level `components.schemas.X` that is itself a
215+
// cross-file `$ref` (X -> another file's X) used to generate a schema file
216+
// with the import but no `export` for X, producing a dangling, unusable
217+
// module. Each referenced schema must still be a usable exported type.
218+
// Keep this focused assertion alongside the snapshot so #1107 fails with a
219+
// targeted message instead of a full-file snapshot diff.
220+
const model = (file: string) =>
221+
readFile(
222+
generated('default', 'issue-1107-cross-file-ref', 'model', file),
223+
'utf8',
224+
);
225+
226+
// Object schemas reached through a cross-file `$ref` are exported as types.
227+
expect(await model('pet.ts')).toContain('export interface Pet {');
228+
expect(await model('error.ts')).toContain('export interface Error {');
229+
230+
// The array schema both imports its item type and exports its own alias;
231+
// the missing `export type` line was the #1107 bug.
232+
const pets = await model('pets.ts');
233+
expect(pets).toContain("import type { Pet } from './pet';");
234+
expect(pets).toContain('export type Pets = Pet[];');
235+
});

tests/configs/default.config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,17 @@ export default defineConfig({
681681
target: '../specifications/issue-398-encoded-path-ref.yaml',
682682
},
683683
},
684+
'issue-1107-cross-file-ref': {
685+
output: {
686+
target: '../generated/default/issue-1107-cross-file-ref/endpoints.ts',
687+
schemas: '../generated/default/issue-1107-cross-file-ref/model',
688+
clean: true,
689+
formatter: 'prettier',
690+
},
691+
input: {
692+
target: '../specifications/issue-1107/issue-1107.yaml',
693+
},
694+
},
684695
'boolean-discriminator': {
685696
output: {
686697
target: '../generated/default/boolean-discriminator/endpoints.ts',
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Issue 1107 - external petstore
4+
version: 1.0.0
5+
paths: {}
6+
components:
7+
schemas:
8+
Pet:
9+
type: object
10+
required:
11+
- id
12+
- name
13+
properties:
14+
id:
15+
type: integer
16+
format: int64
17+
name:
18+
type: string
19+
tag:
20+
type: string
21+
Pets:
22+
type: array
23+
items:
24+
$ref: '#/components/schemas/Pet'
25+
Error:
26+
type: object
27+
required:
28+
- code
29+
- message
30+
properties:
31+
code:
32+
type: integer
33+
format: int32
34+
message:
35+
type: string
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Issue 1107 - cross-file $ref type alias exports
4+
version: 1.0.0
5+
paths:
6+
/pets:
7+
get:
8+
operationId: listPets
9+
responses:
10+
'200':
11+
description: A list of pets
12+
content:
13+
application/json:
14+
schema:
15+
$ref: '#/components/schemas/Pets'
16+
default:
17+
description: unexpected error
18+
content:
19+
application/json:
20+
schema:
21+
$ref: '#/components/schemas/Error'
22+
components:
23+
# Regression for #1107: every top-level schema here is itself a cross-file
24+
# `$ref`. `Pet` is intentionally not referenced by any path — it is reached
25+
# only transitively (`Pets` resolves to `Pet[]` in the external file), which
26+
# exercises emitting an export for a referenced-but-not-path-used schema.
27+
schemas:
28+
Pet:
29+
$ref: './issue-1107-petstore.yaml#/components/schemas/Pet'
30+
Pets:
31+
$ref: './issue-1107-petstore.yaml#/components/schemas/Pets'
32+
Error:
33+
$ref: './issue-1107-petstore.yaml#/components/schemas/Error'

0 commit comments

Comments
 (0)