Skip to content

Commit

Permalink
Merge pull request #614 from chradek/test-time-format
Browse files Browse the repository at this point in the history
[v6] add support for TimeSchema
  • Loading branch information
chradek authored Apr 10, 2020
2 parents b0bfcf7 + 35ab4ad commit ac5dbbd
Show file tree
Hide file tree
Showing 29 changed files with 677 additions and 98 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ More information about these can be found [here](https://github.com/Azure/autore
3. Hook up plugins into the AutoRest pipeline DAG, e.g.

```yaml
version: 3.0.6246
version: 3.0.6258
use-extension:
"@autorest/modelerfour": "4.12.277"
"@autorest/modelerfour": "4.12.301"

modelerfour:
# this runs a pre-namer step to clean up names
Expand Down
86 changes: 46 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
],
"dependencies": {
"@azure-tools/async-io": "3.0.203",
"@azure-tools/autorest-extension-base": "3.1.237",
"@azure-tools/codegen": "2.4.259",
"@azure-tools/codemodel": "3.4.302",
"@azure-tools/autorest-extension-base": "3.1.246",
"@azure-tools/codegen": "2.4.267",
"@azure-tools/codemodel": "4.13.317",
"@azure-tools/linq": "3.1.206",
"@azure-tools/openapi": "3.0.209",
"@azure/core-http": "^1.0.4",
Expand All @@ -43,7 +43,7 @@
"devDependencies": {
"@autorest/test-server": "^3.0.27",
"@azure/abort-controller": "^1.0.1",
"@microsoft.azure/autorest.testserver": "^2.10.14",
"@microsoft.azure/autorest.testserver": "^2.10.26",
"@types/chai": "^4.2.8",
"@types/express": "^4.17.2",
"@types/js-yaml": "3.12.1",
Expand Down
23 changes: 23 additions & 0 deletions src/transforms/mapperTransforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export function transformMapper({ schema, options }: MapperInput) {
transformNumberMapper,
transformConstantMapper,
transformDateMapper,
transformTimeMapper,
transformChoiceMapper,
transformPrimitiveMapper,
transformByteArrayMapper,
Expand Down Expand Up @@ -483,6 +484,26 @@ function transformDateMapper(pipelineValue: PipelineValue) {
};
}

function transformTimeMapper(pipelineValue: PipelineValue) {
const { schema, options } = pipelineValue;

if (!isSchemaType([SchemaType.Time], schema)) {
return pipelineValue;
}

const mapper = buildMapper(
schema,
{ name: getMapperTypeFromSchema(schema.type) },
options
);

return {
schema,
mapper,
isHandled: true
};
}

function transformStringMapper(pipelineValue: PipelineValue) {
const { schema, options } = pipelineValue;

Expand Down Expand Up @@ -682,6 +703,8 @@ export function getMapperTypeFromSchema(type: SchemaType, format?: string) {
return MapperType.UnixTime;
case SchemaType.Date:
return MapperType.Date;
case SchemaType.Time:
return MapperType.String;
case SchemaType.Dictionary:
return MapperType.Dictionary;
case SchemaType.Integer:
Expand Down
16 changes: 14 additions & 2 deletions src/transforms/parameterTransforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ export function populateOperationParameters(
targetMediaType?: KnownMediaType
): void {
const parameterSerializedName = getParameterName(parameter);
const description = getLanguageMetadata(parameter.language).description;
let description =
getLanguageMetadata(parameter.language).description ||
getLanguageMetadata(parameter.schema.language).description;

if (!parameterSerializedName) {
throw new Error(
Expand All @@ -202,6 +204,10 @@ export function populateOperationParameters(

const sameNameParams = operationParameters.filter(p => p.name === name);

if (parameter.schema.type === SchemaType.Time) {
description += `\nThis value should be an ISO-8601 formatted string representing time. E.g. "HH:MM:SS" or "HH:MM:SS.mm".`;
}

if (!sameNameParams.length) {
const collectionFormat = getCollectionFormat(parameter);
const typeDetails = getTypeForSchema(parameter.schema);
Expand Down Expand Up @@ -380,7 +386,13 @@ export function disambiguateParameter(
const name = normalizeName(serializedName, NameType.Property);
const nameRef = `${name}${sameNameParams.length}`;
const collectionFormat = getCollectionFormat(parameter);
const description = getLanguageMetadata(parameter.language).description;
let description =
getLanguageMetadata(parameter.language).description ||
getLanguageMetadata(parameter.schema.language).description;

if (parameter.schema.type === SchemaType.Time) {
description += `\nThis value should be an ISO-8601 formatted string representing time. E.g. "HH:MM:SS" or "HH:MM:SS.mm".`;
}
const typeDetails = getTypeForSchema(parameter.schema);

operationParameters.push({
Expand Down
1 change: 1 addition & 0 deletions src/utils/schemaHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export function getTypeForSchema(schema: Schema): TypeDetails {
usedModels.push(typeName);
break;
case SchemaType.String:
case SchemaType.Time:
case SchemaType.Uuid:
typeName = "string";
break;
Expand Down
37 changes: 37 additions & 0 deletions test/integration/bodyTime.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { assert } from "chai";
import { BodyTimeClient } from "./generated/bodyTime/src/bodyTimeClient";
import { TimeGetResponse } from "./generated/bodyTime/src/models";

/**
* Returns an interface that omits the _response field.
* This is useful when doing assert.deepEqual since _response
* is not enumerable, but deepEqual still does type checking for it.
*/
type RemoveResponse<T> = Omit<T, "_response">;

describe("BodyTimeClient", () => {
let client: BodyTimeClient;

beforeEach(() => {
client = new BodyTimeClient();
});

describe("#get", () => {
it("returns time as a string", async () => {
const result = await client.time.get();

assert.equal(result._response.status, 200, "Unexpected status code.");
assert.deepEqual(result as RemoveResponse<TimeGetResponse>, {
body: "11:34:56"
});
});
});

describe("#put", () => {
it("puts time as a string", async () => {
const result = await client.time.put("08:07:56");

assert.equal(result._response.status, 200, "Unexpected status code.");
});
});
});
Loading

0 comments on commit ac5dbbd

Please sign in to comment.