Skip to content

Commit

Permalink
Add x-az-enum-skip-value to ignore default enum values (#7426)
Browse files Browse the repository at this point in the history
  • Loading branch information
tg-msft authored Aug 29, 2019
1 parent e907f64 commit 8d6d454
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (<IEnumType>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(`"${((<IEnumType>param.model).values[0].value || '').toString()}"`);
} else if (param.model.type === 'dictionary') {
Expand All @@ -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();
};

Expand Down Expand Up @@ -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:`);
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export interface IEnumType extends IModelType {
customSerialization: boolean,
constant: boolean,
public: boolean,
skipValue?: string,
values: IEnumValue[]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: []
};
Expand Down

0 comments on commit 8d6d454

Please sign in to comment.