Skip to content

Conversation

@ksvirkou-hubspot
Copy link
Contributor

@ksvirkou-hubspot ksvirkou-hubspot commented Sep 16, 2021

fixes #10368

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master (5.3.0), 6.0.x
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@ksvirkou-hubspot
Copy link
Contributor Author

const requestContext = _config.baseServer.makeRequestContext(
localVarPath,
HttpMethod.{{httpMethod}},
_config.baseServer.getHeaders()
Copy link
Contributor

Choose a reason for hiding this comment

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

looks like you always pass baseServer.headers to the baseServer.makeRequestContext call.
looks redundant - why don't baseServer.makeRequestContext does it internally - automatically?

Copy link
Contributor

@amakhrov amakhrov left a comment

Choose a reason for hiding this comment

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

@ksvirkou-hubspot thanks for the proposal! Could you provide an example of how you would use the new api to make a request with some custom headers?

@ksvirkou-hubspot
Copy link
Contributor Author

@ksvirkou-hubspot thanks for the proposal! Could you provide an example of how you would use the new api to make a request with some custom headers?

    const configuration = createConfiguration({..})

    configuration.baseServer.setHeaderParam('User-Agent', 'test')

    let testApi = new TestApi(configuration)
    let response = TestApi.method()

@amakhrov

@amakhrov
Copy link
Contributor

let testApi = new TestApi(configuration)
let response = TestApi.method()

Ah, I see. So the use case is setting some headers for a group of requests (maybe even across the whole app) - rather overriding headers for a specific single request you make.

return this.variableConfiguration
}

public setHeaders(headers: Headers): void {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Any thoughts about changing the name?
getHeaders return all headers - quite expected
But setHeaders does not set all headers, instead it's an additive operation merging new headers with existing ones

Consequently, it's not possible to remove a header previously added to the server configuration.

I mean - it resembles the existing setVariables method, true. Not sure if that interface was well thought through, though (e.g. getConfiguration instead of getVariables indicates otherwise :) )

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right
I think It is better to change the function
@amakhrov

Copy link
Contributor Author

Choose a reason for hiding this comment

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

public setHeaders(headers: Headers): void {
     this.headers = headers;
}

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

or we can rename it to mergeHeaders or addHeaders

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

addHeaders sounds reasonable!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done
@amakhrov

@ksvirkou-hubspot
Copy link
Contributor Author

let testApi = new TestApi(configuration)
let response = TestApi.method()

Ah, I see. So the use case is setting some headers for a group of requests (maybe even across the whole app) - rather overriding headers for a specific single request you make.

yes it is necessary for us

Copy link
Contributor

@amakhrov amakhrov left a comment

Choose a reason for hiding this comment

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

LGTM

@macjohnny
Copy link
Member

@ksvirkou-hubspot can you please fix the ci failure?

@ksvirkou-hubspot
Copy link
Contributor Author

@ksvirkou-hubspot can you please fix the ci failure?

I think I can't fix it (It isn't the first PR to Open API Generator and every time I see this errors)
But I ll merge master to this branch (Maybe this will help)

@ksvirkou-hubspot
Copy link
Contributor Author

@ksvirkou-hubspot can you please fix the ci failure?

Done
@amakhrov

@macjohnny macjohnny added this to the 5.3.0 milestone Oct 5, 2021
@macjohnny
Copy link
Member

@ksvirkou-hubspot please take a look at the circle CI failure:
https://app.circleci.com/pipelines/github/OpenAPITools/openapi-generator/13101/workflows/7896a7d0-be2c-4691-80da-020d58867b06/jobs/26742

1) PetApi
       uploadFile:
     Error: HTTP-Code: 415
Message: Unknown API Status Code!
Body: "{\"code\":415,\"type\":\"unknown\"}"
      at new ApiException (/home/circleci/OpenAPITools/openapi-generator/samples/openapi3/client/petstore/typescript/builds/default/apis/exception.ts:12:9)
      at PetApiResponseProcessor.<anonymous> (/home/circleci/OpenAPITools/openapi-generator/samples/openapi3/client/petstore/typescript/builds/default/apis/PetApi.ts:625:15)
      at step (/home/circleci/OpenAPITools/openapi-generator/samples/openapi3/client/petstore/typescript/builds/default/dist/apis/PetApi.js:46:23)
      at Object.next (/home/circleci/OpenAPITools/openapi-generator/samples/openapi3/client/petstore/typescript/builds/default/dist/apis/PetApi.js:27:53)
      at fulfilled (/home/circleci/OpenAPITools/openapi-generator/samples/openapi3/client/petstore/typescript/builds/default/dist/apis/PetApi.js:18:58)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)

the current master at f5e8f54 seems to build correctly: https://app.circleci.com/pipelines/github/OpenAPITools/openapi-generator/13100/workflows/a8cc5b7e-e4b4-48b6-ac4a-13288c3ac369/jobs/26741

@bodograumann
Copy link
Contributor

Wouldn‘t it make more sense to write a Middleware to set these headers?

@ksvirkou-hubspot
Copy link
Contributor Author

Wouldn‘t it make more sense to write a Middleware to set these headers?

Maybe
but when I checked code of this generator (typescript) I decided to do it by this way because it does not require rewriting of existing code
@bodograumann

@ksvirkou-hubspot
Copy link
Contributor Author

@ksvirkou-hubspot please take a look at the circle CI failure: https://app.circleci.com/pipelines/github/OpenAPITools/openapi-generator/13101/workflows/7896a7d0-be2c-4691-80da-020d58867b06/jobs/26742

1) PetApi
       uploadFile:
     Error: HTTP-Code: 415
Message: Unknown API Status Code!
Body: "{\"code\":415,\"type\":\"unknown\"}"
      at new ApiException (/home/circleci/OpenAPITools/openapi-generator/samples/openapi3/client/petstore/typescript/builds/default/apis/exception.ts:12:9)
      at PetApiResponseProcessor.<anonymous> (/home/circleci/OpenAPITools/openapi-generator/samples/openapi3/client/petstore/typescript/builds/default/apis/PetApi.ts:625:15)
      at step (/home/circleci/OpenAPITools/openapi-generator/samples/openapi3/client/petstore/typescript/builds/default/dist/apis/PetApi.js:46:23)
      at Object.next (/home/circleci/OpenAPITools/openapi-generator/samples/openapi3/client/petstore/typescript/builds/default/dist/apis/PetApi.js:27:53)
      at fulfilled (/home/circleci/OpenAPITools/openapi-generator/samples/openapi3/client/petstore/typescript/builds/default/dist/apis/PetApi.js:18:58)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)

the current master at f5e8f54 seems to build correctly: https://app.circleci.com/pipelines/github/OpenAPITools/openapi-generator/13100/workflows/a8cc5b7e-e4b4-48b6-ac4a-13288c3ac369/jobs/26741

I've just run this test on my local machine and It works correctly
Could you re-run tests?

@macjohnny

@bodograumann
Copy link
Contributor

I ran the tests locally and found the same 415 error on uploadFile. This error does not occur on master.
Make sure you are following the correct steps: https://github.com/OpenAPITools/openapi-generator/wiki/Integration-Tests

Not sure why it is happening, but Content-Type is set after your change as application/json in the fetch call for uploadFile, where previously it was not set explicitely, but automatically determined by fetch. (Cf. https://github.com/ksvirkou-hubspot/openapi-generator/blob/feature/typescriptAddHeaders/samples/openapi3/client/petstore/typescript/builds/default/http/isomorphic-fetch.ts#L14)

Furthermore you should add some tests of your own to (at least) document the new feature.

@wing328 wing328 modified the milestones: 5.3.0, 5.3.1 Oct 25, 2021
@atanasiuk-hubspot
Copy link
Contributor

Hi @bodograumann, I'm trying to repro failing tests with ./run-in-docker.sh mvn integration-test, but it works well for me. Am I missing something?

@bodograumann
Copy link
Contributor

According to the documentation you need to do:

cd samples/openapi3/client/petstore/typescript/tests/default
mvn integration-test

I don’t know why these tests are not automatically executed when running mvn integration-test on the top-level.
Can you help here @macjohnny ?

@ksvirkou-hubspot
Copy link
Contributor Author

Hey everyone
I ve fix all tests
could you merge it?
@amakhrov @macjohnny @bodograumann

@bodograumann
Copy link
Contributor

Works now afaics. Still think this should be done in a Middleware instead though.

@ksvirkou-hubspot
Copy link
Contributor Author

Works now afaics. Still think this should be done in a Middleware instead though.

I didn’t create a new construction
I only updated existing code base
I my opinion it is no need to make something new because it requires rewriting of existing code base
and more over this design was approved earlier

@bodograumann
Copy link
Contributor

I didn’t want to start a new discussion.
Not sure you know about middlewares though. They are an already existing mechanism in the generated code.
You can define a middleware in your own code and register it when instantiating the api client.
It can inspect the request and add headers before sending easily.

@ksvirkou-hubspot
Copy link
Contributor Author

ok
Could you merge it?
or if I have to fix something write me please
@macjohnny

@macjohnny
Copy link
Member

@TiFu what is your opinion here?

@ksvirkou-hubspot
Copy link
Contributor Author

Any Updates?
@macjohnny @TiFu

@ksvirkou-hubspot
Copy link
Contributor Author

Any Updates?
@macjohnny @TiFu @amakhrov @bodograumann

@macjohnny
Copy link
Member

Sorry for the long delay.
One question though: could this also be achieved by adding an approriate middleware, which could be done with the currently generated code?

@wing328 wing328 modified the milestones: 5.3.1, 5.4.0 Dec 29, 2021
@ksvirkou-hubspot
Copy link
Contributor Author

Yes we can do it
but Could you give me an example (where it is implemented)?
CC @macjohnny @TiFu @amakhrov @bodograumann

@amakhrov
Copy link
Contributor

amakhrov commented Jan 4, 2022

@ksvirkou-hubspot I guess it could be something like this (using your original code snippet as the basis):

const configuration = createConfiguration({
  middleware: [{
    pre: context => {
      context.setHeaderParam('User-Agent', 'test');
      return of(context);
    }
  }],
   ... other config params
})

let testApi = new TestApi(configuration)
let response = TestApi.method()

@TiFu
Copy link
Contributor

TiFu commented Jan 15, 2022

Sorry for the delay!

Given that this can easily be implemented in a Middleware (as demonstrated by @amakhrov), I would suggest to not merge this PR. I think this use case is a bit of a poster child for why we added the Middleware option.

@macjohnny
Copy link
Member

Thanks @TiFu
Will close this PR then.
@ksvirkou-hubspot would you like to add @amakhrov s example to the readme?

@macjohnny macjohnny closed this Jan 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[REQ] [Typescript] Add Headers to Request

7 participants