Skip to content

Commit e9357b5

Browse files
authored
Merge pull request #3370 from hey-api/refactor/plugin-ir-ref
refactor: allow path in top-level detect function
2 parents 23b1574 + 27cd91f commit e9357b5

56 files changed

Lines changed: 1378 additions & 1068 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.changeset/fluffy-words-dress.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hey-api/shared": minor
3+
---
4+
5+
**utils**: rename `isTopLevelComponentRef` to `isTopLevelComponent`

.changeset/smart-comics-talk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hey-api/openapi-ts": patch
3+
---
4+
5+
**internal**: use shared schema processor

dev/openapi-python.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default defineConfig(() => [
1212
path: './logs',
1313
},
1414
output: {
15-
path: path.resolve(__dirname, '..', '.gen', 'python'),
15+
path: path.resolve(__dirname, '.gen', 'python'),
1616
},
1717
plugins: getPreset(),
1818
},

dev/openapi-ts.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default defineConfig(() => [
1212
path: './logs',
1313
},
1414
output: {
15-
path: path.resolve(__dirname, '..', '.gen', 'typescript'),
15+
path: path.resolve(__dirname, '.gen', 'typescript'),
1616
},
1717
plugins: getPreset(),
1818
},

packages/openapi-python/src/plugins/pydantic/shared/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Refs, Symbol, SymbolMeta } from '@hey-api/codegen-core';
2-
import type { IR } from '@hey-api/shared';
2+
import type { IR, SchemaExtractor } from '@hey-api/shared';
33

44
import type { $ } from '../../../py-dsl';
55
import type { PydanticPlugin } from '../types';
@@ -29,7 +29,11 @@ export type Ast = {
2929
};
3030

3131
export type IrSchemaToAstOptions = {
32+
/** The plugin instance. */
3233
plugin: PydanticPlugin['Instance'];
34+
/** Optional schema extractor function. */
35+
schemaExtractor?: SchemaExtractor;
36+
/** The plugin state references. */
3337
state: Refs<PluginState>;
3438
};
3539

packages/openapi-python/src/plugins/pydantic/v2/plugin.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,24 @@ export function irSchemaToAst({
1313
optional,
1414
plugin,
1515
schema,
16+
schemaExtractor,
1617
state,
1718
}: IrSchemaToAstOptions & {
1819
optional?: boolean;
1920
schema: IR.SchemaObject;
2021
}): Ast {
22+
if (schemaExtractor && !schema.$ref) {
23+
const extracted = schemaExtractor({
24+
meta: {
25+
resource: 'definition',
26+
resourceId: pathToJsonPointer(fromRef(state.path)),
27+
},
28+
path: fromRef(state.path),
29+
schema,
30+
});
31+
if (extracted !== schema) schema = extracted;
32+
}
33+
2134
if (schema.$ref) {
2235
const query: SymbolMeta = {
2336
category: 'schema',

packages/openapi-ts/src/plugins/@hey-api/typescript/shared/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import type { Refs, SymbolMeta } from '@hey-api/codegen-core';
2+
import type { SchemaExtractor } from '@hey-api/shared';
23

34
import type { HeyApiTypeScriptPlugin } from '../types';
45

56
export type IrSchemaToAstOptions = {
7+
/** The plugin instance. */
68
plugin: HeyApiTypeScriptPlugin['Instance'];
9+
/** Optional schema extractor function. */
10+
schemaExtractor?: SchemaExtractor;
11+
/** The plugin state references. */
712
state: Refs<PluginState>;
813
};
914

packages/openapi-ts/src/plugins/@hey-api/typescript/v1/plugin.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import type { Symbol } from '@hey-api/codegen-core';
2-
import { refs } from '@hey-api/codegen-core';
3-
import type { IR } from '@hey-api/shared';
4-
import type { SchemaWithType } from '@hey-api/shared';
5-
import { applyNaming } from '@hey-api/shared';
6-
import { deduplicateSchema } from '@hey-api/shared';
2+
import { fromRef, refs } from '@hey-api/codegen-core';
3+
import type { IR, SchemaWithType } from '@hey-api/shared';
4+
import { applyNaming, deduplicateSchema, pathToJsonPointer } from '@hey-api/shared';
75

86
import type { MaybeTsDsl, TypeTsDsl } from '../../../../ts-dsl';
97
import { $ } from '../../../../ts-dsl';
@@ -18,10 +16,23 @@ import { irSchemaWithTypeToAst } from './toAst';
1816
export function irSchemaToAst({
1917
plugin,
2018
schema,
19+
schemaExtractor,
2120
state,
2221
}: IrSchemaToAstOptions & {
2322
schema: IR.SchemaObject;
2423
}): MaybeTsDsl<TypeTsDsl> {
24+
if (schemaExtractor && !schema.$ref) {
25+
const extracted = schemaExtractor({
26+
meta: {
27+
resource: 'definition',
28+
resourceId: pathToJsonPointer(fromRef(state.path)),
29+
},
30+
path: fromRef(state.path),
31+
schema,
32+
});
33+
if (extracted !== schema) schema = extracted;
34+
}
35+
2536
if (schema.symbolRef) {
2637
const baseType = $.type(schema.symbolRef);
2738
if (schema.omit && schema.omit.length > 0) {

packages/openapi-ts/src/plugins/arktype/shared/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Refs, SymbolMeta } from '@hey-api/codegen-core';
2-
import type { IR } from '@hey-api/shared';
2+
import type { IR, SchemaExtractor } from '@hey-api/shared';
33
import type ts from 'typescript';
44

55
import type { $ } from '../../../ts-dsl';
@@ -13,7 +13,11 @@ export type Ast = {
1313
};
1414

1515
export type IrSchemaToAstOptions = {
16+
/** The plugin instance. */
1617
plugin: ArktypePlugin['Instance'];
18+
/** Optional schema extractor function. */
19+
schemaExtractor?: SchemaExtractor;
20+
/** The plugin state references. */
1721
state: Refs<PluginState>;
1822
};
1923

packages/openapi-ts/src/plugins/arktype/v2/plugin.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export function irSchemaToAst({
1313
// optional,
1414
plugin,
1515
schema,
16+
schemaExtractor,
1617
state,
1718
}: IrSchemaToAstOptions & {
1819
/**
@@ -23,6 +24,18 @@ export function irSchemaToAst({
2324
optional?: boolean;
2425
schema: IR.SchemaObject;
2526
}): Ast {
27+
if (schemaExtractor && !schema.$ref) {
28+
const extracted = schemaExtractor({
29+
meta: {
30+
resource: 'definition',
31+
resourceId: pathToJsonPointer(fromRef(state.path)),
32+
},
33+
path: fromRef(state.path),
34+
schema,
35+
});
36+
if (extracted !== schema) schema = extracted;
37+
}
38+
2639
let ast: Partial<Ast> = {};
2740

2841
// const z = plugin.referenceSymbol({

0 commit comments

Comments
 (0)