Skip to content

Commit

Permalink
Merge pull request #138 from palantir/ssanjay/fixActionParams
Browse files Browse the repository at this point in the history
Fix action imports for object array params
  • Loading branch information
ssanjay1 authored Mar 20, 2024
2 parents 2deefb9 + 0298f3b commit 56ce464
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 6 deletions.
6 changes: 3 additions & 3 deletions examples-extra/docs_example/ontology.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
"Todo": {
"description": "A todo Object",
"dataType": {
"type": "object",
"objectApiName": "Todo",
"objectTypeApiName": "Todo"
"type": "array",

"subType": { "type": "object", "objectApiName": "Todo", "objectTypeApiName": "Todo" }
},
"required": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type ActionDef$completeTodo$Params = {
};
Todo: {
description: 'A todo Object';
multiplicity: false;
multiplicity: true;
nullable: false;
type: ObjectActionDataType<'Todo', Todo>;
};
Expand Down Expand Up @@ -42,7 +42,7 @@ export const completeTodo: ActionDef$completeTodo = {
apiName: 'completeTodo',
parameters: {
Todo: {
multiplicity: false,
multiplicity: true,
type: {
type: 'object',
object: 'Todo',
Expand Down
5 changes: 5 additions & 0 deletions packages/generator/changelog/@unreleased/pr-138.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: Fix action params
links:
- https://github.com/palantir/osdk-ts/pull/138
25 changes: 25 additions & 0 deletions packages/generator/src/util/test/TodoWireOntology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,31 @@ export const TodoWireOntology = {
}],
status: "ACTIVE",
},
"deleteTodos": {
apiName: "deleteTodos",
description: "An action which takes in an array of objects",
parameters: {
object: {
description: "Todo(s) to be deleted",
"dataType": {
"type": "array",

"subType": {
"type": "object",
"objectApiName": "Todo",
"objectTypeApiName": "Todo",
},
},
required: false,
},
},
rid: "ri.ontology.main.action-type.8f94017d-cf17-4fa8-84c3-8e01e5d594f2",
operations: [{
type: "deleteObject",
objectTypeApiName: "Todo",
}],
status: "ACTIVE",
},
},
objectTypes: {
Todo: {
Expand Down
11 changes: 11 additions & 0 deletions packages/generator/src/v1.1/generateActions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ describe(generateActions, () => {
},
options?: O,
): Promise<Result<ActionResponseFromOptions<O, Edits<void, Todo>>, ActionError>>;
/**
* An action which takes in an array of objects
* @param {Array<Todo | Todo["__primaryKey"]>} params.object
*/
deleteTodos<O extends ActionExecutionOptions>(
params: {
object?: Array<Todo | Todo['__primaryKey']>;
},
options?: O,
): Promise<Result<ActionResponseFromOptions<O, Edits<void, void>>, ActionError>>;
}
"
`);
Expand Down
11 changes: 11 additions & 0 deletions packages/generator/src/v1.1/generateBulkActions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ describe(generateBulkActions, () => {
}[],
options?: O,
): Promise<Result<BulkActionResponseFromOptions<O, Edits<void, Todo>>, ActionError>>;
/**
* An action which takes in an array of objects
* @param {Array<Todo | Todo["__primaryKey"]>} params.object
*/
deleteTodos<O extends BulkActionExecutionOptions>(
params: {
object?: Array<Todo | Todo['__primaryKey']>;
}[],
options?: O,
): Promise<Result<BulkActionResponseFromOptions<O, Edits<void, void>>, ActionError>>;
}
"
`);
Expand Down
5 changes: 4 additions & 1 deletion packages/generator/src/v1.1/generateMetadataFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ describe(generateMetadataFile, () => {
import type { Ontology as ClientOntology } from '@osdk/legacy-client';
import type { Actions } from './ontology/actions/Actions';
import type { BulkActions } from './ontology/actions/BulkActions';
import { deleteTodos } from './ontology/actions/deleteTodos';
import { markTodoCompleted } from './ontology/actions/markTodoCompleted';
import type { Objects } from './ontology/objects/Objects';
import { Person } from './ontology/objects/Person';
Expand All @@ -59,6 +60,7 @@ describe(generateMetadataFile, () => {
};
actions: {
markTodoCompleted: typeof markTodoCompleted;
deleteTodos: typeof deleteTodos;
};
queries: {
getCount: typeof getCount;
Expand All @@ -75,11 +77,12 @@ describe(generateMetadataFile, () => {
},
actions: {
markTodoCompleted,
deleteTodos,
},
queries: {
getCount,
},
} satisfies OntologyDefinition<'Todo' | 'Person', 'markTodoCompleted', 'getCount'>;
} satisfies OntologyDefinition<'Todo' | 'Person', 'markTodoCompleted' | 'deleteTodos', 'getCount'>;
export interface Ontology extends ClientOntology<typeof Ontology> {
objects: Objects;
Expand Down
16 changes: 16 additions & 0 deletions packages/generator/src/v1.1/generatePerActionDataFiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,20 @@ describe(generatePerActionDataFiles, () => {
);
expect(helper.getFiles()[`${BASE_PATH}/index.ts`]).toEqual("export {};\n");
});

it("imports object types correctly with array params in actions", async () => {
const helper = createMockMinimalFiles();
const BASE_PATH = "/foo/actions";
await generatePerActionDataFiles(
TodoWireOntology,
helper.minimalFiles,
BASE_PATH,
"",
true,
);

expect(helper.getFiles()[`${BASE_PATH}/deleteTodos.ts`]).toContain(
"import type { Todo } from '../objects';\n",
);
});
});
12 changes: 12 additions & 0 deletions packages/generator/src/v1.1/generatePerActionDataFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ export async function generatePerActionDataFiles(
getObjectDefIdentifier(p.dataType.objectTypeApiName!, v2),
);
}
if (
p.dataType.type === "array"
&& (p.dataType.subType.type === "object"
|| p.dataType.subType.type === "objectSet")
) {
referencedObjectDefs.add(
getObjectDefIdentifier(p.dataType.subType.objectApiName!, v2),
);
referencedObjectDefs.add(
getObjectDefIdentifier(p.dataType.subType.objectTypeApiName!, v2),
);
}
}

const importObjects = referencedObjectDefs.size > 0
Expand Down

0 comments on commit 56ce464

Please sign in to comment.