Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ASPNETCORE] Fix having two "?" when not required and nullable = true #19062

Merged
merged 3 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading