Skip to content

Commit

Permalink
fix: do not transform property names for standalone clients
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos committed May 24, 2024
1 parent 17b982c commit 735561c
Show file tree
Hide file tree
Showing 40 changed files with 4,162 additions and 307 deletions.
5 changes: 5 additions & 0 deletions .changeset/fair-mugs-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/client-fetch': patch
---

fix: JSON stringify object headers
5 changes: 5 additions & 0 deletions .changeset/twenty-ways-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: do not transform property names for standalone clients
66 changes: 33 additions & 33 deletions examples/openapi-ts-fetch/src/client/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export type UpdatePetWithFormData = {

export type DeletePetData = {
headers?: {
apiKey?: string;
api_key?: string;
};
path: {
/**
Expand Down Expand Up @@ -330,11 +330,11 @@ export type $OpenApiTs = {
/**
* Successful operation
*/
200: Pet;
'200': Pet;
/**
* Invalid input
*/
405: unknown;
'405': unknown;
};
};
put: {
Expand All @@ -343,19 +343,19 @@ export type $OpenApiTs = {
/**
* Successful operation
*/
200: Pet;
'200': Pet;
/**
* Invalid ID supplied
*/
400: unknown;
'400': unknown;
/**
* Pet not found
*/
404: unknown;
'404': unknown;
/**
* Validation exception
*/
405: unknown;
'405': unknown;
};
};
};
Expand All @@ -366,11 +366,11 @@ export type $OpenApiTs = {
/**
* successful operation
*/
200: Array<Pet>;
'200': Array<Pet>;
/**
* Invalid status value
*/
400: unknown;
'400': unknown;
};
};
};
Expand All @@ -381,11 +381,11 @@ export type $OpenApiTs = {
/**
* successful operation
*/
200: Array<Pet>;
'200': Array<Pet>;
/**
* Invalid tag value
*/
400: unknown;
'400': unknown;
};
};
};
Expand All @@ -396,15 +396,15 @@ export type $OpenApiTs = {
/**
* successful operation
*/
200: Pet;
'200': Pet;
/**
* Invalid ID supplied
*/
400: unknown;
'400': unknown;
/**
* Pet not found
*/
404: unknown;
'404': unknown;
};
};
post: {
Expand All @@ -413,7 +413,7 @@ export type $OpenApiTs = {
/**
* Invalid input
*/
405: unknown;
'405': unknown;
};
};
delete: {
Expand All @@ -422,7 +422,7 @@ export type $OpenApiTs = {
/**
* Invalid pet value
*/
400: unknown;
'400': unknown;
};
};
};
Expand All @@ -433,7 +433,7 @@ export type $OpenApiTs = {
/**
* successful operation
*/
200: ApiResponse;
'200': ApiResponse;
};
};
};
Expand All @@ -443,7 +443,7 @@ export type $OpenApiTs = {
/**
* successful operation
*/
200: {
'200': {
[key: string]: number;
};
};
Expand All @@ -456,11 +456,11 @@ export type $OpenApiTs = {
/**
* successful operation
*/
200: Order;
'200': Order;
/**
* Invalid input
*/
405: unknown;
'405': unknown;
};
};
};
Expand All @@ -471,15 +471,15 @@ export type $OpenApiTs = {
/**
* successful operation
*/
200: Order;
'200': Order;
/**
* Invalid ID supplied
*/
400: unknown;
'400': unknown;
/**
* Order not found
*/
404: unknown;
'404': unknown;
};
};
delete: {
Expand All @@ -488,11 +488,11 @@ export type $OpenApiTs = {
/**
* Invalid ID supplied
*/
400: unknown;
'400': unknown;
/**
* Order not found
*/
404: unknown;
'404': unknown;
};
};
};
Expand All @@ -514,7 +514,7 @@ export type $OpenApiTs = {
/**
* Successful operation
*/
200: User;
'200': User;
/**
* successful operation
*/
Expand All @@ -529,11 +529,11 @@ export type $OpenApiTs = {
/**
* successful operation
*/
200: string;
'200': string;
/**
* Invalid username/password supplied
*/
400: unknown;
'400': unknown;
};
};
};
Expand All @@ -554,15 +554,15 @@ export type $OpenApiTs = {
/**
* successful operation
*/
200: User;
'200': User;
/**
* Invalid username supplied
*/
400: unknown;
'400': unknown;
/**
* User not found
*/
404: unknown;
'404': unknown;
};
};
put: {
Expand All @@ -580,11 +580,11 @@ export type $OpenApiTs = {
/**
* Invalid username supplied
*/
400: unknown;
'400': unknown;
/**
* User not found
*/
404: unknown;
'404': unknown;
};
};
};
Expand Down
1 change: 1 addition & 0 deletions packages/client-fetch/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface Config
| (string | number | boolean)[]
| null
| undefined
| unknown
>;
/**
* The request method.
Expand Down
7 changes: 6 additions & 1 deletion packages/client-fetch/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,12 @@ export const mergeHeaders = (
mergedHeaders.append(key, v as string);
}
} else if (value !== undefined) {
mergedHeaders.set(key, value as string);
// assume object headers are meant to be JSON stringified, i.e. their
// content value in OpenAPI specification is 'application/json'
mergedHeaders.set(
key,
typeof value === 'object' ? JSON.stringify(value) : (value as string),
);
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions packages/openapi-ts/src/openApi/common/interfaces/client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { OpenApiParameter } from '../../v3/interfaces/OpenApiParameter';

export interface ModelComposition
extends Pick<Model, '$refs' | 'enums' | 'imports' | 'properties'> {
export: Extract<Model['export'], 'all-of' | 'any-of' | 'one-of'>;
Expand All @@ -11,7 +13,7 @@ export interface Enum {
}

export interface OperationParameter extends Model {
in: 'path' | 'query' | 'header' | 'formData' | 'body' | 'cookie';
in: 'body' | 'cookie' | 'formData' | 'header' | 'path' | 'query';
prop: string;
mediaType: string | null;
}
Expand All @@ -27,7 +29,7 @@ export interface OperationParameters extends Pick<Model, '$refs' | 'imports'> {
}

export interface OperationResponse extends Model {
in: 'response' | 'header';
in: 'header' | 'response';
code: number | 'default';
}

Expand Down Expand Up @@ -130,6 +132,11 @@ export interface Model extends Schema {
| 'one-of'
| 'reference';
imports: string[];
in:
| OperationParameter['in']
| OpenApiParameter['in']
| OperationResponse['in']
| '';
link: Model | null;
meta?: ModelMeta;
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { describe, expect, it } from 'vitest';

import { setConfig } from '../../../../utils/config';
import {
getOperationName,
getOperationParameterName,
getOperationResponseCode,
} from '../operation';
import { getOperationName, getOperationResponseCode } from '../operation';

describe('getOperationName', () => {
const options1: Parameters<typeof setConfig>[0] = {
Expand Down Expand Up @@ -225,29 +221,6 @@ describe('getOperationName', () => {
);
});

describe('getOperationParameterName', () => {
it.each([
{ expected: '', input: '' },
{ expected: 'foobar', input: 'foobar' },
{ expected: 'fooBar', input: 'fooBar' },
{ expected: 'fooBar', input: 'foo_bar' },
{ expected: 'fooBar', input: 'foo-bar' },
{ expected: 'fooBar', input: 'foo.bar' },
{ expected: 'fooBar', input: '@foo.bar' },
{ expected: 'fooBar', input: '$foo.bar' },
{ expected: 'fooBar', input: '123.foo.bar' },
{ expected: 'fooBar', input: 'Foo-Bar' },
{ expected: 'fooBar', input: 'FOO-BAR' },
{ expected: 'fooBar', input: 'foo[bar]' },
{ expected: 'fooBarArray', input: 'foo.bar[]' },
])(
'getOperationParameterName($input) -> $expected',
({ input, expected }) => {
expect(getOperationParameterName(input)).toBe(expected);
},
);
});

describe('getOperationResponseCode', () => {
it.each([
{ expected: null, input: '' },
Expand Down
Loading

0 comments on commit 735561c

Please sign in to comment.