OpenAPITools / OpenAPITools/openapi-generator
[BUG] [aspnetcore] useNewtonsoft option set to false leads to non running code generated
Nobody has claimed this yet.
- 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
When using the generator to create an asp.net webserver using aspnetcore generator, the option useNewtonsoft=false leads to a broken application (It does not compile, because Newronsoft.Json is still used in some places)
Following errors are shown:
test\src\Org.OpenAPITools\Startup.cs(62,26,62,44): error CS1061: 'JsonOptions' does not contain a definition for 'SerializerSettings' and no accessible extension method 'SerializerSettings' accepting a first argument of type 'JsonOptions' could be found (are you missing a using directive or an assembly reference?)
test\src\Org.OpenAPITools\Startup.cs(62,68,62,106): error CS0246: The type or namespace name 'CamelCasePropertyNamesContractResolver' could not be found (are you missing a using directive or an assembly reference?)
\test\src\Org.OpenAPITools\Startup.cs(63,26,63,44): error CS1061: 'JsonOptions' does not contain a definition for 'SerializerSettings' and no accessible extension method 'SerializerSettings' accepting a first argument of type 'JsonOptions' could be found (are you missing a using directive or an assembly reference?)
test\src\Org.OpenAPITools\Startup.cs(63,64,63,83): error CS0246: The type or namespace name 'StringEnumConverter' could not be found (are you missing a using directive or an assembly reference?)
openapi-generator version
6.3.0
OpenAPI declaration file content or url
openapi: "3.1.0"
info:
title: enum-example
version: "0.1"
servers:
- url: /api
paths:
/test:
get:
operationId: getEnum
summary: Get enumeration value
tags:
- test
responses:
"200":
description: OK
content:
application/json:
schema:
allOf:
- type: object
properties:
data:
$ref: "#/components/schemas/ExampleEnum"
components:
schemas:
ExampleEnum:
type: string
example: option1
enum:
- option1
- option2
- option3
Generation Details
Run openapi-generator-cli.cmd generate -i [PATH_TO_YAML] -g aspnetcore -o [OUTPUT_PATH] --additional-properties=useNewtonsoft=false,aspnetCoreVersion=6.0,compatibilityVersion=6.0,generateBody=false,classModifier=abstract,operationResultTask=true
Steps to reproduce
Run openapi-generator-cli.cmd generate -i [PATH_TO_YAML] -g aspnetcore -o [OUTPUT_PATH] --additional-properties=useNewtonsoft=false,aspnetCoreVersion=6.0
Related issues/PRs
https://github.com/OpenAPITools/openapi-generator/issues/12952
Suggest a fix
Use System.Text.Json when useNewtonsoft is set to false or alternatively use System.Text.Json as default starting with .net core version 6.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the issue with the provided OpenAPI YAML and aspnetcore generation command using useNewtonsoft=false. Inspect the generated Startup.cs around the reported lines and trace the aspnetcore generator templates that produce them. Done means the generated .NET 6 application compiles without Newtonsoft.Json references when the option is disabled.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 50/100