diff --git a/sdk/storage/Azure.Storage.Common/swagger/Generator/readme.md b/sdk/storage/Azure.Storage.Common/swagger/Generator/readme.md index 00cc71308d6b9..9ebd7746a365c 100644 --- a/sdk/storage/Azure.Storage.Common/swagger/Generator/readme.md +++ b/sdk/storage/Azure.Storage.Common/swagger/Generator/readme.md @@ -8,6 +8,7 @@ We support a number of extensions including using the vendor prefix `x-az-`: - `x-az-response-schema-name`: If we combine headers and a schema into a response type, this allows you to provide the name of the scehma property. The default value is `Body`. - `x-az-create-exception`: `true` denotes that this partial type will provide an implementation of the method `Exception CreateException()` to turn an error object into something that can be thrown. The default value is `false`. - `x-az-trace`: Indicates whether a parameter will be logged as part of our distributed tracing. The default value is `false`. +- `x-az-enum-skip-value`: The name of a default value to skip while serializing. - `x-az-disable-warnings`: Wraps a declaration in a `#pragma disable` when specified. - `x-az-skip-path-components`: Whether to skip any path components and always assume a fully formed URL to the resource (this currently must be set to `true`). - `x-az-include-sync-methods`: Whether to generate support for sync methods. The default value is `false` (this flag should go away soon and always be `true`). diff --git a/sdk/storage/Azure.Storage.Common/swagger/Generator/src/generator.ts b/sdk/storage/Azure.Storage.Common/swagger/Generator/src/generator.ts index 39f2701866a50..bcb0ed27e7295 100644 --- a/sdk/storage/Azure.Storage.Common/swagger/Generator/src/generator.ts +++ b/sdk/storage/Azure.Storage.Common/swagger/Generator/src/generator.ts @@ -258,8 +258,17 @@ function generateOperation(w: IndentWriter, serviceModel: IServiceModel, group: const useParameter = (param: IParameter, use: ((value: string) => void)) => { const constant = isEnumType(param.model) && param.model.constant; const nullable = !constant && !param.required; + const skipValue = isEnumType(param.model) ? param.model.skipValue : undefined; const name = naming.variable(param.clientName); - if (nullable) { w.write(`if (${name} != null) {`); } + if (nullable) { + w.write(`if (${name} != null) {`); + } else if (skipValue) { + const value = (param.model).values.find(v => v.name == skipValue || v.value == skipValue); + if (!value) { throw `Cannot find a value for x-az-enum-skip-value ${skipValue} in ${types.getName(param.model)}`; } + const skipValueName = naming.enumField(value.name || value.value); + w.write(`if (${name} != ${types.getName(param.model)}.${skipValueName}) {`); + } + const indent = !!(nullable || skipValue); if (constant) { use(`"${((param.model).values[0].value || '').toString()}"`); } else if (param.model.type === 'dictionary') { @@ -280,14 +289,14 @@ function generateOperation(w: IndentWriter, serviceModel: IServiceModel, group: if (param.model.type === `boolean`) { w.line(); w.line(`#pragma warning disable CA1308 // Normalize strings to uppercase`); - } else if (nullable) { w.write(` `); } + } else if (indent) { w.write(` `); } use(types.convertToString(name, param.model, service, param.required)); if (param.model.type === `boolean`) { w.line(); w.line(`#pragma warning restore CA1308 // Normalize strings to uppercase`); - } else if (nullable) { w.write(` `); } + } else if (indent) { w.write(` `); } } - if (nullable) { w.write(`}`); } + if (indent) { w.write(`}`); } w.line(); }; @@ -650,7 +659,7 @@ function generateEnum(w: IndentWriter, model: IServiceModel, type: IEnumType) { // Write the values for (const value of type.values) { w.write(`case ${types.getName(type)}.${naming.enumField(value.name || value.value)}:`); - w.scope(() => w.line(`return "${value.value}";`)); + w.scope(() => w.line(`return ${value.value == null ? 'null' : '"' + value.value + '"'};`)); } // Throw for random values w.write(`default:`); @@ -665,7 +674,7 @@ function generateEnum(w: IndentWriter, model: IServiceModel, type: IEnumType) { w.scope('{', '}', () => { // Write the values for (const value of type.values) { - w.write(`case "${value.value}":`); + w.write(`case ${value.value == null ? 'null' : '"' + value.value + '"'}:`); w.scope(() => w.line(`return ${types.getName(type)}.${naming.enumField(value.name || value.value)};`)); } // Throw for random values diff --git a/sdk/storage/Azure.Storage.Common/swagger/Generator/src/models.ts b/sdk/storage/Azure.Storage.Common/swagger/Generator/src/models.ts index a5e7e2f2e9e04..6e90875ba6586 100644 --- a/sdk/storage/Azure.Storage.Common/swagger/Generator/src/models.ts +++ b/sdk/storage/Azure.Storage.Common/swagger/Generator/src/models.ts @@ -168,6 +168,7 @@ export interface IEnumType extends IModelType { customSerialization: boolean, constant: boolean, public: boolean, + skipValue?: string, values: IEnumValue[] } diff --git a/sdk/storage/Azure.Storage.Common/swagger/Generator/src/serviceModel.ts b/sdk/storage/Azure.Storage.Common/swagger/Generator/src/serviceModel.ts index dfe7ee87e4a71..f0dab498df459 100644 --- a/sdk/storage/Azure.Storage.Common/swagger/Generator/src/serviceModel.ts +++ b/sdk/storage/Azure.Storage.Common/swagger/Generator/src/serviceModel.ts @@ -481,6 +481,7 @@ function createEnumType(project: IProject, name: string, swagger: any, location: constant: false, customSerialization, public: isPublic, + skipValue: swagger[`x-az-enum-skip-value`], values, extendedHeaders: [] };