Skip to content

Commit 0b1b3f5

Browse files
JFCotewing328
andauthored
[ASPNETCORE] Fix having two "?" when not required and nullable = true (#19062)
* fix #18005: Prevent adding 2 times the "?" when not required and nullable = true * fix #18005: Fix carriage return diff * update samples --------- Co-authored-by: William Cheng <[email protected]>
1 parent b897a99 commit 0b1b3f5

File tree

37 files changed

+1633
-1
lines changed

37 files changed

+1633
-1
lines changed

modules/openapi-generator/src/main/resources/aspnetcore/3.0/model.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ namespace {{modelPackage}}
7777
public {{{datatypeWithEnum}}}{{#isNullable}}?{{/isNullable}} {{name}} { get; set; }{{#defaultValue}} = {{{.}}};{{/defaultValue}}
7878
{{/isEnum}}
7979
{{^isEnum}}
80-
public {{{dataType}}}{{#nullableReferenceTypes}}{{^isContainer}}{{^required}}?{{/required}}{{/isContainer}}{{/nullableReferenceTypes}} {{name}} { get; set; }{{#defaultValue}} = {{{.}}};{{/defaultValue}}
80+
public {{{dataType}}}{{#nullableReferenceTypes}}{{^isContainer}}{{^required}}{{^isNullable}}?{{/isNullable}}{{/required}}{{/isContainer}}{{/nullableReferenceTypes}} {{name}} { get; set; }{{#defaultValue}} = {{{.}}};{{/defaultValue}}
8181
{{/isEnum}}
8282
{{^-last}}
8383

modules/openapi-generator/src/test/resources/3_0/aspnetcore/petstore.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,20 @@ paths:
594594
responses:
595595
default:
596596
description: successful operation
597+
/fake/nullable_example_test:
598+
get:
599+
tags:
600+
- fake
601+
summary: Fake endpoint to test nullable example (object)
602+
description: ''
603+
operationId: fake_nullable_example_test
604+
responses:
605+
'200':
606+
description: Successful operation
607+
content:
608+
application/json:
609+
schema:
610+
$ref: '#/components/schemas/TestNullable'
597611
externalDocs:
598612
description: Find out more about Swagger
599613
url: 'http://swagger.io'
@@ -637,6 +651,14 @@ components:
637651
enum:
638652
- A
639653
- B
654+
TestNullable:
655+
type: object
656+
properties:
657+
name:
658+
type: string
659+
nullableName:
660+
type: string
661+
nullable: true
640662
Order:
641663
title: Pet Order
642664
description: An order for a pets from the pet store

samples/server/petstore/aspnetcore-3.0/.openapi-generator/FILES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ src/Org.OpenAPITools/Models/Order.cs
2424
src/Org.OpenAPITools/Models/Pet.cs
2525
src/Org.OpenAPITools/Models/Tag.cs
2626
src/Org.OpenAPITools/Models/TestEnum.cs
27+
src/Org.OpenAPITools/Models/TestNullable.cs
2728
src/Org.OpenAPITools/Models/User.cs
2829
src/Org.OpenAPITools/OpenApi/TypeExtensions.cs
2930
src/Org.OpenAPITools/Program.cs

samples/server/petstore/aspnetcore-3.0/src/Org.OpenAPITools/Controllers/FakeApi.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,30 @@ namespace Org.OpenAPITools.Controllers
2828
[ApiController]
2929
public class FakeApiController : ControllerBase
3030
{
31+
/// <summary>
32+
/// Fake endpoint to test nullable example (object)
33+
/// </summary>
34+
/// <response code="200">Successful operation</response>
35+
[HttpGet]
36+
[Route("/v2/fake/nullable_example_test")]
37+
[ValidateModelState]
38+
[SwaggerOperation("FakeNullableExampleTest")]
39+
[SwaggerResponse(statusCode: 200, type: typeof(TestNullable), description: "Successful operation")]
40+
public virtual IActionResult FakeNullableExampleTest()
41+
{
42+
43+
//TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
44+
// return StatusCode(200, default(TestNullable));
45+
string exampleJson = null;
46+
exampleJson = "{\n \"nullableName\" : \"nullableName\",\n \"name\" : \"name\"\n}";
47+
48+
var example = exampleJson != null
49+
? JsonConvert.DeserializeObject<TestNullable>(exampleJson)
50+
: default(TestNullable);
51+
//TODO: Change the data returned
52+
return new ObjectResult(example);
53+
}
54+
3155
/// <summary>
3256
/// fake endpoint to test parameter example (object)
3357
/// </summary>
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* OpenAPI Petstore
3+
*
4+
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
5+
*
6+
* The version of the OpenAPI document: 1.0.0
7+
*
8+
* Generated by: https://openapi-generator.tech
9+
*/
10+
11+
using System;
12+
using System.Linq;
13+
using System.Text;
14+
using System.Collections.Generic;
15+
using System.ComponentModel;
16+
using System.ComponentModel.DataAnnotations;
17+
using System.Runtime.Serialization;
18+
using Newtonsoft.Json;
19+
using Org.OpenAPITools.Converters;
20+
21+
namespace Org.OpenAPITools.Models
22+
{
23+
/// <summary>
24+
///
25+
/// </summary>
26+
[DataContract]
27+
public partial class TestNullable : IEquatable<TestNullable>
28+
{
29+
/// <summary>
30+
/// Gets or Sets Name
31+
/// </summary>
32+
[DataMember(Name="name", EmitDefaultValue=false)]
33+
public string Name { get; set; }
34+
35+
/// <summary>
36+
/// Gets or Sets NullableName
37+
/// </summary>
38+
[DataMember(Name="nullableName", EmitDefaultValue=true)]
39+
public string NullableName { get; set; }
40+
41+
/// <summary>
42+
/// Returns the string presentation of the object
43+
/// </summary>
44+
/// <returns>String presentation of the object</returns>
45+
public override string ToString()
46+
{
47+
var sb = new StringBuilder();
48+
sb.Append("class TestNullable {\n");
49+
sb.Append(" Name: ").Append(Name).Append("\n");
50+
sb.Append(" NullableName: ").Append(NullableName).Append("\n");
51+
sb.Append("}\n");
52+
return sb.ToString();
53+
}
54+
55+
/// <summary>
56+
/// Returns the JSON string presentation of the object
57+
/// </summary>
58+
/// <returns>JSON string presentation of the object</returns>
59+
public string ToJson()
60+
{
61+
return JsonConvert.SerializeObject(this, Formatting.Indented);
62+
}
63+
64+
/// <summary>
65+
/// Returns true if objects are equal
66+
/// </summary>
67+
/// <param name="obj">Object to be compared</param>
68+
/// <returns>Boolean</returns>
69+
public override bool Equals(object obj)
70+
{
71+
if (obj is null) return false;
72+
if (ReferenceEquals(this, obj)) return true;
73+
return obj.GetType() == GetType() && Equals((TestNullable)obj);
74+
}
75+
76+
/// <summary>
77+
/// Returns true if TestNullable instances are equal
78+
/// </summary>
79+
/// <param name="other">Instance of TestNullable to be compared</param>
80+
/// <returns>Boolean</returns>
81+
public bool Equals(TestNullable other)
82+
{
83+
if (other is null) return false;
84+
if (ReferenceEquals(this, other)) return true;
85+
86+
return
87+
(
88+
Name == other.Name ||
89+
Name != null &&
90+
Name.Equals(other.Name)
91+
) &&
92+
(
93+
NullableName == other.NullableName ||
94+
NullableName != null &&
95+
NullableName.Equals(other.NullableName)
96+
);
97+
}
98+
99+
/// <summary>
100+
/// Gets the hash code
101+
/// </summary>
102+
/// <returns>Hash code</returns>
103+
public override int GetHashCode()
104+
{
105+
unchecked // Overflow is fine, just wrap
106+
{
107+
var hashCode = 41;
108+
// Suitable nullity checks etc, of course :)
109+
if (Name != null)
110+
hashCode = hashCode * 59 + Name.GetHashCode();
111+
if (NullableName != null)
112+
hashCode = hashCode * 59 + NullableName.GetHashCode();
113+
return hashCode;
114+
}
115+
}
116+
117+
#region Operators
118+
#pragma warning disable 1591
119+
120+
public static bool operator ==(TestNullable left, TestNullable right)
121+
{
122+
return Equals(left, right);
123+
}
124+
125+
public static bool operator !=(TestNullable left, TestNullable right)
126+
{
127+
return !Equals(left, right);
128+
}
129+
130+
#pragma warning restore 1591
131+
#endregion Operators
132+
}
133+
}

samples/server/petstore/aspnetcore-3.0/src/Org.OpenAPITools/wwwroot/openapi-original.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,26 @@
812812
"summary" : "fake endpoint to test parameter example (object)",
813813
"tags" : [ "fake" ]
814814
}
815+
},
816+
"/fake/nullable_example_test" : {
817+
"get" : {
818+
"description" : "",
819+
"operationId" : "fake_nullable_example_test",
820+
"responses" : {
821+
"200" : {
822+
"content" : {
823+
"application/json" : {
824+
"schema" : {
825+
"$ref" : "#/components/schemas/TestNullable"
826+
}
827+
}
828+
},
829+
"description" : "Successful operation"
830+
}
831+
},
832+
"summary" : "Fake endpoint to test nullable example (object)",
833+
"tags" : [ "fake" ]
834+
}
815835
}
816836
},
817837
"components" : {
@@ -852,6 +872,22 @@
852872
"enum" : [ "A", "B" ],
853873
"type" : "string"
854874
},
875+
"TestNullable" : {
876+
"example" : {
877+
"nullableName" : "nullableName",
878+
"name" : "name"
879+
},
880+
"properties" : {
881+
"name" : {
882+
"type" : "string"
883+
},
884+
"nullableName" : {
885+
"nullable" : true,
886+
"type" : "string"
887+
}
888+
},
889+
"type" : "object"
890+
},
855891
"Order" : {
856892
"description" : "An order for a pets from the pet store",
857893
"example" : {

samples/server/petstore/aspnetcore-3.1/.openapi-generator/FILES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ src/Org.OpenAPITools/Models/Order.cs
2424
src/Org.OpenAPITools/Models/Pet.cs
2525
src/Org.OpenAPITools/Models/Tag.cs
2626
src/Org.OpenAPITools/Models/TestEnum.cs
27+
src/Org.OpenAPITools/Models/TestNullable.cs
2728
src/Org.OpenAPITools/Models/User.cs
2829
src/Org.OpenAPITools/OpenApi/TypeExtensions.cs
2930
src/Org.OpenAPITools/Program.cs

samples/server/petstore/aspnetcore-3.1/src/Org.OpenAPITools/Controllers/FakeApi.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,30 @@ namespace Org.OpenAPITools.Controllers
2828
[ApiController]
2929
public class FakeApiController : ControllerBase
3030
{
31+
/// <summary>
32+
/// Fake endpoint to test nullable example (object)
33+
/// </summary>
34+
/// <response code="200">Successful operation</response>
35+
[HttpGet]
36+
[Route("/v2/fake/nullable_example_test")]
37+
[ValidateModelState]
38+
[SwaggerOperation("FakeNullableExampleTest")]
39+
[SwaggerResponse(statusCode: 200, type: typeof(TestNullable), description: "Successful operation")]
40+
public virtual IActionResult FakeNullableExampleTest()
41+
{
42+
43+
//TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
44+
// return StatusCode(200, default(TestNullable));
45+
string exampleJson = null;
46+
exampleJson = "{\n \"nullableName\" : \"nullableName\",\n \"name\" : \"name\"\n}";
47+
48+
var example = exampleJson != null
49+
? JsonConvert.DeserializeObject<TestNullable>(exampleJson)
50+
: default(TestNullable);
51+
//TODO: Change the data returned
52+
return new ObjectResult(example);
53+
}
54+
3155
/// <summary>
3256
/// fake endpoint to test parameter example (object)
3357
/// </summary>

0 commit comments

Comments
 (0)