Skip to content

Commit

Permalink
fix: allow empty base types when all props are read- or writeonly
Browse files Browse the repository at this point in the history
the behaviour has been introduced to address #419
but it seems it's not required with the new implementation

fix #453
  • Loading branch information
Xiphe committed Aug 14, 2023
1 parent 9e3dabc commit c2de017
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 12 deletions.
21 changes: 21 additions & 0 deletions demo/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,27 @@ describe("ok", () => {
// @ts-expect-error (writeonly Property)
type ReadHidden = api.PagedListOfProductRead["items"][number]["hidden"];
});

it("handles mixed readonly and writeonly properties", async () => {
const base: api.ReadWriteMixed = {};
// @ts-expect-error (readonly Property)
base.message = "nope";
// @ts-expect-error (writeonly Property)
base.email = "hi@example.org";

const write: api.ReadWriteMixedWrite = {
email: "hi@example.org",
password: "123",
};
// @ts-expect-error (readonly Property)
write.message = "nope";

const read: api.ReadWriteMixedRead = {
message: "hi",
};
// @ts-expect-error (writeonly Property)
read.email = "hi@example.org";
});
});

describe("object query parameters", () => {
Expand Down
29 changes: 28 additions & 1 deletion demo/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ export type PagedListOfProductWrite = {
pageSize: number;
totalCount: number;
};
export type ReadWriteMixed = {};
export type ReadWriteMixedRead = {
message: string;
};
export type ReadWriteMixedWrite = {
email: string;
password: string;
};
/**
* Update an existing pet
*/
Expand Down Expand Up @@ -653,7 +661,10 @@ export function productsCreateMany(
body?: PagedListOfProductWrite | Pet,
opts?: Oazapfts.RequestOpts,
) {
return oazapfts.fetchText(
return oazapfts.fetchJson<{
status: 200;
data: {};
}>(
"/issue-446",
oazapfts.json({
...opts,
Expand All @@ -662,3 +673,19 @@ export function productsCreateMany(
}),
);
}
export function readWriteMixed(
readWriteMixed?: ReadWriteMixedWrite,
opts?: Oazapfts.RequestOpts,
) {
return oazapfts.fetchJson<{
status: 200;
data: ReadWriteMixedRead;
}>(
"issue-453",
oazapfts.json({
...opts,
method: "POST",
body: readWriteMixed,
}),
);
}
29 changes: 28 additions & 1 deletion demo/enumApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ export type PagedListOfProductWrite = {
pageSize: number;
totalCount: number;
};
export type ReadWriteMixed = {};
export type ReadWriteMixedRead = {
message: string;
};
export type ReadWriteMixedWrite = {
email: string;
password: string;
};
/**
* Update an existing pet
*/
Expand Down Expand Up @@ -652,7 +660,10 @@ export function productsCreateMany(
body?: PagedListOfProductWrite | Pet,
opts?: Oazapfts.RequestOpts,
) {
return oazapfts.fetchText(
return oazapfts.fetchJson<{
status: 200;
data: {};
}>(
"/issue-446",
oazapfts.json({
...opts,
Expand All @@ -661,6 +672,22 @@ export function productsCreateMany(
}),
);
}
export function readWriteMixed(
readWriteMixed?: ReadWriteMixedWrite,
opts?: Oazapfts.RequestOpts,
) {
return oazapfts.fetchJson<{
status: 200;
data: ReadWriteMixedRead;
}>(
"issue-453",
oazapfts.json({
...opts,
method: "POST",
body: readWriteMixed,
}),
);
}
export enum Status {
Available = "available",
Pending = "pending",
Expand Down
26 changes: 25 additions & 1 deletion demo/mergedReadWriteApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ export type PagedListOfProduct = {
pageSize: number;
totalCount: number;
};
export type ReadWriteMixed = {
email: string;
password: string;
message: string;
};
/**
* Update an existing pet
*/
Expand Down Expand Up @@ -630,7 +635,10 @@ export function productsCreateMany(
body?: PagedListOfProduct | Pet,
opts?: Oazapfts.RequestOpts,
) {
return oazapfts.fetchText(
return oazapfts.fetchJson<{
status: 200;
data: {};
}>(
"/issue-446",
oazapfts.json({
...opts,
Expand All @@ -639,3 +647,19 @@ export function productsCreateMany(
}),
);
}
export function readWriteMixed(
readWriteMixed?: ReadWriteMixed,
opts?: Oazapfts.RequestOpts,
) {
return oazapfts.fetchJson<{
status: 200;
data: ReadWriteMixed;
}>(
"issue-453",
oazapfts.json({
...opts,
method: "POST",
body: readWriteMixed,
}),
);
}
31 changes: 30 additions & 1 deletion demo/optimisticApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ export type PagedListOfProductWrite = {
pageSize: number;
totalCount: number;
};
export type ReadWriteMixed = {};
export type ReadWriteMixedRead = {
message: string;
};
export type ReadWriteMixedWrite = {
email: string;
password: string;
};
/**
* Update an existing pet
*/
Expand Down Expand Up @@ -708,7 +716,10 @@ export function productsCreateMany(
opts?: Oazapfts.RequestOpts,
) {
return oazapfts.ok(
oazapfts.fetchText(
oazapfts.fetchJson<{
status: 200;
data: {};
}>(
"/issue-446",
oazapfts.json({
...opts,
Expand All @@ -718,3 +729,21 @@ export function productsCreateMany(
),
);
}
export function readWriteMixed(
readWriteMixed?: ReadWriteMixedWrite,
opts?: Oazapfts.RequestOpts,
) {
return oazapfts.ok(
oazapfts.fetchJson<{
status: 200;
data: ReadWriteMixedRead;
}>(
"issue-453",
oazapfts.json({
...opts,
method: "POST",
body: readWriteMixed,
}),
),
);
}
56 changes: 52 additions & 4 deletions demo/petstore.json
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,6 @@
}
}
},

"post": {
"operationId": "Products_CreateMany",
"parameters": [],
Expand All @@ -1159,9 +1158,40 @@
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"type": "object",
"properties": {}
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {}
}
}
}
}
}
}
},
"issue-453": {
"post": {
"operationId": "ReadWriteMixed",
"parameters": [],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ReadWriteMixed"
}
}
}
},
"responses": {
"200": {
"description": "A successful response.",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ReadWriteMixed"
}
}
}
}
}
Expand All @@ -1170,6 +1200,24 @@
},
"components": {
"schemas": {
"ReadWriteMixed": {
"type": "object",
"properties": {
"email": {
"type": "string",
"writeOnly": true
},
"password": {
"type": "string",
"writeOnly": true
},
"message": {
"type": "string",
"readOnly": true
}
},
"required": ["email", "password", "message"]
},
"PagedListOfProduct": {
"type": "object",
"additionalProperties": false,
Expand Down
5 changes: 1 addition & 4 deletions src/codegen/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,11 +753,8 @@ export default class ApiGenerator {
return !isReadOnly && !isWriteOnly;
}
});
// By filtering by readOnly/writeOnly props, we may have filtered out all props in schemas
const hasFilteredAllProps = filteredPropertyNames.length === 0;
const names = hasFilteredAllProps ? propertyNames : filteredPropertyNames;

const members: ts.TypeElement[] = names.map((name) => {
const members: ts.TypeElement[] = filteredPropertyNames.map((name) => {
const schema = props[name];
const isRequired = required && required.includes(name);
let type = this.getTypeFromSchema(schema, name, onlyMode);
Expand Down

0 comments on commit c2de017

Please sign in to comment.