Skip to content

Commit

Permalink
[OpenAPI to TypeSpec] - Support primitive types as array elements (#4738
Browse files Browse the repository at this point in the history
)
  • Loading branch information
joheredi committed May 26, 2023
1 parent 07c2b97 commit 0354d7f
Show file tree
Hide file tree
Showing 56 changed files with 2,222 additions and 728 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@autorest/openapi-to-cadl",
"comment": "Fix pagination issue and update to compiler 0.44.0",
"type": "minor"
}
],
"packageName": "@autorest/openapi-to-cadl"
}
269 changes: 163 additions & 106 deletions common/config/rush/pnpm-lock.yaml

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions packages/extensions/openapi-to-cadl/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@autorest/openapi-to-cadl",
"version": "0.4.0",
"version": "0.4.1",
"description": "Autorest plugin to scaffold a Typespec definition from an OpenAPI document",
"main": "dist/main.js",
"typings": "dist/main.d.ts",
Expand Down Expand Up @@ -41,15 +41,15 @@
"@azure-tools/codegen": "~2.9.2",
"@autorest/extension-base": "~3.5.2",
"@autorest/codemodel": "~4.19.3",
"@typespec/compiler": "^0.41.0",
"@typespec/rest": "^0.41.0",
"@typespec/http": "^0.41.0",
"@typespec/versioning": "^0.41.0",
"@typespec/prettier-plugin-typespec": "^0.41.0",
"@azure-tools/typespec-azure-core": "^0.27.0",
"@azure-tools/typespec-autorest": "^0.27.0",
"@typespec/openapi": "^0.41.0",
"@typespec/openapi3": "^0.41.0",
"@typespec/compiler": "^0.44.0",
"@typespec/rest": "^0.44.0",
"@typespec/http": "^0.44.0",
"@typespec/versioning": "^0.44.0",
"@typespec/prettier-plugin-typespec": "^0.44.0",
"@azure-tools/typespec-azure-core": "^0.30.0",
"@azure-tools/typespec-autorest": "^0.30.0",
"@typespec/openapi": "^0.44.0",
"@typespec/openapi3": "^0.44.0",
"prettier": "~2.7.1"
},
"devDependencies": {
Expand All @@ -62,7 +62,7 @@
"fs-extra": "^10.1.0",
"@types/fs-extra": "^9.0.13",
"chalk": "^4.1.0",
"@azure-tools/typespec-autorest": "^0.27.0",
"@azure-tools/typespec-autorest": "^0.30.0",
"webpack-cli": "~4.7.2",
"webpack": "~5.40.0",
"@typescript-eslint/eslint-plugin": "^5.27.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { formatFile } from "../utils/format";

export async function emitCadlConfig(filePath: string): Promise<void> {
const content = `
emitters:
"@azure-tools/typespec-autorest": true
emit:
- "@azure-tools/typespec-autorest":
# Uncomment this line and add "@azure-tools/typespec-python" to your package.json to generate Python code
# "@azure-tools/typespec-python":
# "basic-setup-py": true
Expand Down
23 changes: 15 additions & 8 deletions packages/extensions/openapi-to-cadl/src/emiters/emit-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ import { CadlProgram } from "../interfaces";
import { formatFile } from "../utils/format";

export async function emitPackage(filePath: string, program: CadlProgram): Promise<void> {
const session = getSession();
// Default to false;
const includePackage = session.configuration["include-package"] === true;

if (!includePackage) {
return;
}

const name = program.serviceInformation.name.toLowerCase().replace(/ /g, "-");
const description = program.serviceInformation.doc;
const content = JSON.stringify(getPackage(name, description as string));
const session = getSession();
session.writeFile({ filename: filePath, content: formatFile(content, filePath) });
}

Expand All @@ -16,13 +23,13 @@ const getPackage = (name: string, description: string) => ({
description,
license: "MIT",
dependencies: {
"@typespec/compiler": "^0.41.0",
"@typespec/rest": "^0.41.0",
"@typespec/http": "^0.41.0",
"@typespec/versioning": "^0.41.0",
"@typespec/prettier-plugin-typespec": "^0.41.0",
"@azure-tools/typespec-azure-core": "^0.27.0",
"@azure-tools/typespec-autorest": "^0.27.0",
"@typespec/compiler": "^0.44.0",
"@typespec/rest": "^0.44.0",
"@typespec/http": "^0.44.0",
"@typespec/versioning": "^0.44.0",
"@typespec/prettier-plugin-typespec": "^0.44.0",
"@azure-tools/typespec-azure-core": "^0.30.0",
"@azure-tools/typespec-autorest": "^0.30.0",
prettier: "^2.7.1",
},
private: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function generateObject(cadlObject: CadlObject) {
propertyDoc && definitions.push(propertyDoc);
const decorators = generateDecorators(property.decorators);
decorators && definitions.push(decorators);

property.fixMe && property.fixMe.length && definitions.push(property.fixMe.join("\n"));
definitions.push(`"${property.name}"${getOptionalOperator(property)}: ${property.type};`);
}
definitions.push("}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { transformValue } from "../utils/values";

const cadlTypes = new Map<SchemaType, string>([
[SchemaType.Date, "plainDate"],
[SchemaType.DateTime, "zonedDateTime"],
[SchemaType.DateTime, "utcDateTime"],
[SchemaType.UnixTime, "plainTime"],
[SchemaType.String, "string"],
[SchemaType.Time, "plainTime"],
Expand Down Expand Up @@ -131,9 +131,19 @@ export function transformObjectProperty(propertySchema: Property, codeModel: Cod
isOptional: propertySchema.required !== true,
type: getCadlType(propertySchema.schema, codeModel),
decorators: getPropertyDecorators(propertySchema),
fixMe: getFixme(propertySchema, codeModel),
};
}

function getFixme(property: Property, codeModel: CodeModel): string[] {
const cadlType = getCadlType(property.schema, codeModel);
if (cadlType === "utcDateTime") {
return ["// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario."];
}

return [];
}

function getParents(schema: ObjectSchema): string[] {
const immediateParents = schema.parents?.immediate ?? [];

Expand Down
10 changes: 4 additions & 6 deletions packages/extensions/openapi-to-cadl/src/utils/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,10 @@ function getPageableResource(codeModel: CodeModel, operation: Operation): CadlRe
const dataTypes = getDataTypes(codeModel);

const elementType = property.schema.elementType;
if (!isObjectSchema(elementType)) {
continue;
}

if (!markResource(operation, elementType)) {
return undefined;
if (isObjectSchema(elementType)) {
if (!markResource(operation, elementType)) {
return undefined;
}
}

const cadlResponse = dataTypes.get(elementType) ?? transformDataType(elementType, codeModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ clear-output-folder: false
guessResourceKey: true
isAzureSpec: true
namespace: "Azure.Language.Authoring"
include-package: true
modelerfour:
lenient-model-deduplication: true
prenamer: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,20 @@ model ProjectsMetadata is Azure.Core.Page<ProjectMetadata>;
@resource("authoring/analyze-text/projects")
model ProjectMetadata {
@doc("Represents the project creation datetime.")
createdDateTime: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
createdDateTime: utcDateTime;

@doc("Represents the project last modification datetime.")
lastModifiedDateTime: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
lastModifiedDateTime: utcDateTime;

@doc("Represents the project last training datetime.")
lastTrainedDateTime?: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
lastTrainedDateTime?: utcDateTime;

@doc("Represents the project last deployment datetime.")
lastDeployedDateTime?: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
lastDeployedDateTime?: utcDateTime;

@doc("The project kind.")
projectKind: ProjectKind;
Expand Down Expand Up @@ -305,10 +309,12 @@ model ProjectDeployment {
modelId: string;

@doc("Represents deployment last trained time.")
lastTrainedDateTime: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
lastTrainedDateTime: utcDateTime;

@doc("Represents deployment last deployed time.")
lastDeployedDateTime: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
lastDeployedDateTime: utcDateTime;

@doc("Represents deployment expiration date in the runtime.")
deploymentExpirationDate: plainDate;
Expand Down Expand Up @@ -345,13 +351,16 @@ model JobState {
jobId: string;

@doc("The creation date time of the job.")
createdDateTime: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
createdDateTime: utcDateTime;

@doc("The last date time the job was updated.")
lastUpdatedDateTime: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
lastUpdatedDateTime: utcDateTime;

@doc("The expiration date time of the job.")
expirationDateTime?: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
expirationDateTime?: utcDateTime;

@doc("The job status.")
status: JobStatus;
Expand Down Expand Up @@ -400,7 +409,8 @@ model ProjectTrainedModel {
modelId: string;

@doc("The last trained date time of the model.")
lastTrainedDateTime: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
lastTrainedDateTime: utcDateTime;

@doc("The duration of the model's last training request in seconds.")
lastTrainingDurationInSeconds: int32;
Expand Down Expand Up @@ -466,7 +476,8 @@ model TrainingJobResult {
evaluationStatus?: SubTrainingJobState;

@doc("Represents the estimate end date time for training and evaluation.")
estimatedEndDateTime?: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
estimatedEndDateTime?: utcDateTime;
}

@doc("Represents the detailed state of a training sub-operation.")
Expand All @@ -475,10 +486,12 @@ model SubTrainingJobState {
percentComplete: int32;

@doc("Represents the start date time.")
startDateTime?: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
startDateTime?: utcDateTime;

@doc("Represents the end date time.")
endDateTime?: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
endDateTime?: utcDateTime;

@doc("Represents the status of the sub-operation.")
status: JobStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"description": "The language service API is a suite of natural language processing (NLP) skills built with best-in-class Microsoft machine learning algorithms. The API can be used to analyze unstructured text for tasks such as sentiment analysis, key phrase extraction, language detection and question answering. Further documentation can be found in <a href=\"https://docs.microsoft.com/en-us/azure/cognitive-services/language-service/overview\">https://docs.microsoft.com/en-us/azure/cognitive-services/language-service/overview</a>.",
"license": "MIT",
"dependencies": {
"@typespec/compiler": "^0.41.0",
"@typespec/rest": "^0.41.0",
"@typespec/http": "^0.41.0",
"@typespec/versioning": "^0.41.0",
"@typespec/prettier-plugin-typespec": "^0.41.0",
"@azure-tools/typespec-azure-core": "^0.27.0",
"@azure-tools/typespec-autorest": "^0.27.0",
"@typespec/compiler": "^0.44.0",
"@typespec/rest": "^0.44.0",
"@typespec/http": "^0.44.0",
"@typespec/versioning": "^0.44.0",
"@typespec/prettier-plugin-typespec": "^0.44.0",
"@azure-tools/typespec-azure-core": "^0.30.0",
"@azure-tools/typespec-autorest": "^0.30.0",
"prettier": "^2.7.1"
},
"private": true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
emitters:
"@azure-tools/typespec-autorest": true
emit:
- "@azure-tools/typespec-autorest":
# Uncomment this line and add "@azure-tools/typespec-python" to your package.json to generate Python code
# "@azure-tools/typespec-python":
# "basic-setup-py": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ and imputeMode is \"fixed\".
@doc("The definition of input timeseries points.")
model TimeSeriesPoint {
@doc("Optional argument, timestamp of a data point (ISO8601 format).")
timestamp?: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
timestamp?: utcDateTime;

@doc("The measurement of that point, should be float.")
value: float32;
Expand Down Expand Up @@ -383,10 +384,12 @@ model VariableState {
effectiveCount?: int32;

@doc("First timestamp of the variable.")
firstTimestamp?: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
firstTimestamp?: utcDateTime;

@doc("Last timestamp of the variable.")
lastTimestamp?: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
lastTimestamp?: utcDateTime;
}

@doc("Detection request.")
Expand All @@ -406,18 +409,21 @@ used in the training phase.
A required field, indicating the start time of data for detection. Should be
date-time.
""")
startTime: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
startTime: utcDateTime;

@doc("""
A required field, indicating the end time of data for detection. Should be
date-time.
""")
endTime: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
endTime: utcDateTime;
}

model AnomalyState {
@doc("timestamp")
timestamp: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
timestamp: utcDateTime;
value?: AnomalyValue;

@doc("Error message for the current timestamp")
Expand Down Expand Up @@ -467,10 +473,12 @@ will be used as its variable name.
A required field, indicating the start time of training data. Should be
date-time.
""")
startTime: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
startTime: utcDateTime;

@doc("A required field, indicating the end time of training data. Should be date-time.")
endTime: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
endTime: utcDateTime;

@doc("An optional field. The name of the model whose maximum length is 24.")
displayName?: string;
Expand Down Expand Up @@ -553,10 +561,12 @@ model ModelSnapshot {
modelId: string;

@doc("Date and time (UTC) when the model was created.")
createdTime: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
createdTime: utcDateTime;

@doc("Date and time (UTC) when the model was last updated.")
lastUpdatedTime: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
lastUpdatedTime: utcDateTime;

@doc("Model status. One of CREATED, RUNNING, READY, and FAILED.")
status: ModelStatus;
Expand All @@ -575,10 +585,12 @@ model Model {
modelId: string;

@doc("Date and time (UTC) when the model was created.")
createdTime: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
createdTime: utcDateTime;

@doc("Date and time (UTC) when the model was last updated.")
lastUpdatedTime: zonedDateTime;
// FIXME: (utcDateTime) Please double check that this is the correct type for your scenario.
lastUpdatedTime: utcDateTime;

@doc("""
Training result of a model including its status, errors and diagnostics
Expand Down
Loading

0 comments on commit 0354d7f

Please sign in to comment.