Skip to content
This repository has been archived by the owner on Oct 11, 2023. It is now read-only.

Add a new Always create accept flag #366

Merged
merged 6 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion modelerfour/checker/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> = {};
options: ModelerFourOptions = {};

constructor(protected session: Session<CodeModel>) {
this.codeModel = session.model; // shadow(session.model, filename);
Expand Down
3 changes: 2 additions & 1 deletion modelerfour/flattener/flattener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -23,7 +24,7 @@ const hasBeenFlattened = "x-ms-flattened";

export class Flattener {
codeModel: CodeModel;
options: Dictionary<any> = {};
options: ModelerFourOptions = {};
threshold = 0;
recursePayload = false;

Expand Down
6 changes: 3 additions & 3 deletions modelerfour/grouper/grouper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> = {};
options: ModelerFourOptions = {};
groups: Dictionary<GroupSchema> = {};

constructor(protected session: Session<CodeModel>) {
Expand All @@ -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)) {
Expand Down
54 changes: 54 additions & 0 deletions modelerfour/modeler/modelerfour-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* 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;

"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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

"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;
}
9 changes: 5 additions & 4 deletions modelerfour/modeler/modelerfour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -145,7 +146,7 @@ export class ModelerFour {
private schemaCache = new ProcessingCache((schema: OpenAPI.Schema, name: string) =>
this.processSchemaImpl(schema, name),
);
private options: Dictionary<any> = {};
private options: ModelerFourOptions = {};
private uniqueNames: Dictionary<any> = {};

constructor(protected session: Session<oai3>) {
Expand Down Expand Up @@ -241,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) {
Expand Down Expand Up @@ -1360,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);

Expand Down Expand Up @@ -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)) {
Expand Down
3 changes: 2 additions & 1 deletion modelerfour/prenamer/prenamer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
Styler,
pascalCase,
} from "@azure-tools/codegen";
import { ModelerFourOptions } from "../modeler/modelerfour-options";

function getNameOptions(typeName: string, components: Array<string>) {
const result = new Set<string>();
Expand Down Expand Up @@ -133,7 +134,7 @@ function deduplicateSchemaName(

export class PreNamer {
codeModel: CodeModel;
options: Dictionary<any> = {};
options: ModelerFourOptions = {};
format = {
parameter: Style.camel,
property: Style.camel,
Expand Down
3 changes: 2 additions & 1 deletion modelerfour/quality-precheck/prechecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,7 +58,7 @@ export async function processRequest(host: Host) {

export class QualityPreChecker {
input: oai3;
options: Dictionary<any> = {};
options: ModelerFourOptions = {};
protected interpret: Interpretations;

constructor(protected session: Session<oai3>) {
Expand Down
10 changes: 9 additions & 1 deletion modelerfour/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +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|false

# always create SealedChoiceSchema for x-ms-enum schemas no matter
# what the settings are. This can be used to smooth migration from
Expand Down Expand Up @@ -215,6 +218,11 @@ modelerfour:
```
~~~

Default options:
```yaml
modelerfour:
always-create-accept-parameter: true
```

#### ModelerFour

Expand Down
1 change: 1 addition & 0 deletions modelerfour/test/test-modeler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
40 changes: 22 additions & 18 deletions modelerfour/test/unit/modelerfour.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CodeModel> {
async function runModeler(spec: any, config: { modelerfour: ModelerFourOptions } = cfg): Promise<CodeModel> {
const modelerErrors: Array<any> = [];
const session = await createTestSession(config, spec, modelerErrors);
const modeler = await new ModelerFour(session).init();
Expand Down Expand Up @@ -1065,6 +1068,7 @@ class Modeler {
const codeModel = await runModeler(spec, {
modelerfour: {
"always-create-content-type-parameter": true,
"always-create-accept-parameter": true,
},
});

Expand Down