Skip to content

Commit

Permalink
fix: handle discriminators in experimental parser
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos committed Nov 10, 2024
1 parent 04c88dd commit ff3aa4a
Show file tree
Hide file tree
Showing 66 changed files with 1,103 additions and 351 deletions.
5 changes: 5 additions & 0 deletions .changeset/shiny-bears-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: handle discriminators in experimental parser
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ const getSpec = async ({ config }: { config: Config }) => {
const absolutePathOrUrl = existsSync(config.input.path)
? path.resolve(config.input.path)
: config.input.path;
spec = await $RefParser.bundle(absolutePathOrUrl, absolutePathOrUrl, {});
spec = await $RefParser.bundle(absolutePathOrUrl);
}

return spec;
Expand Down
19 changes: 19 additions & 0 deletions packages/openapi-ts/src/ir/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,23 @@ interface ContextFile {
}

export class IRContext<Spec extends Record<string, any> = any> {
/**
* Configuration for parsing and generating the output. This
* is a mix of user-provided and default values.
*/
public config: Config;
/**
* A map of files that will be generated from `spec`.
*/
public files: Files;
/**
* Intermediate representation model obtained from `spec`.
*/
public ir: IR;
public parserConfig: ParserConfig;
/**
* Resolved specification from `input`.
*/
public spec: Spec;

constructor({
Expand Down Expand Up @@ -62,6 +75,9 @@ export class IRContext<Spec extends Record<string, any> = any> {
return createdFile;
}

/**
* Returns a specific file by ID from `files`.
*/
public file({ id }: Pick<ContextFile, 'id'>): TypeScriptFile | undefined {
return this.files[id];
}
Expand All @@ -77,6 +93,9 @@ export class IRContext<Spec extends Record<string, any> = any> {
});
}

/**
* Returns a resolved reference from `spec`.
*/
public resolveRef<T>($ref: string) {
return resolveRef<T>({
$ref,
Expand Down
6 changes: 6 additions & 0 deletions packages/openapi-ts/src/ir/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ export const deduplicateSchema = <T extends IRSchemaObject>({

for (const item of schema.items) {
// skip nested schemas for now, handle if necessary
if (!item.type && item.items) {
uniqueItems.push(item);
continue;
}

if (
// no `type` might still include `$ref` or `const`
!item.type ||
item.type === 'boolean' ||
item.type === 'null' ||
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/openApi/3.0.x/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ export const parseV3_0_X = (context: IRContext<OpenApiV3_0_X>) => {
const schema = context.spec.components.schemas[name];

parseSchema({
$ref,

Check warning on line 241 in packages/openapi-ts/src/openApi/3.0.x/parser/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/openApi/3.0.x/parser/index.ts#L241

Added line #L241 was not covered by tests
context,
name,
schema,
});
}
Expand Down
Loading

0 comments on commit ff3aa4a

Please sign in to comment.