Skip to content

Support EnumDataType annotation in JSON schema generation#1890

Merged
lahma merged 2 commits intomasterfrom
copilot/support-enumdatatype-annotation
Mar 2, 2026
Merged

Support EnumDataType annotation in JSON schema generation#1890
lahma merged 2 commits intomasterfrom
copilot/support-enumdatatype-annotation

Conversation

Copy link
Contributor

Copilot AI commented Mar 1, 2026

[EnumDataType(typeof(MyEnum))] on a string or int property was silently ignored during schema generation, producing a plain string/integer schema instead of an enum schema with the valid values.

Changes

  • ReflectionServiceBase.GetDescription() — After checking for JsonSchemaTypeAttribute, now also checks for EnumDataTypeAttribute. When present and the referenced type is an enum, the property's effective type is replaced with the enum type before description generation — consistent with how JsonSchemaTypeAttribute already works.
  • EnumGenerationTests — Added tests for string and int properties annotated with [EnumDataType].

Example

public class MyModel
{
    [EnumDataType(typeof(Status))]
    public string StatusValue { get; set; }  // now generates enum schema with Status values
}

public enum Status { Active, Inactive, Pending }

The generated schema for StatusValue will now include an enum with the values from Status rather than being typed as a plain string.

Original prompt

This section details on the original issue you should resolve

<issue_title>Question: Support for EnumDataType Annotation</issue_title>
<issue_description>When using the EnumDataType Annotation to map an enum type to a string the enum type is being ignored. Unfortunately I am modifying some legacy code and would like to be able to generate a schema which includes an enum for allowable values.

This can be done in Json.Net but I am preferring the additional attributed provided by NJsonSchema.

Have I missed a setting or is the EnumDataType attribute not yet supported?

Thanks</issue_description>

Comments on the Issue (you are @copilot in this section)

@RicoSuter Is this something which correctly works with Newtonsoft.Json but is not correctly picked up (same way) by NJS? So is NJS not behaving the same way as Newtonsoft.Json? If this is the case we should add it. @RicoSuter We need to either fix it here: https://github.com/RicoSuter/NJsonSchema/blob/master/src/NJsonSchema/Generation/JsonSchemaGenerator.cs#L680

And/Or here:
https://github.com/RicoSuter/NJsonSchema/blob/master/src/NJsonSchema/Generation/DefaultReflectionService.cs#L263</comment_new>


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Add support for EnumDataType annotation in NJsonSchema Support EnumDataType annotation in JSON schema generation Mar 1, 2026
Copilot finished work on behalf of lahma March 1, 2026 20:06
@lahma lahma marked this pull request as ready for review March 2, 2026 10:15
@lahma lahma merged commit 6e216ef into master Mar 2, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Question: Support for EnumDataType Annotation

2 participants