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

Commit

Permalink
Respect required attribute for request body (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeguerin authored Jan 25, 2021
1 parent ff8015f commit 20ca12f
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 38 deletions.
3 changes: 2 additions & 1 deletion modelerfour/src/modeler/modelerfour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1402,7 +1402,7 @@ export class ModelerFour {
}),
},
implementation: ImplementationLocation.Method,
required: true,
required: body.instance?.required,
nullable: requestSchema?.instance?.nullable,
clientDefaultValue: this.interpret.getClientDefault(body?.instance || {}, {}),
}),
Expand Down Expand Up @@ -1862,6 +1862,7 @@ export class ModelerFour {
.select((each) => dereference(this.input, each))
.select((pp) => {
const parameter = pp.instance;

this.use(parameter.schema, (name, schema) => {
if (this.apiVersionMode !== "none" && this.interpret.isApiVersionParameter(parameter)) {
return this.processApiVersionParameter(parameter, operation, pathItem);
Expand Down
37 changes: 37 additions & 0 deletions modelerfour/test/modeler/modelerfour-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { CodeModel } from "@azure-tools/codemodel";
import { Model } from "@azure-tools/openapi";
import { ModelerFour } from "../../src/modeler/modelerfour";
import { ModelerFourOptions } from "modeler/modelerfour-options";
import { createTestSessionFromModel } from "../utils";

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,
};

export async function runModeler(spec: any, config: { modelerfour: ModelerFourOptions } = cfg): Promise<CodeModel> {
const { session, errors } = await createTestSessionFromModel<Model>(config, spec);
const modeler = await new ModelerFour(session).init();

expect(errors.length).toBe(0);

return modeler.process();
}
52 changes: 52 additions & 0 deletions modelerfour/test/modeler/modelerfour.requests.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { addOperation, createTestSpec } from "../utils";
import { runModeler } from "./modelerfour-utils";

describe("Modelerfour.Request", () => {
describe("Body", () => {
describe("Required attribute", () => {
const runModelerWithBody = async (body: any) => {
const spec = createTestSpec();

addOperation(spec, "/test", {
post: {
requestBody: {
...body,
},
},
});

const codeModel = await runModeler(spec);
const parameter = codeModel.operationGroups[0]?.operations[0]?.requests?.[0]?.parameters?.[0];

expect(parameter).not.toBeNull();
return parameter;
};

const defaultBody = {
content: {
"application/octet-stream": {
schema: {
type: "object",
format: "file",
},
},
},
};

it("mark body as required if required: true", async () => {
const parameter = await runModelerWithBody({ ...defaultBody, required: true });
expect(parameter?.required).toBe(true);
});

it("mark body as not required if required: false", async () => {
const parameter = await runModelerWithBody({ ...defaultBody, required: true });
expect(parameter?.required).toBe(true);
});

it("mark body as not required by default", async () => {
const parameter = await runModelerWithBody(defaultBody);
expect(parameter?.required).toBe(undefined);
});
});
});
});
39 changes: 2 additions & 37 deletions modelerfour/test/modeler/modelerfour.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,16 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ModelerFour } from "../../src/modeler/modelerfour";
import { CodeModel, Parameter, SchemaResponse, ConstantSchema, SealedChoiceSchema } from "@azure-tools/codemodel";
import { ModelerFourOptions } from "../../src/modeler/modelerfour-options";
import { Parameter, SchemaResponse, ConstantSchema, SealedChoiceSchema } from "@azure-tools/codemodel";
import {
addOperation,
addSchema,
createTestSessionFromModel,
createTestSpec,
InitialTestSpec,
response,
responses,
} from "../utils";
import { Model } from "@azure-tools/openapi";

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: { modelerfour: ModelerFourOptions } = cfg): Promise<CodeModel> {
const { session, errors } = await createTestSessionFromModel<Model>(config, spec);
const modeler = await new ModelerFour(session).init();

expect(errors.length).toBe(0);

return modeler.process();
}
import { runModeler } from "./modelerfour-utils";

export function findByName<T>(name: string, items: Array<T> | undefined): T | undefined {
return (items && items.find((i) => (<any>i).language.default.name === name)) || undefined;
Expand Down

0 comments on commit 20ca12f

Please sign in to comment.