OpenAPITools / OpenAPITools/openapi-generator

[BUG] [ASPNETCORE] - Invalid model namespace added if no models exist

Open
#13,113 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Generate an OpenAPI declaration file, which contains no models for ASPNETCORE. In the generated controllers an invalid namespace to Models is added, which does not exist.

openapi-generator version

Version: 6.0.1

OpenAPI declaration file content or url

device_registration_api.yaml:

openapi: 3.0.1
info:
  title: Device Registration API
  description: API of the device registration server where phones get their configuration and firmware.
  version: 1.0.0
  contact:
    name: WD
servers:
  - url: 'http://localhost:3000'
tags:
  - name: API
    description: ''
paths:
  '/CTL{MAC}.tlv':
    get:
      tags:
        - API
      summary: Get CTL file of the given MAC address
      description: 'This file is not necessarily required by the phone for the registration process. To keep the file system organized, CTL files should be looked up in a subfolder `ctl` or may be retrieved from a database.'
      operationId: getCTLByMACAddress
      parameters:
        - name: MAC
          in: path
          description: The MAC address of the phone requesting the CTL file
          required: true
          schema:
            type: string
      responses:
        '200':
          description: Requested file is found and is returned to the phone
        '404':
          description: Not Found
    parameters:
      - schema:
          type: string
        name: MAC
        in: path
        required: true
        description: The MAC address of the requesting device
Generation Details

openApiGeneratorConfig.json:

{
  "packageName": "DeviceRegistrationApi",
  "packageTitle": "DeviceRegistrationApi",
  "packageCopyright": "WD2023",
  "packageVersion": "1.0",
  "aspnetCoreVersion": "6.0",
  "buildTarget": "library",
  "isLibrary": "true",
  "operationIsAsync": "true",
  "operationResultTask": "true",
  "nullableReferenceTypes": "true",
  "operationModifier": "abstract",
  "swashbuckleVersion": "6.0.0",
  "useNewtonsoft": "false",
  "sourceFolder": ""
}
Steps to reproduce
  1. Create file device_registration_api.yaml containing the OpenAPI declaration
  2. Create file openApiGeneratorConfig.json containing the generation details
  3. Run openapi-generator-cli generate -i .\device_registration_api.yaml -g aspnetcore -c .\openApiGeneratorConfig.json -o .\out
  4. Open file out/DeviceRegistrationApi/Controllers/APIApi.cs, which contains the line using DeviceRegistrationApi.Models;, however namespace Models does not exist and thus building the application will fail.
Related issues/PRs

No related issues found

Suggest a fix

As no models are specified in the OpenAPI file, no models are generated and thus no Models namespace exists, which is correct. So the controllers should not have a namespace to Models, however the generator still adds a reference, which is wrong. I assume, controller generation does not check if there were models generated.

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

Reproduce the issue with device_registration_api.yaml and openApiGeneratorConfig.json using the listed aspnetcore generation command. Inspect out/DeviceRegistrationApi/Controllers/APIApi.cs and verify the generated project when no models exist. Done means the controller does not reference the nonexistent DeviceRegistrationApi.Models namespace and the output builds successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, openapi
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.