Skip to content

multipart/form-data support for typespec-todo#2984

Merged
timovv merged 10 commits intoAzure:mainfrom
timovv:multipart-todo
Jan 15, 2025
Merged

multipart/form-data support for typespec-todo#2984
timovv merged 10 commits intoAzure:mainfrom
timovv:multipart-todo

Conversation

@timovv
Copy link
Copy Markdown
Member

@timovv timovv commented Jan 9, 2025

multipart/form-data support for the cases covered by the todo spec for the e2e demo.

Includes basic support for file uploads, JSON, and text parts. There are some items to follow up on after this PR:

  • Anonymous models with @multipartBody are not currently supported
  • Models with text parts and a custom content type header (the non-string cadl ranch case) are not yet supported

Fixes #2422

@timovv timovv force-pushed the multipart-todo branch 2 times, most recently from c2e63bd to 8c0bd25 Compare January 9, 2025 21:53
}

// @public
export type FileContents = string | NodeJS.ReadableStream | ReadableStream<Uint8Array> | Uint8Array | Blob;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

just curious why this is added? is this expected?

Copy link
Copy Markdown
Member Author

@timovv timovv Jan 13, 2025

Choose a reason for hiding this comment

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

Seems like this spec has an anonymous model for MFD which isn't properly supported yet. FileContents should be used in uploadFile but is currently Uint8Array. Will fix.

Copy link
Copy Markdown
Member Author

@timovv timovv Jan 14, 2025

Choose a reason for hiding this comment

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

Will address anonymous models including this case in a follow up issue

Comment thread packages/typespec-ts/test/commands/cadl-ranch-list.js
Comment thread packages/typespec-ts/test/modularUnit/scenarios/multipart/file.md Outdated
Comment thread packages/typespec-ts/test/modularUnit/scenarios/multipart/file.md Outdated
Comment thread packages/typespec-ts/test/modularUnit/scenarios/multipart/file.md Outdated

```tsp
model RequestBody {
files: HttpPart<File>[];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

should we add a case for

model RequestBody {
  files: HttpPart<File[]>;
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't think that case really makes sense. That body model would represent a single part consisting of a JSON array of File objects.

We do already have a case for a JSON array here.

if (isAzureCoreErrorType(context.program, type.__raw)) {
return;
}
if (isOrExtendsHttpFile(context.program, type.__raw!)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wonder if we could use usage to filter the HttpPart models out?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We can't because tcgc doesn't set UsageFlags.MultipartFormData for Http.File. It only does that for the actual multipart body model.

);
if (
!properties.some((x) => x.name === name) &&
!(existInterface && existInterface.getProperty(name))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wonder why we need this filter? do you see any cases where there's duplication?

Copy link
Copy Markdown
Member Author

@timovv timovv Jan 13, 2025

Choose a reason for hiding this comment

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

Yes, the multipart payload cadl ranch spec causes this duplication for some reason. If you revert this change and regenerate you should see the issue.

NameType.Property
);

// HACK: check if the statement includes a group of this name already to prevent an operation group appearing multiple times
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I feel like it's strange if an operation group would appear multiple times. how could I repro?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Same issue as above -- without this the multipart spec generates the group 3 times. I think it might have to do with the way that the operations are nested?

Comment thread packages/typespec-ts/src/modular/helpers/operationHelpers.ts Outdated
@timovv timovv changed the title [WIP] multipart/form-data support for typespec-todo multipart/form-data support for typespec-todo Jan 14, 2025
@timovv timovv marked this pull request as ready for review January 14, 2025 21:56
Comment thread packages/typespec-ts/test/modularUnit/scenarios/example/example.md Outdated
Copy link
Copy Markdown
Member

@qiaozha qiaozha left a comment

Choose a reason for hiding this comment

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

LGTM

parts.push(partDefinition);
}

// TODO: How to handle additionalProperties for MFD?
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

is there an issue to track this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added it to the follow up issue #3001

@timovv timovv merged commit d6ccc49 into Azure:main Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Multipart Form Data Implementation in Modular

4 participants