diff --git a/lib/core/lib/api.tsp b/lib/core/lib/api.tsp index d8626375d..5a5722baa 100644 --- a/lib/core/lib/api.tsp +++ b/lib/core/lib/api.tsp @@ -115,6 +115,13 @@ namespace Opportunities { op read is Router.read; @tag("optional") + @returnTypeChangedFrom( + CommonGrants.Versions.v0_6, + Responses.WithVersionErrors> + ) op search is Router.search; } @@ -137,6 +144,13 @@ namespace Awards { @added(Versions.v0_4) @tag("optional") + @returnTypeChangedFrom( + CommonGrants.Versions.v0_6, + Responses.WithVersionErrors> + ) op search is Router.search; } diff --git a/lib/core/lib/core/responses/success.tsp b/lib/core/lib/core/responses/success.tsp index 9c8722a8f..fa64b8d41 100644 --- a/lib/core/lib/core/responses/success.tsp +++ b/lib/core/lib/core/responses/success.tsp @@ -250,6 +250,77 @@ model FilteredT extends Success { ...FilteredBase; } +// ############################################################################ +// 200 search results response +// ############################################################################ + +/** Details about the search term applied to the response items */ +@Versioning.added(CommonGrants.Versions.v0_6) +model SearchResultsInfo { + /** The search term used to find the response items */ + @example("Pre-school education") + query: string; +} + +/** Body properties shared by `SearchResults` and `SearchResultsT` + * + * @template ItemsT The schema for the value of the `"items"` property in this response + * @template FilterT The schema for the value of the `"filter"` property in this response + */ +@Versioning.added(CommonGrants.Versions.v0_6) +model SearchResultsBase { + ...FilteredBase; + + /** The search term applied to the response items + * + * Omitted when the request had no search term. An empty or whitespace-only + * `search` value counts as no search term. + */ + searchInfo?: SearchResultsInfo; +} + +/** A paginated list of items of an unspecified shape matching a search + * + * Routes use the typed `SearchResultsT` variant. + */ +@doc("A paginated list of items matching a search") +@Versioning.added(CommonGrants.Versions.v0_6) +model SearchResults extends Success { + ...SearchResultsBase; +} + +/** Template for a paginated list of typed items matching a search, with a typed filter + * + * @template ItemsT The schema for the value of the `"items"` property in this response + * @template FilterT The schema for the value of the `"filter"` property in this response + * @example How to specify a custom search results response model + * + * ```typespec + * // Define a model for the items in the response + * model CustomModel { + * id: string; + * description: string; + * } + * + * // Define a model for the filter in the response + * model CustomFilter extends Record { + * lastModifiedAt: Filters.DateComparisonFilter; + * } + * + * // Pass that model to the `SearchResultsT` template + * alias CustomModelSearchResponse = Responses.SearchResultsT; + * ``` + */ +@doc("A paginated list of typed items matching a search, with a typed filter") +@Versioning.added(CommonGrants.Versions.v0_6) +model SearchResultsT extends Success { + // Inherit the 200 status code + ...Http.OkResponse; + + ...Headers.VersionResponse; + ...SearchResultsBase; +} + // ############################################################################ // 201 response // ############################################################################ diff --git a/lib/core/lib/core/routes/awards.tsp b/lib/core/lib/core/routes/awards.tsp index b90c215c9..04b4455b0 100644 --- a/lib/core/lib/core/routes/awards.tsp +++ b/lib/core/lib/core/routes/awards.tsp @@ -84,7 +84,7 @@ interface Awards { * Must be an extension of Models.AwardBase. Default is Models.AwardBase. */ @summary("Search awards") - @doc("Search for awards based on the provided filters.") + @doc("Search for awards based on the provided search term and filters.") @post @route("/search") search( @@ -106,5 +106,8 @@ interface Awards { pagination?: Pagination.PaginatedBodyParams, ...Headers.VersionRequest, - ): Responses.WithVersionErrors>; + ): Responses.WithVersionErrors>; } diff --git a/lib/core/lib/core/routes/opportunities.tsp b/lib/core/lib/core/routes/opportunities.tsp index a934312d3..55c80e5bb 100644 --- a/lib/core/lib/core/routes/opportunities.tsp +++ b/lib/core/lib/core/routes/opportunities.tsp @@ -89,7 +89,7 @@ interface Opportunities { * Must be an extension of Schemas.OpportunityBase. Default is Schemas.OpportunityBase. */ @summary("Search opportunities") - @doc("Search for opportunities based on the provided filters.") + @doc("Search for opportunities based on the provided search term and filters.") @post @route("/search") search( @@ -111,5 +111,8 @@ interface Opportunities { pagination?: Pagination.PaginatedBodyParams, ...Headers.VersionRequest, - ): Responses.WithVersionErrors>; + ): Responses.WithVersionErrors>; } diff --git a/website/public/openapi/openapi.0.1.0.yaml b/website/public/openapi/openapi.0.1.0.yaml index d3ddd2970..64e63db65 100644 --- a/website/public/openapi/openapi.0.1.0.yaml +++ b/website/public/openapi/openapi.0.1.0.yaml @@ -53,7 +53,7 @@ paths: post: operationId: Opportunities_search summary: Search opportunities - description: Search for opportunities based on the provided filters. + description: Search for opportunities based on the provided search term and filters. parameters: [] responses: '200': diff --git a/website/public/openapi/openapi.0.2.0.yaml b/website/public/openapi/openapi.0.2.0.yaml index 752b43621..fd3fe1559 100644 --- a/website/public/openapi/openapi.0.2.0.yaml +++ b/website/public/openapi/openapi.0.2.0.yaml @@ -397,7 +397,7 @@ paths: post: operationId: Opportunities_search summary: Search opportunities - description: Search for opportunities based on the provided filters. + description: Search for opportunities based on the provided search term and filters. parameters: [] responses: '200': diff --git a/website/public/openapi/openapi.0.3.0.yaml b/website/public/openapi/openapi.0.3.0.yaml index 15d5ec31f..43a3af9e1 100644 --- a/website/public/openapi/openapi.0.3.0.yaml +++ b/website/public/openapi/openapi.0.3.0.yaml @@ -465,7 +465,7 @@ paths: post: operationId: Opportunities_search summary: Search opportunities - description: Search for opportunities based on the provided filters. + description: Search for opportunities based on the provided search term and filters. parameters: [] responses: '200': diff --git a/website/public/openapi/openapi.0.4.0.yaml b/website/public/openapi/openapi.0.4.0.yaml index feffa18c3..2720d6820 100644 --- a/website/public/openapi/openapi.0.4.0.yaml +++ b/website/public/openapi/openapi.0.4.0.yaml @@ -361,7 +361,7 @@ paths: post: operationId: Awards_search summary: Search awards - description: Search for awards based on the provided filters. + description: Search for awards based on the provided search term and filters. parameters: [] responses: '200': @@ -619,7 +619,7 @@ paths: post: operationId: Opportunities_search summary: Search opportunities - description: Search for opportunities based on the provided filters. + description: Search for opportunities based on the provided search term and filters. parameters: [] responses: '200': diff --git a/website/public/openapi/openapi.0.5.0.yaml b/website/public/openapi/openapi.0.5.0.yaml index 68f1e6566..8051ae862 100644 --- a/website/public/openapi/openapi.0.5.0.yaml +++ b/website/public/openapi/openapi.0.5.0.yaml @@ -361,7 +361,7 @@ paths: post: operationId: Awards_search summary: Search awards - description: Search for awards based on the provided filters. + description: Search for awards based on the provided search term and filters. parameters: [] responses: '200': @@ -619,7 +619,7 @@ paths: post: operationId: Opportunities_search summary: Search opportunities - description: Search for opportunities based on the provided filters. + description: Search for opportunities based on the provided search term and filters. parameters: [] responses: '200': diff --git a/website/public/openapi/openapi.0.6.0.yaml b/website/public/openapi/openapi.0.6.0.yaml index 16c54ae8d..79d6c2cc5 100644 --- a/website/public/openapi/openapi.0.6.0.yaml +++ b/website/public/openapi/openapi.0.6.0.yaml @@ -1109,12 +1109,12 @@ paths: post: operationId: Awards_search summary: Search awards - description: Search for awards based on the provided filters. + description: Search for awards based on the provided search term and filters. parameters: - $ref: '#/components/parameters/CommonGrants.Headers.VersionRequest' responses: '200': - description: A paginated list of typed items with a typed filter + description: A paginated list of typed items matching a search, with a typed filter headers: CG-Version-Served: required: true @@ -1182,9 +1182,17 @@ paths: required: - filters description: The filters applied to the response items + searchInfo: + allOf: + - $ref: '#/components/schemas/CommonGrants.Responses.SearchResultsInfo' + description: |- + The search term applied to the response items + + Omitted when the request had no search term. An empty or whitespace-only + `search` value counts as no search term. allOf: - $ref: '#/components/schemas/CommonGrants.Responses.Success' - description: A paginated list of typed items with a typed filter + description: A paginated list of typed items matching a search, with a typed filter '400': description: The request is invalid, or its `CG-Version` value is malformed or was sent more than once headers: @@ -1909,12 +1917,12 @@ paths: post: operationId: Opportunities_search summary: Search opportunities - description: Search for opportunities based on the provided filters. + description: Search for opportunities based on the provided search term and filters. parameters: - $ref: '#/components/parameters/CommonGrants.Headers.VersionRequest' responses: '200': - description: A paginated list of typed items with a typed filter + description: A paginated list of typed items matching a search, with a typed filter headers: CG-Version-Served: required: true @@ -1982,9 +1990,17 @@ paths: required: - filters description: The filters applied to the response items + searchInfo: + allOf: + - $ref: '#/components/schemas/CommonGrants.Responses.SearchResultsInfo' + description: |- + The search term applied to the response items + + Omitted when the request had no search term. An empty or whitespace-only + `search` value counts as no search term. allOf: - $ref: '#/components/schemas/CommonGrants.Responses.Success' - description: A paginated list of typed items with a typed filter + description: A paginated list of typed items matching a search, with a typed filter '400': description: The request is invalid, or its `CG-Version` value is malformed or was sent more than once headers: @@ -8977,6 +8993,59 @@ components: allOf: - $ref: '#/components/schemas/CommonGrants.Responses.Success' description: A 200 response with a paginated list of items + CommonGrants.Responses.SearchResults: + type: object + required: + - items + - paginationInfo + - sortInfo + - filterInfo + properties: + items: + type: array + items: {} + description: Items from the current page + paginationInfo: + allOf: + - $ref: '#/components/schemas/CommonGrants.Pagination.PaginatedResultsInfo' + description: Details about the paginated results + sortInfo: + allOf: + - $ref: '#/components/schemas/CommonGrants.Sorting.SortedResultsInfo' + description: The sort order of the items + filterInfo: + type: object + properties: + filters: {} + errors: + type: array + items: + type: string + description: Non-fatal errors that occurred during filtering + required: + - filters + description: The filters applied to the response items + searchInfo: + allOf: + - $ref: '#/components/schemas/CommonGrants.Responses.SearchResultsInfo' + description: |- + The search term applied to the response items + + Omitted when the request had no search term. An empty or whitespace-only + `search` value counts as no search term. + allOf: + - $ref: '#/components/schemas/CommonGrants.Responses.Success' + description: A paginated list of items matching a search + CommonGrants.Responses.SearchResultsInfo: + type: object + required: + - query + properties: + query: + type: string + description: The search term used to find the response items + example: Pre-school education + description: Details about the search term applied to the response items CommonGrants.Responses.Sorted: type: object required: diff --git a/website/public/schemas/yaml/SearchResults.yaml b/website/public/schemas/yaml/SearchResults.yaml new file mode 100644 index 000000000..b8e1509b9 --- /dev/null +++ b/website/public/schemas/yaml/SearchResults.yaml @@ -0,0 +1,45 @@ +$schema: https://json-schema.org/draft/2020-12/schema +$id: SearchResults.yaml +type: object +properties: + items: + type: array + items: {} + description: Items from the current page + paginationInfo: + $ref: PaginatedResultsInfo.yaml + description: Details about the paginated results + sortInfo: + $ref: SortedResultsInfo.yaml + description: The sort order of the items + filterInfo: + type: object + properties: + filters: {} + errors: + type: array + items: + type: string + description: Non-fatal errors that occurred during filtering + required: + - filters + unevaluatedProperties: + not: {} + description: The filters applied to the response items + searchInfo: + $ref: SearchResultsInfo.yaml + description: |- + The search term applied to the response items + + Omitted when the request had no search term. An empty or whitespace-only + `search` value counts as no search term. +required: + - items + - paginationInfo + - sortInfo + - filterInfo +allOf: + - $ref: Success.yaml +unevaluatedProperties: + not: {} +description: A paginated list of items matching a search diff --git a/website/public/schemas/yaml/SearchResultsInfo.yaml b/website/public/schemas/yaml/SearchResultsInfo.yaml new file mode 100644 index 000000000..5045a0127 --- /dev/null +++ b/website/public/schemas/yaml/SearchResultsInfo.yaml @@ -0,0 +1,14 @@ +$schema: https://json-schema.org/draft/2020-12/schema +$id: SearchResultsInfo.yaml +type: object +properties: + query: + type: string + examples: + - Pre-school education + description: The search term used to find the response items +required: + - query +unevaluatedProperties: + not: {} +description: Details about the search term applied to the response items diff --git a/website/src/content/docs/protocol/responses/error.mdx b/website/src/content/docs/protocol/responses/error.mdx index fc31e765a..63969765c 100644 --- a/website/src/content/docs/protocol/responses/error.mdx +++ b/website/src/content/docs/protocol/responses/error.mdx @@ -2,7 +2,7 @@ title: Errors description: A standard error response schema, used to create custom error responses sidebar: - order: 5 + order: 6 error: example: file: diff --git a/website/src/content/docs/protocol/responses/filtered.mdx b/website/src/content/docs/protocol/responses/filtered.mdx index 387fea1b6..b61985e91 100644 --- a/website/src/content/docs/protocol/responses/filtered.mdx +++ b/website/src/content/docs/protocol/responses/filtered.mdx @@ -26,8 +26,8 @@ filtered: "totalPages": 3 }, "sortInfo": { - "field": "name", - "direction": "asc" + "sortBy": "name", + "sortOrder": "asc" }, "filterInfo": { "filters": { @@ -52,7 +52,7 @@ filtered: file: path: "lib/core/lib/core/responses/success.tsp" startLine: 177 - endLine: 198 + endLine: 203 python: file: path: "lib/python-sdk/common_grants_sdk/schemas/pydantic/responses/success.py" @@ -102,6 +102,7 @@ import "@common-grants/core"; import "@typespec/http"; using TypeSpec.Http; +using CommonGrants; using CommonGrants.Pagination; using CommonGrants.Responses; diff --git a/website/src/content/docs/protocol/responses/index.mdx b/website/src/content/docs/protocol/responses/index.mdx index 254889cd0..2f7b8879e 100644 --- a/website/src/content/docs/protocol/responses/index.mdx +++ b/website/src/content/docs/protocol/responses/index.mdx @@ -34,6 +34,11 @@ Learn more about the response models defined by the CommonGrants protocol: href="/protocol/responses/filtered" description="200 response with a filtered list of items" /> + + +### Usage + +Use the `SearchResultsT` template to define a 200 response with a paginated list of typed items matching a search within an API operation: + +```typespec +import "@common-grants/core"; +import "@typespec/http"; + +using TypeSpec.Http; +using CommonGrants; +using CommonGrants.Pagination; +using CommonGrants.Responses; + +model TestModel { + id: string; + name: string; +} + +model TestFilter extends Record { + lastModifiedAt: Filters.DateComparisonFilter; +} + +@summary("Search test models") +@doc("Get a paginated list of test models matching a search term and filter") +@post +op searchTest( + search?: string, + filters?: TestFilter, + pagination?: PaginatedBodyParams, +): SearchResultsT; +``` + +### Changelog + + + +## SearchResultsInfo + +Details about the search term applied to the response items. + +### Table + +| Parameter | Type | Description | +| --------- | --------------------------------------- | ----------------------------------------------- | +| query | [string](/protocol/types/string#string) | The search term used to find the response items | + +### Formats + + + +### Changelog + + diff --git a/website/src/content/docs/protocol/responses/sorted.mdx b/website/src/content/docs/protocol/responses/sorted.mdx index 640467a18..ead2ed9a5 100644 --- a/website/src/content/docs/protocol/responses/sorted.mdx +++ b/website/src/content/docs/protocol/responses/sorted.mdx @@ -26,8 +26,8 @@ sorted: "totalPages": 3 }, "sortInfo": { - "field": "name", - "direction": "asc" + "sortBy": "name", + "sortOrder": "asc" } } jsonSchema: diff --git a/website/src/content/docs/protocol/responses/success.mdx b/website/src/content/docs/protocol/responses/success.mdx index 731a4ef8c..2df893774 100644 --- a/website/src/content/docs/protocol/responses/success.mdx +++ b/website/src/content/docs/protocol/responses/success.mdx @@ -39,8 +39,8 @@ created: typeSpec: file: path: "lib/core/lib/core/responses/success.tsp" - startLine: 239 - endLine: 244 + startLine: 316 + endLine: 321 accepted: example: code: | @@ -58,8 +58,8 @@ accepted: typeSpec: file: path: "lib/core/lib/core/responses/success.tsp" - startLine: 268 - endLine: 273 + startLine: 347 + endLine: 352 success: example: file: