Skip to content

fix emitter for ADP #1659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 9, 2023
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
6 changes: 6 additions & 0 deletions packages/cadl-python/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release History

## 2023-01-xx - 0.xx.xx

**Bug Fixes**

- Fix body type for spread model. #1659

## 2023-01-06 - 0.4.15

**Other Changes**
Expand Down
53 changes: 40 additions & 13 deletions packages/cadl-python/src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ import {
getServers,
HttpAuth,
HttpOperationParameter,
HttpOperationRequestBody,
HttpOperationResponse,
HttpOperationResponseContent,
HttpServer,
isStatusCode,
HttpOperation,
} from "@cadl-lang/rest/http";
import { getAddedOn } from "@cadl-lang/versioning";
import {
Expand Down Expand Up @@ -243,15 +243,16 @@ function getEffectiveSchemaType(program: Program, type: Model): Model {
function getType(program: Program, type: EmitterType): any {
// don't cache simple type(string, int, etc) since decorators may change the result
const enableCache = !isSimpleType(program, type);
const effectiveModel = type.kind === "Model" ? getEffectiveSchemaType(program, type) : type;
Copy link
Member Author

@changlong-liu changlong-liu Jan 9, 2023

Choose a reason for hiding this comment

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

we are using effective type in emitModel (

const modelName = getName(program, type) || getEffectiveSchemaType(program, type).name;
), so we need changes here for caching by effective type to avoid emit two times for effective model.

if (enableCache) {
const cached = typesMap.get(type);
const cached = typesMap.get(effectiveModel);
if (cached) {
return cached;
}
}
let newValue = emitType(program, type);
if (enableCache) {
typesMap.set(type, newValue);
typesMap.set(effectiveModel, newValue);
if (type.kind === "Model") {
// need to do properties after insertion to avoid infinite recursion
for (const property of type.properties.values()) {
Expand Down Expand Up @@ -321,7 +322,39 @@ type BodyParameter = ParamBase & {
defaultContentType: string;
};

function emitBodyParameter(program: Program, body: HttpOperationRequestBody, operation: Operation): BodyParameter {
function getBodyType(program: Program, route: HttpOperation): Type {
let bodyModel = route.parameters.body?.type;
if (bodyModel && bodyModel.kind === "Model" && route.operation) {
const resourceType = getResourceOperation(program, route.operation)?.resourceType;
if (resourceType && route.responses && route.responses.length > 0) {
const resp = route.responses[0];
if (resp && resp.responses && resp.responses.length > 0) {
const responseBody = resp.responses[0]?.body;
if (responseBody?.type?.kind === "Model") {
const bodyTypeInResponse = getEffectiveSchemaType(program, responseBody.type);
// response body type is reosurce type, and request body type (if templated) contains resource type
if (
bodyTypeInResponse === resourceType &&
bodyModel.templateArguments &&
bodyModel.templateArguments.some((it) => {
return it.kind === "Model" || it.kind === "Union" ? it === bodyTypeInResponse : false;
})
) {
bodyModel = resourceType;
}
}
}
}
if (resourceType && bodyModel.name === "") {
bodyModel = resourceType;
}
}
return bodyModel!;
}

function emitBodyParameter(program: Program, httpOperation: HttpOperation): BodyParameter {
const params = httpOperation.parameters;
const body = params.body!;
const base = emitParamBase(program, body.parameter ?? body.type);
let contentTypes = body.contentTypes;
if (contentTypes.length === 0) {
Expand All @@ -330,16 +363,10 @@ function emitBodyParameter(program: Program, body: HttpOperationRequestBody, ope
if (contentTypes.length !== 1) {
throw Error("Currently only one kind of content-type!");
}
let type;
const resourceOperation = getResourceOperation(program, operation);
if (resourceOperation) {
type = getType(program, resourceOperation.resourceType);
} else {
type = getType(program, body.type);
}
const type = getType(program, getBodyType(program, httpOperation));

if (type.type === "model" && type.name === "") {
type.name = capitalize(operation.name) + "Request";
type.name = capitalize(httpOperation.operation.name) + "Request";
}

return {
Expand Down Expand Up @@ -631,7 +658,7 @@ function emitBasicOperation(program: Program, operation: Operation, operationGro
if (httpOperation.parameters.body === undefined) {
bodyParameter = undefined;
} else {
bodyParameter = emitBodyParameter(program, httpOperation.parameters.body, operation);
bodyParameter = emitBodyParameter(program, httpOperation);
if (parameters.filter((e) => e.restApiName.toLowerCase() === "content-type").length === 0) {
parameters.push(emitContentTypeParameter(bodyParameter, isOverload, isOverriden));
}
Expand Down