Skip to content

Commit

Permalink
Fix collectionFormat issue in header (#1878)
Browse files Browse the repository at this point in the history
* Add helper method for header csv format

* Update the test cases in swagger

* Update the swagger side change

* Revert changes

* Revert changes

* Update the generated code
  • Loading branch information
MaryGao authored Jun 6, 2023
1 parent 6e2e955 commit 46674e7
Show file tree
Hide file tree
Showing 23 changed files with 136 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,4 @@ function getAllOperations(model: CodeModel): Operation[] {
}

return operations;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

export function buildMultiCollection(
queryParameters: string[],
parameterName: string
) {
return queryParameters
export function buildMultiCollection(items: string[], parameterName: string) {
return items
.map((item, index) => {
if (index === 0) {
return item;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

export function buildPipeCollection(queryParameters: string[]): string {
return queryParameters.join("|");
export function buildPipeCollection(items: string[]): string {
return items.join("|");
}

export function buildSsvCollection(queryParameters: string[]): string {
return queryParameters.join(" ");
export function buildSsvCollection(items: string[]): string {
return items.join(" ");
}

export function buildTsvCollection(queryParameters: string[]) {
return queryParameters.join("\t");
export function buildTsvCollection(items: string[]) {
return items.join("\t");
}
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ export interface BoundaryOverlapResponseOutput {
export type BoundaryResourceMergeAndPatch = Partial<Boundary>;

// @public (undocumented)
export function buildMultiCollection(queryParameters: string[], parameterName: string): string;
export function buildMultiCollection(items: string[], parameterName: string): string;

// @public
export interface CascadeDeleteJobOutput {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

export function buildMultiCollection(
queryParameters: string[],
parameterName: string
) {
return queryParameters
export function buildMultiCollection(items: string[], parameterName: string) {
return items
.map((item, index) => {
if (index === 0) {
return item;
Expand Down
4 changes: 3 additions & 1 deletion packages/rlc-common/src/buildIndexFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { Project, SourceFile } from "ts-morph";
import { NameType, normalizeName } from "./helpers/nameUtils.js";
import {
hasCsvCollection,
hasInputModels,
hasMultiCollection,
hasOutputModels,
Expand Down Expand Up @@ -134,7 +135,8 @@ function generateRLCIndexForMultiClient(file: SourceFile, model: RLCModel) {
hasMultiCollection(model) ||
hasSsvCollection(model) ||
hasPipeCollection(model) ||
hasTsvCollection(model)
hasTsvCollection(model) ||
hasCsvCollection(model)
) {
file.addImportDeclaration({
namespaceImport: "SerializeHelper",
Expand Down
5 changes: 5 additions & 0 deletions packages/rlc-common/src/buildSerializeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import * as path from "path";
// @ts-ignore: to fix the handlebars issue
import hbs from "handlebars";
import {
hasCsvCollection,
hasMultiCollection,
hasPipeCollection,
hasSsvCollection,
hasTsvCollection
} from "./helpers/operationHelpers.js";
import {
buildCsvCollectionContent,
buildMultiCollectionContent,
buildPipeCollectionContent,
buildSsvCollectionContent,
Expand All @@ -29,6 +31,9 @@ export function buildSerializeHelper(model: RLCModel) {
if (hasTsvCollection(model)) {
serializeHelperContent += "\n" + buildTsvCollectionContent;
}
if (hasCsvCollection(model)) {
serializeHelperContent += "\n" + buildCsvCollectionContent;
}
if (serializeHelperContent !== "") {
const readmeFileContents = hbs.compile(serializeHelperContent, {
noEscape: true
Expand Down
4 changes: 4 additions & 0 deletions packages/rlc-common/src/helpers/operationHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ export function hasTsvCollection(model: RLCModel) {
return Boolean(model.helperDetails?.hasTsvCollection);
}

export function hasCsvCollection(model: RLCModel) {
return Boolean(model.helperDetails?.hasCsvCollection);
}

export function hasUnexpectedHelper(model: RLCModel) {
const pathDictionary = model.paths;
for (const details of Object.values(pathDictionary)) {
Expand Down
1 change: 1 addition & 0 deletions packages/rlc-common/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface HelperFunctionDetails {
hasPipeCollection?: boolean;
hasSsvCollection?: boolean;
hasTsvCollection?: boolean;
hasCsvCollection?: boolean;
}

export interface PagingDetails {
Expand Down
21 changes: 13 additions & 8 deletions packages/rlc-common/src/static/serializeHelper.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export const buildMultiCollectionContent = `
export function buildMultiCollection(
queryParameters: string[],
items: string[],
parameterName: string
) {
return queryParameters
return items
.map((item, index) => {
if (index === 0) {
return item;
Expand All @@ -14,16 +14,21 @@ export function buildMultiCollection(
}`;

export const buildPipeCollectionContent = `
export function buildPipeCollection(queryParameters: string[]): string {
return queryParameters.join("|");
export function buildPipeCollection(items: string[]): string {
return items.join("|");
}`;

export const buildSsvCollectionContent = `
export function buildSsvCollection(queryParameters: string[]): string {
return queryParameters.join(" ");
export function buildSsvCollection(items: string[]): string {
return items.join(" ");
}`;

export const buildTsvCollectionContent = `
export function buildTsvCollection(queryParameters: string[]) {
return queryParameters.join("\\t");
export function buildTsvCollection(items: string[]) {
return items.join("\\t");
}`;

export const buildCsvCollectionContent = `
export function buildCsvCollection(items: string[]) {
return items.join(",");
}`;
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { RequestParameters } from '@azure-rest/core-client';
import { StreamableMethod } from '@azure-rest/core-client';

// @public (undocumented)
export function buildMultiCollection(queryParameters: string[], parameterName: string): string;
export function buildMultiCollection(items: string[], parameterName: string): string;

// @public (undocumented)
export type CollectionFormatTestServiceClient = Client & {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

export function buildMultiCollection(
queryParameters: string[],
parameterName: string
) {
return queryParameters
export function buildMultiCollection(items: string[], parameterName: string) {
return items
.map((item, index) => {
if (index === 0) {
return item;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export interface BreakSentenceQueryParamProperties {
}

// @public (undocumented)
export function buildMultiCollection(queryParameters: string[], parameterName: string): string;
export function buildMultiCollection(items: string[], parameterName: string): string;

// @public
export interface CommonScriptModelOutput {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

export function buildMultiCollection(
queryParameters: string[],
parameterName: string
) {
return queryParameters
export function buildMultiCollection(items: string[], parameterName: string) {
return items
.map((item, index) => {
if (index === 0) {
return item;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ function extractSpecialSerializeInfo(
let hasPipeCollection = false;
let hasTsvCollection = false;
let hasSsvCollection = false;
let hasCsvCollection = false;
const operationGroups = listOperationGroups(dpgContext, client);
for (const operationGroup of operationGroups) {
const operations = listOperationsInOperationGroup(
Expand All @@ -216,6 +217,9 @@ function extractSpecialSerializeInfo(
hasSsvCollection = hasSsvCollection
? hasSsvCollection
: serializeInfo.hasSsvCollection;
hasCsvCollection = hasCsvCollection
? hasCsvCollection
: serializeInfo.hasCsvCollection;
});
}
}
Expand All @@ -236,12 +240,16 @@ function extractSpecialSerializeInfo(
hasSsvCollection = hasSsvCollection
? hasSsvCollection
: serializeInfo.hasSsvCollection;
hasCsvCollection = hasCsvCollection
? hasCsvCollection
: serializeInfo.hasCsvCollection;
});
}
return {
hasMultiCollection,
hasPipeCollection,
hasTsvCollection,
hasSsvCollection
hasSsvCollection,
hasCsvCollection
};
}
11 changes: 10 additions & 1 deletion packages/typespec-ts/src/transform/transformParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ function getParameterMetadata(
serializeInfo.hasMultiCollection ||
serializeInfo.hasPipeCollection ||
serializeInfo.hasSsvCollection ||
serializeInfo.hasTsvCollection
serializeInfo.hasTsvCollection ||
serializeInfo.hasCsvCollection
) {
type = "string";
description += ` This parameter needs to be formatted as ${serializeInfo.collectionInfo.join(
Expand Down Expand Up @@ -495,6 +496,7 @@ export function getSpecialSerializeInfo(parameter: HttpOperationParameter) {
let hasPipeCollection = false;
let hasSsvCollection = false;
let hasTsvCollection = false;
let hasCsvCollection = false;
const descriptions = [];
const collectionInfo = [];
if (
Expand Down Expand Up @@ -522,11 +524,18 @@ export function getSpecialSerializeInfo(parameter: HttpOperationParameter) {
descriptions.push("buildPipeCollection");
collectionInfo.push("pipe");
}

if (parameter.type === "header" && (parameter as any).format === "csv") {
hasCsvCollection = true;
descriptions.push("buildCsvCollection");
collectionInfo.push("csv");
}
return {
hasMultiCollection,
hasPipeCollection,
hasSsvCollection,
hasTsvCollection,
hasCsvCollection,
descriptions,
collectionInfo
};
Expand Down
Loading

0 comments on commit 46674e7

Please sign in to comment.