Skip to content

Commit

Permalink
fix: generate correct optional key in types when using positional arg…
Browse files Browse the repository at this point in the history
…uments (useOptions: false)
  • Loading branch information
mrlubos committed May 15, 2024
1 parent 0277e29 commit 86485c4
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 49 deletions.
5 changes: 5 additions & 0 deletions .changeset/selfish-waves-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix: generate correct optional key in types when using positional arguments (useOptions: false)
19 changes: 0 additions & 19 deletions packages/openapi-ts/src/utils/required.ts

This file was deleted.

12 changes: 10 additions & 2 deletions packages/openapi-ts/src/utils/write/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import type {
import type { Client } from '../../types/client';
import { getConfig } from '../config';
import { escapeComment, escapeName } from '../escape';
import { modelIsRequired } from '../required';
import { transformServiceName } from '../transform';
import { unique } from '../unique';
import { uniqueTypeName } from './type';
Expand Down Expand Up @@ -82,11 +81,20 @@ const toOperationParamType = (
];
}

const getDefaultPrintable = (
p: OperationParameter | Model,
): string | undefined => {
if (p.default === undefined) {
return undefined;
}
return JSON.stringify(p.default, null, 4);
};

Check warning on line 92 in packages/openapi-ts/src/utils/write/services.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/write/services.ts#L84-L92

Added lines #L84 - L92 were not covered by tests
return operation.parameters.map((p) => {
const typePath = `${importedType}['${p.name}']`;
return {
default: p?.default,
isRequired: modelIsRequired(p) === '',
isRequired: (!p.isRequired && !getDefaultPrintable(p) ? '?' : '') === '',

Check warning on line 97 in packages/openapi-ts/src/utils/write/services.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/write/services.ts#L97

Added line #L97 was not covered by tests
name: p.name,
type: typePath,
};
Expand Down
3 changes: 1 addition & 2 deletions packages/openapi-ts/src/utils/write/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { Client } from '../../types/client';
import { getConfig } from '../config';
import { enumValue } from '../enum';
import { escapeComment } from '../escape';
import { modelIsRequired } from '../required';
import { unique } from '../unique';

const base = (model: Model) => {
Expand Down Expand Up @@ -86,7 +85,7 @@ const typeInterface = (model: Model) => {
}

const properties: Property[] = model.properties.map((property) => {
let maybeRequired = modelIsRequired(property);
let maybeRequired = property.isRequired ? '' : '?';

Check warning on line 88 in packages/openapi-ts/src/utils/write/type.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/write/type.ts#L88

Added line #L88 was not covered by tests
let value = toType(property);
// special case for additional properties type
if (property.name === '[key: string]' && maybeRequired) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,65 +24,65 @@ export type CallWithDefaultParametersData = {
/**
* This is a simple boolean with default value
*/
parameterBoolean: boolean | null;
parameterBoolean?: boolean | null;
/**
* This is a simple enum with default value
*/
parameterEnum: 'Success' | 'Warning' | 'Error';
parameterEnum?: 'Success' | 'Warning' | 'Error';
/**
* This is a simple model with default value
*/
parameterModel: ModelWithString | null;
parameterModel?: ModelWithString | null;
/**
* This is a simple number with default value
*/
parameterNumber: number | null;
parameterNumber?: number | null;
/**
* This is a simple string with default value
*/
parameterString: string | null;
parameterString?: string | null;
};

export type CallWithDefaultOptionalParametersData = {
/**
* This is a simple boolean that is optional with default value
*/
parameterBoolean: boolean;
parameterBoolean?: boolean;
/**
* This is a simple enum that is optional with default value
*/
parameterEnum: 'Success' | 'Warning' | 'Error';
parameterEnum?: 'Success' | 'Warning' | 'Error';
/**
* This is a simple model that is optional with default value
*/
parameterModel: ModelWithString;
parameterModel?: ModelWithString;
/**
* This is a simple number that is optional with default value
*/
parameterNumber: number;
parameterNumber?: number;
/**
* This is a simple string that is optional with default value
*/
parameterString: string;
parameterString?: string;
};

export type CallToTestOrderOfParamsData = {
/**
* This is a optional string with default
*/
parameterOptionalStringWithDefault: string;
parameterOptionalStringWithDefault?: string;
/**
* This is a optional string with empty default
*/
parameterOptionalStringWithEmptyDefault: string;
parameterOptionalStringWithEmptyDefault?: string;
/**
* This is a optional string with no default
*/
parameterOptionalStringWithNoDefault?: string;
/**
* This is a string that can be null with default
*/
parameterStringNullableWithDefault: string | null;
parameterStringNullableWithDefault?: string | null;
/**
* This is a string that can be null with no default
*/
Expand All @@ -108,67 +108,67 @@ export type $OpenApiTs = {
/**
* This is a simple boolean with default value
*/
parameterBoolean: boolean | null;
parameterBoolean?: boolean | null;
/**
* This is a simple enum with default value
*/
parameterEnum: 'Success' | 'Warning' | 'Error';
parameterEnum?: 'Success' | 'Warning' | 'Error';
/**
* This is a simple model with default value
*/
parameterModel: ModelWithString | null;
parameterModel?: ModelWithString | null;
/**
* This is a simple number with default value
*/
parameterNumber: number | null;
parameterNumber?: number | null;
/**
* This is a simple string with default value
*/
parameterString: string | null;
parameterString?: string | null;
};
};
post: {
req: {
/**
* This is a simple boolean that is optional with default value
*/
parameterBoolean: boolean;
parameterBoolean?: boolean;
/**
* This is a simple enum that is optional with default value
*/
parameterEnum: 'Success' | 'Warning' | 'Error';
parameterEnum?: 'Success' | 'Warning' | 'Error';
/**
* This is a simple model that is optional with default value
*/
parameterModel: ModelWithString;
parameterModel?: ModelWithString;
/**
* This is a simple number that is optional with default value
*/
parameterNumber: number;
parameterNumber?: number;
/**
* This is a simple string that is optional with default value
*/
parameterString: string;
parameterString?: string;
};
};
put: {
req: {
/**
* This is a optional string with default
*/
parameterOptionalStringWithDefault: string;
parameterOptionalStringWithDefault?: string;
/**
* This is a optional string with empty default
*/
parameterOptionalStringWithEmptyDefault: string;
parameterOptionalStringWithEmptyDefault?: string;
/**
* This is a optional string with no default
*/
parameterOptionalStringWithNoDefault?: string;
/**
* This is a string that can be null with default
*/
parameterStringNullableWithDefault: string | null;
parameterStringNullableWithDefault?: string | null;
/**
* This is a string that can be null with no default
*/
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-ts/test/sample.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const main = async () => {
// include: '^NestedAnyOfArraysNullable',
// name: 'PascalCase',
},
// useOptions: false,
};

const { createClient } = await import(
Expand Down

0 comments on commit 86485c4

Please sign in to comment.