swagger-api / swagger-api/swagger-codegen

[C# et al] Default user-agent violates RFC

Open
#8,376 0 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Supporting code generated for C# (but also many other languages) default to an invalid User-Agent string according to RFC 7231 HTTP/1.1 Semantics and Content. We noticed this because Akka HTTP Server issues a warning about the incorrect format. It doesn't seem to cause any other ill effects beside the warning, however, apart from the feeling one gets around RFC violations.

For example C# sets UserAgent = "Swagger-Codegen/1.0.0/csharp";

Replacing the actual user-agent (RestSharp in this case) not only defeats the fields purpose, but in this case also violates the specification.

RFC 7231 says the format of the User-Agent string should be:

User-Agent      = product *( RWS ( product / comment ) )
product         = token ["/" product-version]
product-version = token

where 'token' does not allow '/'. Comment is basically "something in parentesis".

Swagger-codegen version

I'm using 2.3.1, but incorrectly formatted user-agent strings can be found in the codebase going back at least two years.

For C# this User-Agent default is present in the templates here and here

However, User-Agent is prevalent in the code base (try git log -G "[Aa]gent.*[Ss]wagger-[Cc]odegen"). At least the template resources and sample code for Android, C#, Eiffel, Go, Java, Perl, PHP, Python, R, Ruby and Rust contains (defaults) to the Swagger-Codegen/version/language format.

Swagger declaration file content or url

Any declaration will probably do, the problem is in the supporting code.

Command line used for generation
swagger-codegen generate --lang csharp --model-package Models -DtargetFramework=v5.0 -DpackageName=Some.Export -i some/schema.yaml -o some-csharp
Steps to reproduce

Generate code, then search for "agent" in ./some-csharp/src/Some.Export/Client/Configuration.cs.

Related issues/PRs

Tried a few searches, couldn't find a similar issue reporting this as a problem. However, https://github.com/swagger-api/swagger-codegen/pull/2376 and several issues linked from there, encourages the use of Swagger-Codegen/{version}/{language} as a User-Agent.

Suggest a fix/enhancement

Replacing the actual user-agent defeats the fields purpose:

... often used by servers to help identify the scope of reported
interoperability problems, to work around or tailor responses to avoid
particular user agent limitations, and for analytics regarding browser or
operating system use.

So if self-promotion is desired, it would be more appropriate to append Swagger-Codegen as a less significant part of the User-Agent, and include the language used as a comment. For example using C# and RestSharp v105.2.3, the User-Agent could be;

User-Agent: RestSharp/105.2.3 Swagger-Codegen/1.0.0 (csharp)

This way, any workarounds necessary for RestSharp can still be applied, self-promotion still there, and the format is RFC 7231-compliant.

As for the annoying warning in Akka HTTP server logs, there's an Akka configuration option to turn that off. I can also explicitly set User-Agent using --http-user-agent=Something/1.0 when calling swagger-codegen.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/swagger-codegen/src/main/resources/csharp/Configuration.mustache and search the repository with git log -G "[Aa]gent.*[Ss]wagger-[Cc]odegen". Review the generated C# Configuration.cs and the named language templates, then verify that the affected default User-Agent values follow the reported RFC format across the relevant generated clients.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.