Skip to content

Commit

Permalink
[ASPNETCORE] Fix having two "?" when not required and nullable = true (
Browse files Browse the repository at this point in the history
…#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 <wing328hk@gmail.com>
  • Loading branch information
JFCote and wing328 authored Jul 6, 2024
1 parent b897a99 commit 0b1b3f5
Show file tree
Hide file tree
Showing 37 changed files with 1,633 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ namespace {{modelPackage}}
public {{{datatypeWithEnum}}}{{#isNullable}}?{{/isNullable}} {{name}} { get; set; }{{#defaultValue}} = {{{.}}};{{/defaultValue}}
{{/isEnum}}
{{^isEnum}}
public {{{dataType}}}{{#nullableReferenceTypes}}{{^isContainer}}{{^required}}?{{/required}}{{/isContainer}}{{/nullableReferenceTypes}} {{name}} { get; set; }{{#defaultValue}} = {{{.}}};{{/defaultValue}}
public {{{dataType}}}{{#nullableReferenceTypes}}{{^isContainer}}{{^required}}{{^isNullable}}?{{/isNullable}}{{/required}}{{/isContainer}}{{/nullableReferenceTypes}} {{name}} { get; set; }{{#defaultValue}} = {{{.}}};{{/defaultValue}}
{{/isEnum}}
{{^-last}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,20 @@ paths:
responses:
default:
description: successful operation
/fake/nullable_example_test:
get:
tags:
- fake
summary: Fake endpoint to test nullable example (object)
description: ''
operationId: fake_nullable_example_test
responses:
'200':
description: Successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/TestNullable'
externalDocs:
description: Find out more about Swagger
url: 'http://swagger.io'
Expand Down Expand Up @@ -637,6 +651,14 @@ components:
enum:
- A
- B
TestNullable:
type: object
properties:
name:
type: string
nullableName:
type: string
nullable: true
Order:
title: Pet Order
description: An order for a pets from the pet store
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ src/Org.OpenAPITools/Models/Order.cs
src/Org.OpenAPITools/Models/Pet.cs
src/Org.OpenAPITools/Models/Tag.cs
src/Org.OpenAPITools/Models/TestEnum.cs
src/Org.OpenAPITools/Models/TestNullable.cs
src/Org.OpenAPITools/Models/User.cs
src/Org.OpenAPITools/OpenApi/TypeExtensions.cs
src/Org.OpenAPITools/Program.cs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,30 @@ namespace Org.OpenAPITools.Controllers
[ApiController]
public class FakeApiController : ControllerBase
{
/// <summary>
/// Fake endpoint to test nullable example (object)
/// </summary>
/// <response code="200">Successful operation</response>
[HttpGet]
[Route("/v2/fake/nullable_example_test")]
[ValidateModelState]
[SwaggerOperation("FakeNullableExampleTest")]
[SwaggerResponse(statusCode: 200, type: typeof(TestNullable), description: "Successful operation")]
public virtual IActionResult FakeNullableExampleTest()
{

//TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
// return StatusCode(200, default(TestNullable));
string exampleJson = null;
exampleJson = "{\n \"nullableName\" : \"nullableName\",\n \"name\" : \"name\"\n}";

var example = exampleJson != null
? JsonConvert.DeserializeObject<TestNullable>(exampleJson)
: default(TestNullable);
//TODO: Change the data returned
return new ObjectResult(example);
}

/// <summary>
/// fake endpoint to test parameter example (object)
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* OpenAPI Petstore
*
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* The version of the OpenAPI document: 1.0.0
*
* Generated by: https://openapi-generator.tech
*/

using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Org.OpenAPITools.Converters;

namespace Org.OpenAPITools.Models
{
/// <summary>
///
/// </summary>
[DataContract]
public partial class TestNullable : IEquatable<TestNullable>
{
/// <summary>
/// Gets or Sets Name
/// </summary>
[DataMember(Name="name", EmitDefaultValue=false)]
public string Name { get; set; }

/// <summary>
/// Gets or Sets NullableName
/// </summary>
[DataMember(Name="nullableName", EmitDefaultValue=true)]
public string NullableName { get; set; }

/// <summary>
/// Returns the string presentation of the object
/// </summary>
/// <returns>String presentation of the object</returns>
public override string ToString()
{
var sb = new StringBuilder();
sb.Append("class TestNullable {\n");
sb.Append(" Name: ").Append(Name).Append("\n");
sb.Append(" NullableName: ").Append(NullableName).Append("\n");
sb.Append("}\n");
return sb.ToString();
}

/// <summary>
/// Returns the JSON string presentation of the object
/// </summary>
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

/// <summary>
/// Returns true if objects are equal
/// </summary>
/// <param name="obj">Object to be compared</param>
/// <returns>Boolean</returns>
public override bool Equals(object obj)
{
if (obj is null) return false;
if (ReferenceEquals(this, obj)) return true;
return obj.GetType() == GetType() && Equals((TestNullable)obj);
}

/// <summary>
/// Returns true if TestNullable instances are equal
/// </summary>
/// <param name="other">Instance of TestNullable to be compared</param>
/// <returns>Boolean</returns>
public bool Equals(TestNullable other)
{
if (other is null) return false;
if (ReferenceEquals(this, other)) return true;

return
(
Name == other.Name ||
Name != null &&
Name.Equals(other.Name)
) &&
(
NullableName == other.NullableName ||
NullableName != null &&
NullableName.Equals(other.NullableName)
);
}

/// <summary>
/// Gets the hash code
/// </summary>
/// <returns>Hash code</returns>
public override int GetHashCode()
{
unchecked // Overflow is fine, just wrap
{
var hashCode = 41;
// Suitable nullity checks etc, of course :)
if (Name != null)
hashCode = hashCode * 59 + Name.GetHashCode();
if (NullableName != null)
hashCode = hashCode * 59 + NullableName.GetHashCode();
return hashCode;
}
}

#region Operators
#pragma warning disable 1591

public static bool operator ==(TestNullable left, TestNullable right)
{
return Equals(left, right);
}

public static bool operator !=(TestNullable left, TestNullable right)
{
return !Equals(left, right);
}

#pragma warning restore 1591
#endregion Operators
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,26 @@
"summary" : "fake endpoint to test parameter example (object)",
"tags" : [ "fake" ]
}
},
"/fake/nullable_example_test" : {
"get" : {
"description" : "",
"operationId" : "fake_nullable_example_test",
"responses" : {
"200" : {
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/TestNullable"
}
}
},
"description" : "Successful operation"
}
},
"summary" : "Fake endpoint to test nullable example (object)",
"tags" : [ "fake" ]
}
}
},
"components" : {
Expand Down Expand Up @@ -852,6 +872,22 @@
"enum" : [ "A", "B" ],
"type" : "string"
},
"TestNullable" : {
"example" : {
"nullableName" : "nullableName",
"name" : "name"
},
"properties" : {
"name" : {
"type" : "string"
},
"nullableName" : {
"nullable" : true,
"type" : "string"
}
},
"type" : "object"
},
"Order" : {
"description" : "An order for a pets from the pet store",
"example" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ src/Org.OpenAPITools/Models/Order.cs
src/Org.OpenAPITools/Models/Pet.cs
src/Org.OpenAPITools/Models/Tag.cs
src/Org.OpenAPITools/Models/TestEnum.cs
src/Org.OpenAPITools/Models/TestNullable.cs
src/Org.OpenAPITools/Models/User.cs
src/Org.OpenAPITools/OpenApi/TypeExtensions.cs
src/Org.OpenAPITools/Program.cs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,30 @@ namespace Org.OpenAPITools.Controllers
[ApiController]
public class FakeApiController : ControllerBase
{
/// <summary>
/// Fake endpoint to test nullable example (object)
/// </summary>
/// <response code="200">Successful operation</response>
[HttpGet]
[Route("/v2/fake/nullable_example_test")]
[ValidateModelState]
[SwaggerOperation("FakeNullableExampleTest")]
[SwaggerResponse(statusCode: 200, type: typeof(TestNullable), description: "Successful operation")]
public virtual IActionResult FakeNullableExampleTest()
{

//TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
// return StatusCode(200, default(TestNullable));
string exampleJson = null;
exampleJson = "{\n \"nullableName\" : \"nullableName\",\n \"name\" : \"name\"\n}";

var example = exampleJson != null
? JsonConvert.DeserializeObject<TestNullable>(exampleJson)
: default(TestNullable);
//TODO: Change the data returned
return new ObjectResult(example);
}

/// <summary>
/// fake endpoint to test parameter example (object)
/// </summary>
Expand Down
Loading

0 comments on commit 0b1b3f5

Please sign in to comment.