From 63248690854fdadaad7a45c5759bcad913b47fbd Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Fri, 4 Dec 2020 08:52:53 -0800 Subject: [PATCH 1/5] Add a new Always create accept flag --- modelerfour/modeler/modelerfour-options.ts | 16 +++++++++++++ modelerfour/modeler/modelerfour.ts | 27 ++++++---------------- 2 files changed, 23 insertions(+), 20 deletions(-) create mode 100644 modelerfour/modeler/modelerfour-options.ts diff --git a/modelerfour/modeler/modelerfour-options.ts b/modelerfour/modeler/modelerfour-options.ts new file mode 100644 index 0000000..fd1de2b --- /dev/null +++ b/modelerfour/modeler/modelerfour-options.ts @@ -0,0 +1,16 @@ +/** + * List of configuration that can be used with modelerfour. + */ +export interface ModelerFourOptions { + /** + * Flag to automatically add the Content-Type header to operations. + */ + "always-create-content-type-parameter"?: boolean; + + /** + * Flag to automatically add the Accept header to operations. + */ + "always-create-accept-parameter"?: boolean; + + "always-seal-x-ms-enums"?: boolean; +} diff --git a/modelerfour/modeler/modelerfour.ts b/modelerfour/modeler/modelerfour.ts index a321141..b9c1071 100644 --- a/modelerfour/modeler/modelerfour.ts +++ b/modelerfour/modeler/modelerfour.ts @@ -75,6 +75,7 @@ import { import { Session, Channel } from "@azure-tools/autorest-extension-base"; import { Interpretations, XMSEnum } from "./interpretations"; import { fail, minimum, pascalCase, knownMediaType, KnownMediaType } from "@azure-tools/codegen"; +import { ModelerFourOptions } from "./modelerfour-options"; /** adds only if the item is not in the collection already * @@ -145,7 +146,7 @@ export class ModelerFour { private schemaCache = new ProcessingCache((schema: OpenAPI.Schema, name: string) => this.processSchemaImpl(schema, name), ); - private options: Dictionary = {}; + private options: ModelerFourOptions = {}; private uniqueNames: Dictionary = {}; constructor(protected session: Session) { @@ -2078,7 +2079,7 @@ export class ModelerFour { // operation and add it to all requests. Before adding the header, // make sure there isn't an existing Accept parameter. const mediaTypes = Array.from(acceptTypes); - if (acceptTypes.size > 0) { + if (this.options["always-create-accept-parameter"] === true && acceptTypes.size > 0) { const acceptSchema = this.getAcceptParameterSchema(mediaTypes); if (!values(operation.parameters).first(isAcceptHeaderParam)) { for (const request of values(operation.requests)) { @@ -2144,7 +2145,7 @@ export class ModelerFour { } const kmtBinary = groupedMediaTypes.get(KnownMediaType.Binary); - + if (kmtBinary) { // handle binary this.processBinary(KnownMediaType.Binary, kmtBinary, operation, requestBody); @@ -2156,19 +2157,9 @@ export class ModelerFour { const kmtJSON = groupedMediaTypes.get(KnownMediaType.Json); if (kmtJSON) { if ([...kmtJSON.values()].find((x) => x.schema.instance && this.isSchemaBinary(x.schema.instance))) { - this.processBinary( - KnownMediaType.Binary, - kmtJSON, - operation, - requestBody - ); + this.processBinary(KnownMediaType.Binary, kmtJSON, operation, requestBody); } else { - this.processSerializedObject( - KnownMediaType.Json, - kmtJSON, - operation, - requestBody - ); + this.processSerializedObject(KnownMediaType.Json, kmtJSON, operation, requestBody); } } const kmtXML = groupedMediaTypes.get(KnownMediaType.Xml); @@ -2285,11 +2276,7 @@ export class ModelerFour { } private isSchemaBinary(schema: OpenAPI.Schema) { - return ( - schema.type === "file" || - schema.format === "file" || - schema.format === "binary" - ); + return schema.type === "file" || schema.format === "file" || schema.format === "binary"; } private propagateSchemaUsage(schema: Schema): void { From 274cb5bc948f2e32405d5a8a4f41faea122db37f Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Fri, 4 Dec 2020 09:13:46 -0800 Subject: [PATCH 2/5] Fix all options types --- modelerfour/checker/checker.ts | 3 +- modelerfour/flattener/flattener.ts | 3 +- modelerfour/grouper/grouper.ts | 6 ++-- modelerfour/modeler/modelerfour-options.ts | 38 ++++++++++++++++++++++ modelerfour/prenamer/prenamer.ts | 3 +- modelerfour/quality-precheck/prechecker.ts | 3 +- 6 files changed, 49 insertions(+), 7 deletions(-) diff --git a/modelerfour/checker/checker.ts b/modelerfour/checker/checker.ts index b20e78a..f9a199b 100644 --- a/modelerfour/checker/checker.ts +++ b/modelerfour/checker/checker.ts @@ -15,10 +15,11 @@ import { } from "@azure-tools/codemodel"; import { Session } from "@azure-tools/autorest-extension-base"; import { values, items, length, Dictionary, refCount, clone } from "@azure-tools/linq"; +import { ModelerFourOptions } from "../modeler/modelerfour-options"; export class Checker { codeModel: CodeModel; - options: Dictionary = {}; + options: ModelerFourOptions = {}; constructor(protected session: Session) { this.codeModel = session.model; // shadow(session.model, filename); diff --git a/modelerfour/flattener/flattener.ts b/modelerfour/flattener/flattener.ts index 3c544db..0fce0bb 100644 --- a/modelerfour/flattener/flattener.ts +++ b/modelerfour/flattener/flattener.ts @@ -15,6 +15,7 @@ import { } from "@azure-tools/codemodel"; import { Session } from "@azure-tools/autorest-extension-base"; import { values, items, length, Dictionary, refCount, clone } from "@azure-tools/linq"; +import { ModelerFourOptions } from "../modeler/modelerfour-options"; const xmsThreshold = "x-ms-payload-flattening-threshold"; const xmsFlatten = "x-ms-client-flatten"; @@ -23,7 +24,7 @@ const hasBeenFlattened = "x-ms-flattened"; export class Flattener { codeModel: CodeModel; - options: Dictionary = {}; + options: ModelerFourOptions = {}; threshold = 0; recursePayload = false; diff --git a/modelerfour/grouper/grouper.ts b/modelerfour/grouper/grouper.ts index 5a516fd..16c524f 100644 --- a/modelerfour/grouper/grouper.ts +++ b/modelerfour/grouper/grouper.ts @@ -18,14 +18,14 @@ import { import { Session } from "@azure-tools/autorest-extension-base"; import { values, items, length, Dictionary, refCount, clone } from "@azure-tools/linq"; import { pascalCase, camelCase } from "@azure-tools/codegen"; +import { ModelerFourOptions } from "../modeler/modelerfour-options"; const mergeReponseHeaders = "merge-response-headers"; -const enableParameterGrouping = "group-parameters"; const xmsParameterGrouping = "x-ms-parameter-grouping"; export class Grouper { codeModel: CodeModel; - options: Dictionary = {}; + options: ModelerFourOptions = {}; groups: Dictionary = {}; constructor(protected session: Session) { @@ -39,7 +39,7 @@ export class Grouper { } process() { - if (this.options[enableParameterGrouping] === true) { + if (this.options["group-parameters"] === true) { for (const group of this.codeModel.operationGroups) { for (const operation of group.operations) { for (const request of values(operation.requests)) { diff --git a/modelerfour/modeler/modelerfour-options.ts b/modelerfour/modeler/modelerfour-options.ts index fd1de2b..6e4d7a5 100644 --- a/modelerfour/modeler/modelerfour-options.ts +++ b/modelerfour/modeler/modelerfour-options.ts @@ -13,4 +13,42 @@ export interface ModelerFourOptions { "always-create-accept-parameter"?: boolean; "always-seal-x-ms-enums"?: boolean; + + "flatten-models"?: boolean; + + "flatten-payloads"?: boolean; + + "keep-unused-flattened-models"?: boolean; + + "multiple-request-parameter-flattening"?: boolean; + + "group-parameters"?: boolean; + + "additional-checks"?: boolean; + + "lenient-model-deduplication"?: boolean; + + "naming"?: ModelerFourNamingOptions; + + "prenamer"?: boolean; + + "resolve-schema-name-collisons"?: boolean; +} + +export interface ModelerFourNamingOptions { + "preserve-uppercase-max-length"?: number; + "parameter"?: string; + "property"?: string; + "operation"?: string; + "operationGroup"?: string; + "header"?: string; + "choice"?: string; + "choiceValue"?: string; + "constant"?: string; + "constantParameter"?: string; + "client"?: string; + "type"?: string; + "global"?: string; + "local"?: string; + "override"?: any; } diff --git a/modelerfour/prenamer/prenamer.ts b/modelerfour/prenamer/prenamer.ts index 78d0020..765b584 100644 --- a/modelerfour/prenamer/prenamer.ts +++ b/modelerfour/prenamer/prenamer.ts @@ -27,6 +27,7 @@ import { Styler, pascalCase, } from "@azure-tools/codegen"; +import { ModelerFourOptions } from "../modeler/modelerfour-options"; function getNameOptions(typeName: string, components: Array) { const result = new Set(); @@ -133,7 +134,7 @@ function deduplicateSchemaName( export class PreNamer { codeModel: CodeModel; - options: Dictionary = {}; + options: ModelerFourOptions = {}; format = { parameter: Style.camel, property: Style.camel, diff --git a/modelerfour/quality-precheck/prechecker.ts b/modelerfour/quality-precheck/prechecker.ts index 4405693..4b44a78 100644 --- a/modelerfour/quality-precheck/prechecker.ts +++ b/modelerfour/quality-precheck/prechecker.ts @@ -16,6 +16,7 @@ import { Host, startSession } from "@azure-tools/autorest-extension-base"; import { Interpretations } from "../modeler/interpretations"; import { getDiff } from "recursive-diff"; +import { ModelerFourOptions } from "../modeler/modelerfour-options"; export async function processRequest(host: Host) { const debug = (await host.GetValue("debug")) || false; @@ -57,7 +58,7 @@ export async function processRequest(host: Host) { export class QualityPreChecker { input: oai3; - options: Dictionary = {}; + options: ModelerFourOptions = {}; protected interpret: Interpretations; constructor(protected session: Session) { From af054eb2a8ef39a25c90bba1d4ad7f407b4242c9 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Fri, 4 Dec 2020 09:21:12 -0800 Subject: [PATCH 3/5] Fix tests --- modelerfour/test/test-modeler.ts | 1 + modelerfour/test/unit/modelerfour.test.ts | 40 +++++++++++++---------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/modelerfour/test/test-modeler.ts b/modelerfour/test/test-modeler.ts index 3d2d569..b644530 100644 --- a/modelerfour/test/test-modeler.ts +++ b/modelerfour/test/test-modeler.ts @@ -263,6 +263,7 @@ class Process { "group-parameters": true, "resolve-schema-name-collisons": true, "additional-checks": true, + "always-create-accept-parameter": true, //'always-create-content-type-parameter': true, "naming": { override: { diff --git a/modelerfour/test/unit/modelerfour.test.ts b/modelerfour/test/unit/modelerfour.test.ts index 59401b2..53c3356 100644 --- a/modelerfour/test/unit/modelerfour.test.ts +++ b/modelerfour/test/unit/modelerfour.test.ts @@ -15,29 +15,32 @@ import { responses, } from "./unitTestUtil"; import { CodeModel, Parameter, SchemaResponse, ConstantSchema, SealedChoiceSchema } from "@azure-tools/codemodel"; -import { ParameterLocation } from "@azure-tools/openapi"; - -const cfg = { - "modelerfour": { - "flatten-models": true, - "flatten-payloads": true, - "group-parameters": true, - "resolve-schema-name-collisons": true, - "additional-checks": true, - //'always-create-content-type-parameter': true, - "naming": { - override: { - $host: "$host", - cmyk: "CMYK", - }, - local: "_ + camel", - constantParameter: "pascal", +import { ModelerFourOptions } from "../../modeler/modelerfour-options"; + +const modelerfourOptions: ModelerFourOptions = { + "flatten-models": true, + "flatten-payloads": true, + "group-parameters": true, + "resolve-schema-name-collisons": true, + "additional-checks": true, + "always-create-accept-parameter": true, + //'always-create-content-type-parameter': true, + "naming": { + override: { + $host: "$host", + cmyk: "CMYK", }, + local: "_ + camel", + constantParameter: "pascal", }, +}; + +const cfg = { + "modelerfour": modelerfourOptions, "payload-flattening-threshold": 2, }; -async function runModeler(spec: any, config: any = cfg): Promise { +async function runModeler(spec: any, config: { modelerfour: ModelerFourOptions } = cfg): Promise { const modelerErrors: Array = []; const session = await createTestSession(config, spec, modelerErrors); const modeler = await new ModelerFour(session).init(); @@ -1065,6 +1068,7 @@ class Modeler { const codeModel = await runModeler(spec, { modelerfour: { "always-create-content-type-parameter": true, + "always-create-accept-parameter": true, }, }); From 574d63f533a7e98ec884398edce0bef39b43ef3d Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Fri, 4 Dec 2020 10:30:31 -0800 Subject: [PATCH 4/5] Change default not to be breaking --- modelerfour/readme.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modelerfour/readme.md b/modelerfour/readme.md index ea791a0..74a016b 100644 --- a/modelerfour/readme.md +++ b/modelerfour/readme.md @@ -179,6 +179,9 @@ modelerfour: # when it's only one possible value, make it a constant. always-create-content-type-parameter: true + # always create the Accept parameter + always-create-accept-parameter: true + # always create SealedChoiceSchema for x-ms-enum schemas no matter # what the settings are. This can be used to smooth migration from # remodeler to modelerfour. From ca93afad444f7e703b8abe593f1c3355a7be23bc Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Fri, 4 Dec 2020 11:49:19 -0800 Subject: [PATCH 5/5] Update config --- modelerfour/modeler/modelerfour.ts | 4 ++-- modelerfour/readme.md | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/modelerfour/modeler/modelerfour.ts b/modelerfour/modeler/modelerfour.ts index b9c1071..b30615c 100644 --- a/modelerfour/modeler/modelerfour.ts +++ b/modelerfour/modeler/modelerfour.ts @@ -242,7 +242,6 @@ export class ModelerFour { async init() { this.options = await this.session.getValue("modelerfour", {}); - // grab override-client-name const newTitle = await this.session.getValue("override-client-name", ""); if (newTitle) { @@ -1361,7 +1360,8 @@ export class ModelerFour { http, }, }); - + this.session.log(`Options ${JSON.stringify(this.options)}`, {}); + this.session.log(`Accept-param ${this.options["always-create-accept-parameter"]}`, {}); if (this.options[`always-create-content-type-parameter`] === true || http.mediaTypes.length > 1) { const scs = this.getContentTypeParameterSchema(http); diff --git a/modelerfour/readme.md b/modelerfour/readme.md index 74a016b..7c82fd5 100644 --- a/modelerfour/readme.md +++ b/modelerfour/readme.md @@ -177,10 +177,10 @@ modelerfour: # always create the content-type parameter for binary requests # when it's only one possible value, make it a constant. - always-create-content-type-parameter: true + always-create-content-type-parameter: false|true - # always create the Accept parameter - always-create-accept-parameter: true + # always create the Accept parameter + always-create-accept-parameter: true|false # always create SealedChoiceSchema for x-ms-enum schemas no matter # what the settings are. This can be used to smooth migration from @@ -218,6 +218,11 @@ modelerfour: ``` ~~~ +Default options: +```yaml +modelerfour: + always-create-accept-parameter: true +``` #### ModelerFour