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

Request body is mapped to object instead of DTO type #4093

Open
mjkoffice opened this issue Jul 27, 2022 · 1 comment · May be fixed by #4747
Open

Request body is mapped to object instead of DTO type #4093

mjkoffice opened this issue Jul 27, 2022 · 1 comment · May be fixed by #4747

Comments

@mjkoffice
Copy link

I've tried to generate controller code (using openApiToCSharpController) from following OpenAPI specification. But it seems, that the POST request body is mapped to an object parameter each time. And not using the referenced schema definition's type.
Hint: Without the $ref: "#/components/requestBodies/Room" (using the Room schema directly) the Room type is used as expected.
It generates:

[HttpPost, Route("rooms", Name = "createRoom")]
public abstract System.Threading.Tasks.Task<Room> CreateRoom([FromBody] [BindRequired] Room body);

Are there any settings affecting the request body mapping?
Thanks in advance for any further information.

Generated controller base class with create method:

[System.CodeDom.Compiler.GeneratedCode("NSwag", "13.16.1.0 (NJsonSchema v10.7.2.0 (Newtonsoft.Json v13.0.0.0))")]
public abstract class RoomsControllerBase : ControllerBase
{
	/// <returns>single room</returns>
	[HttpPost, Route("rooms", Name = "createRoom")]
	public abstract System.Threading.Tasks.Task<Room> CreateRoom([FromBody] object? body = null);
}

The source OpenAPI specification used for the test:

openapi: "3.0.3"

info:
  version: "1.0.0"
  title: Multifile Example
  description: Lorem ipsum
  contact:
    email: [email protected]
servers:
  - url: .

tags:
  - name: rooms

paths:
  /rooms:
    post:
      description: create a room
      operationId: createRoom
      tags:
        - rooms
      requestBody:
        $ref: "#/components/requestBodies/Room"
      responses:
        "201":
          $ref: "#/components/responses/Room"
        "422":
          $ref: "#/components/responses/Error"
        default:
          $ref: "#/components/responses/Error"
components:
  responses:
    Error:
      description: Error occurred
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/Error"
    Room:
      description: single room
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/Room"
  requestBodies:
    Room:
      required: true
      description: room to add
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/Room"
  schemas:
    DefaultObject:
      type: object
      description: Default object
      properties:
        id:
          type: integer
          readOnly: true
          description: id
          example: 1
        createdAt:
          type: string
          format: date-time
          readOnly: true
          description: timestamp
          example: "2020-04-12T23:20:50.52Z"
    Error:
      type: object
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string
    Room:
      allOf:
        - $ref: "#/components/schemas/DefaultObject"
        - type: object
          properties:
            number:
              type: string
              description: The room number
              example: "4711"
              minLength: 0
              maxLength: 20
            name:
              type: string
              description: The name of the room
              example: MyRoom
              minLength: 2
              maxLength: 255
            bookable:
              type: boolean
              description: Wether this room is bookable or not
              example: true
          required:
            - number
            - bookable
@lakeman
Copy link

lakeman commented Sep 16, 2022

+1
I've also hit this when generating a C# client.
The spec allows for requestBody to be either a Request Body Object, or a Reference Object.
So the problem is the implementation not conforming to the standard.

@Eruzo Eruzo linked a pull request Feb 6, 2024 that will close this issue
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 a pull request may close this issue.

2 participants