Skip to content

Allow browsers File type for files #5521

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 4 commits into from
May 5, 2020
Merged

Allow browsers File type for files #5521

merged 4 commits into from
May 5, 2020

Conversation

bodograumann
Copy link
Contributor

I changed the HttpFile type to Blob & { name: string } for browser platform and to Buffer & { name: string } for node platform. This is imho more convenient.
The upload tests now work for both default and jquery petstore samples.

There is also a bit of code cleanup included.

Cf. #802
CC @TiFu

@bodograumann
Copy link
Contributor Author

Seems we are too out-of-date for the pipeline. I guess it is time for merging master again :-D

@bodograumann
Copy link
Contributor Author

Would it be possible to merge master without squashing? @TiFu
The squashing we did last time makes merging master again really hard.
Or is there a trick I don’t know?

@wing328
Copy link
Member

wing328 commented Mar 17, 2020

cc @TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01) @nicokoenig (2018/09) @topce (2018/10) @akehir (2019/07) @petejohansonxo (2019/11) @amakhrov (2020/02)

@@ -120,7 +120,7 @@ describe("PetApi", () =>{

it("uploadFile", (done) => {
const image = fs.readFileSync(__dirname + "/pet.png")
petApi.uploadFile(pet.id, "Metadata", { name: "pet.png", data: image}).then((response: any) => {
petApi.uploadFile(pet.id, "Metadata", Object.assign(image, { name: "pet.png" })).then((response: any) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure I agree that mutating the Buffer is more convenient or even more intuitive than passing a composite of {name, data}.
What is the benefit of this approach?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, for node you could argue this not more / less convenient.
I mostly did it because of the browser platform though.
There you usually handle File objects, which already contain a name.
I'd be happy to hear how you would weigh this.

Copy link
Contributor

Choose a reason for hiding this comment

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

This could also be an generator-specific configuration, couldn't it? Just generate the appropriate wrapper ({name, data} composite for anything node, and what is in this PR for the browser) and then internally map this to an appropriate type before doing all the api logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could get on board with that, @TiFu. As long as you think that this won't be too much additional complexity.

@TiFu
Copy link
Contributor

TiFu commented Mar 28, 2020

Would it be possible to merge master without squashing? @TiFu
The squashing we did last time makes merging master again really hard.
Or is there a trick I don’t know?

You should be able to either git merge master, then resolve conflicts (probably not a whole lot) or git rebase master which rewinds all of our work, then pulls in the commits from master and applies our work again.

@bodograumann
Copy link
Contributor Author

Unfortunately it is not as simple as that. Cf. https://stackoverflow.com/a/11797964/1534459
By squashing the merge from master into the feature branch, the feature branch now contains a copy of the changes in master.
That means when trying to do any further merge of master into the feature branch or the other way around, the two changes will be in conflict. (Even though they contain exactly the same)

Maybe we should just throw away the history of this feature branch and rebase everything as one commit on master, @TiFu !?

@macjohnny
Copy link
Member

@TiFu maybe it would be possible to keep the current typescript-generators and offer the refactored typescript generators as a separate generator, eventually removing the current (old) typescript-generators.

@TiFu
Copy link
Contributor

TiFu commented Apr 19, 2020

@bodograumann that's a good point. If you want, I can rebase ts-refactor onto the latest master and force push those changes. You should then be able to fetch the latest ts-refactor and reapply the changes from this PR. Would this work for you?
This should make ts-refactor look like we started working with the latest master.

@macjohnny sounds like a good idea to get a bit more work done on this - before we do this though I would make it absolutely clear that it's an alpha version and there may be breaking changes in the future. I'll also check next weekend what we need to do to reach feature parity with the already implemented generators (jquery and ts-fetch)

@bodograumann
Copy link
Contributor Author

@TiFu That sounds good. I can probably cherry pick this PR quite easily.

@TiFu
Copy link
Contributor

TiFu commented Apr 26, 2020

@bodograumann Rebasing took me a bit longer than I expected, but I hope everything went okay. The rebased branch will be on typescript-refactor-master until I can make sure everything went right and I didn't delete any changes compared to typescript-refactor. I'll try to verify this tomorrow or the day after.

This rebase also removes the changes introduced in the squashed master merge commit - in the future git merge master without --squash (outside a PR! so that git retains knowledge about common ancestors) should prevent this issue as far as I understand.

@bodograumann
Copy link
Contributor Author

From regenerating the samples, I think in 7a372ac you missed the changes to DefaultCodegen.java, @TiFu.

@bodograumann
Copy link
Contributor Author

This is the diff:

diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java
index 914c59facc..6faccf5c43 100644
--- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java
+++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java
@@ -3327,7 +3327,8 @@ public class DefaultCodegen implements CodegenConfig {
             op.examples = new ExampleGenerator(schemas, this.openAPI).generateFromResponseSchema(exampleStatusCode, responseSchema, getProducesInfo(this.openAPI, operation));
             op.defaultResponse = toDefaultValue(responseSchema);
             op.returnType = cm.dataType;
-            op.hasReference = schemas.containsKey(op.returnBaseType);
+            op.returnFormat = cm.dataFormat;
+            op.hasReference = schemas != null && schemas.containsKey(op.returnBaseType);
 
             // lookup discriminator
             Schema schema = schemas.get(op.returnBaseType);

If you like, I can create a PR for typescript-refactor-master, but it will probably be faster if you do the change yourself.

@TiFu
Copy link
Contributor

TiFu commented May 2, 2020

Thanks! I readded the change. I'll check back in ~30 minutes to see if CI still complains or if it's fine.

This is by far the most common use case. A `File` object already
contains the name attribute. This commit allows that information to be
used directly.
When sending a `Blob`, in most browsers the `File` constructor can be
used to assign a file name. In all other browsers the alternative is
```typescript
Object.assign(data, { name: "foobar.txt" });
```
That is why we explicitely pass the name as third parameter to
`FormData.append`. This `Object.assign` method also works for `Buffer`
objects in node.

If one really does not want to touch the data object in the browser it
is possible to define another reference to the data with
```typescript
new Blob([data], { type: data.type })
```
or in node via
```typescript
Buffer.from(data)
```
@bodograumann bodograumann changed the base branch from typescript-refactor to typescript-refactor-master May 5, 2020 10:25
@bodograumann
Copy link
Contributor Author

I have kept the existing object type for the node platform now and only applied the changes for browsers.
Also the merge base is now typescript-refactor-master.

@TiFu

@TiFu TiFu merged commit 391a191 into OpenAPITools:typescript-refactor-master May 5, 2020
@TiFu
Copy link
Contributor

TiFu commented May 5, 2020

Perfect, looks good to me.

Thank you for the PR which has been merged into ts-refactor-master.

macjohnny added a commit that referenced this pull request Jun 15, 2020
… jquery (#6341)

* Added http module draft

* Added generic enum

* Modified http lib, added config & middleware definition to ts-fetch

* Added model generation with imports

* Added auth module

* Added servers

* Added sample for typescript client

* WIP: Models & API

* Updated auth

* WIP: api modeling

* Implemented RequestFactory and Processor completely

* Implemented fetch client

* Ignore dist folder in typescript client sample

* Added middleware to fetch

* Restructured TypeScript generator

* Reverted: http library.send returns string again

* Removed TODOs

* Added pom.xml files to TypeScript PetStore client samples

* Removed tabs from TypeScriptClientCodegen

* Added ts client codegen to root pom.xml and travis

* Added server variable configuration to ts-refactor

* [TS-Refactor] Added tests for Object Serializer

* Added simple test for PetApi

* Fixed ObjectSerializer test

* Added handling for different http status codes and test for deletePet

* Removed tabs in TypeScriptClientCodegen

* Removed tabs in DefaultCodegen

* Additional tests for pet store api

* Fixed file uploads

* Made api call configuration separately settable

* Use string union for enums

* Remove tab

* Restructured module layout

* Use observables internally

* Added promise based middleware

* Made discriminator and attributeTypeMap readonly

* Configure discriminator correctly

* Set discriminator value automatically

* Fixed date-time and date handling

* Added comments & license info

* Added comments

* Ignore openapi-generator-cli/bin

* Removed accidentally created generated code

* Fixed compilation issues in TypeScriptClientCodegen

* Added typescript to docs/generators

* Updated docs

* Added gitignore and git_push

* Added jquery library

* Added pom.xmls, fixed packagejsons and hopefully webppack

* Removed tabs in TypeScriptClientCodegen

* Fixed a couple issues with pom.xml

* Ensured up to date

* Fixed missing fetch definition in TS default tests

* Updated typescript docs

* Refactor typescript merge master (#4319)

Merge master into ts-refactor

* Typescript refactor: stub rxjs (#4424)

* Remove unused supportsES6 field from codegen

* Add a new switch for RXJS

* Remove redundant npm dependency on rxjs4 types

* Fix return type of PromiseMiddleware methods

* Install webpack dependency to run jquery tests

* Update form-data to 2.5 which includes typings

* Add missing dependency on node typings

* Fix test artifact name typo

* Stub rxjs when it is not explicitly enabled

* Typescript refactor: Platform select for browser and node (#4500)

* Use string form of filename parameter

This works for the form-data library and is also compatible with the
browser FormData object.

* Add new option to select platform node or browser

When no platform is selected, a default is chosen by the framework
option and likewise the file data type option is implied by the
platform.

* Remove redundant import of node dns module

* Only use form-data library for node platform

* Generate npm package from npmName option

* Use method convertPropertyToBooleanAndWriteBack

* Generate typescript samples with ensure-up-to-date

* Removed tab from DefaultCodegen

* Readded missing change

* Mark typescript client codegen as experimental

* Removed whitespace

* [TS-Refactor] Top-level exports for fetch & jquery (#6138)

* Added top-level exports
* Updated generator README
* Updated typescript generator docs

* Allow browsers File type for files (#5521)

* Allow passing file parameters as File objects
* Add test for jquery upload
* Use HttpFile object for node platform
* Regenerate samples

This is by far the most common use case. A `File` object already
contains the name attribute. This commit allows that information to be
used directly.
When sending a `Blob`, in most browsers the `File` constructor can be
used to assign a file name. In all other browsers the alternative is
```typescript
Object.assign(data, { name: "foobar.txt" });
```
That is why we explicitely pass the name as third parameter to
`FormData.append`. This `Object.assign` method also works for `Buffer`
objects in node.

If one really does not want to touch the data object in the browser it
is possible to define another reference to the data with
```typescript
new Blob([data], { type: data.type })
```
or in node via
```typescript
Buffer.from(data)
```

* [TS-Refactor] Added options for npm version, repository, name and updated readme (#6139)

* Added options for npm version, repository, name and updated readme

* Removed `this`  where not required

* Updated typescript docs

* Typescript refactor fixes (#6027)

Fixes a handful of issues identified in #802 (comment)

List of changes

* Clean: Remove redundant cliOption definition

* Remove redundant directory structure in templates

If we need to have different index.ts files for the different
frameworks, we can mostly do that in the one mustache file. In the cases
where that is not possible, we can still add a new override file later.

* Use File.separator consistently

* Only export selected api type

* Simplify promise polyfill import

The behaviour should be the same, according to the es6-promise docs.
Previously tsc would report the error:
> error TS2307: Cannot find module 'es6-promise'.

* Import HttpFile in all models

* Export server configurations

* Use undefined as default body value

The empty string is not interpreted as "no body" by the browser fetch
api and thus leads to an exception during get requests

* Improve codestyle: prefer guards to nesting

* Remove verbose debug output

This should not be commited, because every developer has very different
requirements what debug information he needs to see.

* Fix: Use cleaned model names for imports

* Fix: do not call toString on undefined

* Fix typo in doc comment

* Introduce RequestBody type and remove method check

* Support media types other than json (#6177)

List of changes:

* Add */* as fallback to accept header

* Use more sophisticated media type selection

* Handle object stringify in ObjectSerializer

* Parse response with ObejctSerializer

* Fix: Correctly extract response headers in browser

* Create HttpFile objects from responses

* Handle binary responses

* Clean up dependencies and replace isomorphic-fetch

Instead of isomorphic-fetch, which is unmaintained, we directly use
node-fetch and whatwg-fetch polyfills.

* Updated versions in ts-default/jquery and ts docs

* Replaced isSuccessCode with is2xx

* [TypeScript-Refactor] Use OAIv3 spec and fix bugs in JQuery Blob download (#6416)

* Change to OAIv3 spec for TS-Refactor

* Moved samples to oaiv3 folder

* Updated package-lock

* Update pom to use OAIv3 paths for Typescript-refactor

* Renamed ts-refactor samples & tests in pom.xmls

* Fixed compile issues in ts-refactor jquery http test

* Fixed bugs in blob handling of jquery

* [Typescript] Support http bearer authentication with token provider (#6425)

* Add http bearer security

* Update typescript to 3.9

* Fix: Use Authorization header for basic and bearer

* Allow asynchronous tokenProvider in bearer auth

* Add TS-Rewrite-Jquery tests node_modules to travis caching

* Remove NoAuthentication

* Added file to generate TS samples on Windows

* Exclude btoa in browser

* Regen samples

* Remove outdated ToDo comments

* Document and optimize `getReturnType` in TSClientCodegen

* Added option to generate objects for operation function arguments

* Upgrade typescript docs

* Updated generators

* Updated samples

* Updated docs

* Readded pom.xml

* [Typescript] Support InversifyJS (#6489)

* Add config option to enable InversifyJS

* Add pascal case lambda for mustache

* Generate a class for each auth method

* Add service identifiers and service binder helper

* Split Configuration into interface and factory

This way we don't need to import the factory everywhere to do
typechecking.

* Define minimal interface for ServerConfiguration

* Add annotations for inversify when enabled

* Always expose list of server configurations

* Add samples and defalt tests for useInversify

* Simplify sample generation script

* Fix: Add object_params arg description to help

* Fix: Properly enable inversify with bool property

* Build tests in pom instead of prepublish

Otherwise running `npm install`, when the build failed was impossible.

* Update dependencies for inversify tests

* Test basic api service resolution

* Remove Promise and Observable prefix from exports

* Fix, RxJS: Import Observable in object params api

* Add ioc service identifier for object param api

* Add hint about unimpeded development

* Simplify api service binder syntax

* Remove default tests for inversify

* Add wrapper for easy promise based http libraries

This wrapper allows defining and injecting http libraries that do not
need to know anything about observables, especially when useRxJS is not
enabled. I will employ this in the tests for InversifyJS.

Not sure if we should also use this wrapper internally.

* Add named injects for remaining auth parameters

* Directly inject promise services without RxJS

* Add tests for api service binder

* Add convenience method to bind all api services

* Fix: Rename inversify test artifact

* Run bin/utils/copy-to-website.sh

* Restore changes to CONTRIBUTING.md from PR #6489

Co-authored-by: Bodo Graumann <[email protected]>
Co-authored-by: Esteban Gehring <[email protected]>
@wing328 wing328 added this to the 5.0.0 milestone Jul 3, 2020
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.

5 participants