This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Respect required attribute for request body (#385)
- Loading branch information
1 parent
ff8015f
commit 20ca12f
Showing
4 changed files
with
93 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters