Skip to content

Commit

Permalink
Required or not required, that's the question.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel committed Feb 26, 2024
1 parent 066ae84 commit 44b6f2e
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ namespace PetShopBoys;
public sealed partial record Address
{
[System.Text.Json.Serialization.JsonPropertyName("street")]
[Qowaiv.Validation.DataAnnotations.Optional]
[System.ComponentModel.DataAnnotations.Required]
public string? Street { get; init; }

[System.Text.Json.Serialization.JsonPropertyName("city")]
[Qowaiv.Validation.DataAnnotations.Optional]
[System.ComponentModel.DataAnnotations.Required]
public string? City { get; init; }

[System.Text.Json.Serialization.JsonPropertyName("state")]
[Qowaiv.Validation.DataAnnotations.Optional]
[System.ComponentModel.DataAnnotations.Required]
public string? State { get; init; }

[System.Text.Json.Serialization.JsonPropertyName("zip")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public sealed partial record ApiResponse
public int Code { get; init; }

[System.Text.Json.Serialization.JsonPropertyName("type")]
[Qowaiv.Validation.DataAnnotations.Optional]
[System.ComponentModel.DataAnnotations.Required]
public string? Type { get; init; }

[System.Text.Json.Serialization.JsonPropertyName("message")]
[Qowaiv.Validation.DataAnnotations.Optional]
[System.ComponentModel.DataAnnotations.Required]
public string? Message { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public sealed partial record Category
public long Id { get; init; }

[System.Text.Json.Serialization.JsonPropertyName("name")]
[Qowaiv.Validation.DataAnnotations.Optional]
[System.ComponentModel.DataAnnotations.Required]
public string? Name { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public sealed partial record Customer
public long Id { get; init; }

[System.Text.Json.Serialization.JsonPropertyName("username")]
[Qowaiv.Validation.DataAnnotations.Optional]
[System.ComponentModel.DataAnnotations.Required]
public string? Username { get; init; }

[System.Text.Json.Serialization.JsonPropertyName("address")]
[Qowaiv.Validation.DataAnnotations.Optional]
[Qowaiv.Validation.DataAnnotations.Any]
public PetShopBoys.Address[] Address { get; init; } = System.Array.Empty<PetShopBoys.Address>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ public sealed partial record Pet
public string? Name { get; init; }

[System.Text.Json.Serialization.JsonPropertyName("category")]
[Qowaiv.Validation.DataAnnotations.Optional]
[System.ComponentModel.DataAnnotations.Required]
public PetShopBoys.Category? Category { get; init; }

[System.Text.Json.Serialization.JsonPropertyName("photoUrls")]
[Qowaiv.Validation.DataAnnotations.Any]
public string[] PhotoUrls { get; init; } = System.Array.Empty<string>();

[System.Text.Json.Serialization.JsonPropertyName("tags")]
[Qowaiv.Validation.DataAnnotations.Optional]
[Qowaiv.Validation.DataAnnotations.Any]
public PetShopBoys.Tag[] Tags { get; init; } = System.Array.Empty<PetShopBoys.Tag>();

/// <summary>pet status in the store</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public sealed partial record Tag
public long Id { get; init; }

[System.Text.Json.Serialization.JsonPropertyName("name")]
[Qowaiv.Validation.DataAnnotations.Optional]
[System.ComponentModel.DataAnnotations.Required]
public string? Name { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ public sealed partial record User
public long Id { get; init; }

[System.Text.Json.Serialization.JsonPropertyName("username")]
[Qowaiv.Validation.DataAnnotations.Optional]
[System.ComponentModel.DataAnnotations.Required]
public string? Username { get; init; }

[System.Text.Json.Serialization.JsonPropertyName("firstName")]
[Qowaiv.Validation.DataAnnotations.Optional]
[System.ComponentModel.DataAnnotations.Required]
public string? FirstName { get; init; }

[System.Text.Json.Serialization.JsonPropertyName("lastName")]
[Qowaiv.Validation.DataAnnotations.Optional]
[System.ComponentModel.DataAnnotations.Required]
public string? LastName { get; init; }

[System.Text.Json.Serialization.JsonPropertyName("email")]
[Qowaiv.Validation.DataAnnotations.Optional]
public Qowaiv.EmailAddress Email { get; init; }

[System.Text.Json.Serialization.JsonPropertyName("password")]
[Qowaiv.Validation.DataAnnotations.Optional]
[System.ComponentModel.DataAnnotations.Required]
public string? Password { get; init; }

[System.Text.Json.Serialization.JsonPropertyName("phone")]
[Qowaiv.Validation.DataAnnotations.Optional]
[System.ComponentModel.DataAnnotations.Required]
public string? Phone { get; init; }

/// <summary>User Status</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public partial class OpenApiTypeResolver
propertyType = AsNullable(propertyType);
}

var isRequired = required.Contains(schema.Path.Last)
|| (!schema.Nullable && !propertyType.IsValueType);

var attributes = new List<AttributeInfo>();
var documentation = new XmlDocumentation
{
Expand All @@ -48,7 +51,7 @@ public partial class OpenApiTypeResolver
attributes,
documentation,
nullable,
required.Contains(schema.Path.Last));
isRequired);

attributes.AddRange(DecorateProperty(prop, schema));
return prop;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Version>0.0.1-alpha-006</Version>
<Version>0.0.1-alpha-005</Version>
<Version>0.0.1-alpha-007</Version>
<Version>0.0.1-alpha-008</Version>
<PackageId>Qowaiv.CodeGeneration.OpenApi</PackageId>
<PackageReleaseNotes>
ToBeReleased
Expand Down

0 comments on commit 44b6f2e

Please sign in to comment.