Skip to content
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

Correctly handle multiple methods with similar suffixes #64

Closed
chrismuellner opened this issue Jan 28, 2020 · 5 comments
Closed

Correctly handle multiple methods with similar suffixes #64

chrismuellner opened this issue Jan 28, 2020 · 5 comments
Labels
enhancement New feature or request

Comments

@chrismuellner
Copy link

Problem

When generating Services for an existing OpenApi 3.0.1 Application, the methods have an unnecessary $Json Postfix e.g. register$Json(...) instead of register(...) even though they accept and return concrete classes.

Method in swagger.json

"/Authentication/Register": {
      "post": {
        "tags": [
          "Authentication"
        ],
        "summary": "Register the new user.",
        "operationId": "register",
        "requestBody": {
          "description": "Information about the user that is registered.",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/RegistrationDTO"
              }
            },
            "text/json": {
              "schema": {
                "$ref": "#/components/schemas/RegistrationDTO"
              }
            },
            "application/*+json": {
              "schema": {
                "$ref": "#/components/schemas/RegistrationDTO"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success"
          }
        }
      }

method in generated service

  /**
   * Path part for operation register
   */
  static readonly RegisterPath = '/Authentication/Register';

  /**
   * This method provides access to the full `HttpResponse`, allowing access to response headers.
   * To access only the response body, use `register$Json()` instead.
   *
   * This method sends `application/json` and handles request body of type `application/json`.
   */
  register$Json$Response(params?: {

  
  /**
   * Information about the user that is registered.
   */
  body?: RegistrationDto
  }): Observable<StrictHttpResponse<void>> {

    const rb = new RequestBuilder(this.rootUrl, AuthenticationService.RegisterPath, 'post');
    if (params) {


      rb.body(params.body, 'application/json');
    }
    return this.http.request(rb.build({
      responseType: 'text',
      accept: '*/*'
    })).pipe(
      filter((r: any) => r instanceof HttpResponse),
      map((r: HttpResponse<any>) => {
        return (r as HttpResponse<any>).clone({ body: undefined }) as StrictHttpResponse<void>;
      })
    );
  }

  /**
   * This method provides access to only to the response body.
   * To access the full response (for headers, for example), `register$Json$Response()` instead.
   *
   * This method sends `application/json` and handles request body of type `application/json`.
   */
  register$Json(params?: {

  
  /**
   * Information about the user that is registered.
   */
  body?: RegistrationDto
  }): Observable<void> {

    return this.register$Json$Response(params).pipe(
      map((r: StrictHttpResponse<void>) => r.body as void)
    );
  }

Expected method in service

...
register$Response(params?: {
...
register(params?: {
...

Other similar methods are generated as expected and we can't find a pattern why this and other methods are generated like this.

@luisfpg luisfpg added the enhancement New feature or request label Jan 28, 2020
@luisfpg
Copy link
Contributor

luisfpg commented Jan 28, 2020

The generator seems to get confused by multiple JSON responses.
What if each of the different responses had different schemas?
First question: is the OpenAPI spec being generated by some tool or have you written it yourself?
I don't understand why mapping with multiple possible JSON responses if on runtime you'll have to pick a single one to sent in the HTTP response.
Anyway, it could be improved for this edge case to detect that there are multiple JSON responses all with the exact same schema, then considering as a single one.

@chrismuellner
Copy link
Author

The swagger.json file is generated by Swashbuckle.AspNetCore (v5.0.0) running on .NET Core API.

Is the problem with the generated OpenAPI spec?

@luisfpg
Copy link
Contributor

luisfpg commented Jan 30, 2020

Not really a "problem", but strange still, because probably in runtime the only returned response type is application/json, so why declaring 3 different content types?
Anyway, ng-openapi-gen can be improved in this case to not generate the suffix.
I can't promess a ETA, though (I'm in the middle of my vacations).

@chrismuellner
Copy link
Author

Thanks for the help!

We've got it working now by restricting the returned response types to only application/json on the API side. Now the generated swagger.json only has a single response type per method.

@elonmallin
Copy link

Had the same problem with Swashbuckle.AspNetCore. Put this attribute on my controllers [Consumes(MediaTypeNames.Application.Json)] to restrict it to just one type.

@luisfpg luisfpg changed the title Methods generated with $Json postfix Correctly handle multiple methods with similar suffixes Feb 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants