-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator?
- 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
Whenever generating aspnetcore
models and controllers, the types used in the controller will reference the models by plain name after using
the .Models
namespace.
This is fine, as long as the package name does not include the name of one of the schemas in the spec.
When this happens, one gets the 'X' is a namespace but is used like a type
error during compilation.
openapi-generator version
7.14.0
OpenAPI declaration file content or url
https://gist.github.com/AZMCode/b45fa4cbf4afa6cf5a6404e3738a1d8d
Generation Details
specs/Product.json
{
"components": {
"schemas": {
"Product": { ... }
}
}
}
openapi-generator.json
{
"packageName": "My.Product.Api.Server" // Just a name, it happens whenever `Product` appears in the path
}
Steps to reproduce
Generate the code, and compile it
Problem
These steps produce the following code for the API at {generatedPath}/My.Product.Api.Server/Controllers/DefaultApi.cs
/*
* Simple Auth & Product API
*
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 1.0.0
*
* Generated by: https://openapi-generator.tech
*/
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Onboarding.Domain.Dtos.Product.Attributes;
using Onboarding.Domain.Dtos.Product.Models;
namespace Onboarding.Domain.Dtos.Product.Controllers
{
/// <summary>
///
/// </summary>
[ApiController]
public abstract class DefaultApiController : ControllerBase
{
/// <summary>
/// Get all products
/// </summary>
/// <response code="200">List of products</response>
/// <response code="401">User was not logged in</response>
[HttpGet]
[Route("/api/products")]
[Authorize]
[ValidateModelState]
[ProducesResponseType(statusCode: 200, type: typeof(List<Product>))]
public abstract IActionResult ProductsGet();
The problematic part is the reference to the type by the plain List<Product>
, where Product
becomes ambiguous between a type and a namespace.
And it fails with the following error
My.Product.Api.Server failed with 3 error(s) (0,3s)
{outputPath}/My.Product.Api.Server/Controllers/DefaultApi.cs(37,66): error CS0118: 'Product' is a namespace but is used like a type
This is a problem for me because I am separating the API into smaller sections, and generating a server for each of them, microservices-style. Problem is these APIs often revolve around a specific model, which tends to be in the package name and/or namespaces.
Related issues/PRs
None that I know of
Suggest a fix
Use the fully qualified name for types in the controller type declarations, and preferably in the rest of the generation to avoid similar bugs.