├── src
├── NJsonSchema.CodeGeneration.CSharp
│ ├── Templates
│ │ ├── Class.Body.liquid
│ │ ├── Class.Annotations.liquid
│ │ ├── Class.Constructor.liquid
│ │ ├── Enum.Annotations.liquid
│ │ ├── Enum.Member.Annotations.liquid
│ │ ├── Class.Property.Annotations.liquid
│ │ ├── Class.Inheritance.liquid
│ │ ├── File.liquid
│ │ ├── Class.Inpc.liquid
│ │ ├── Class.ToJson.liquid
│ │ ├── JsonInheritanceAttribute.liquid
│ │ └── Class.FromJson.liquid
│ ├── NuGetIcon.png
│ ├── CSharpJsonPolymorphicSerializationStyle.cs
│ ├── CSharpJsonLibrary.cs
│ ├── NJsonSchema.CodeGeneration.CSharp.csproj
│ └── Models
│ │ └── FileTemplateModel.cs
├── NJsonSchema.snk
├── NJsonSchema.Tests
│ ├── References
│ │ ├── LocalReferencesTests
│ │ │ ├── dir_with_#
│ │ │ │ ├── first.json
│ │ │ │ └── second.json
│ │ │ ├── collection.json
│ │ │ ├── schema_with_reference.json
│ │ │ ├── schema_with_indirect_subreference.json
│ │ │ ├── animal.json
│ │ │ ├── schema_with_indirect_reference.json
│ │ │ └── schema_with_collection_reference.json
│ │ └── Snapshots
│ │ │ └── LocalReferencesTests.When_schema_references_external_schema_then_it_is_inlined_with_ToJson.verified.txt
│ ├── VerifyChecksTests.cs
│ ├── Generation
│ │ ├── Snapshots
│ │ │ ├── JsonInheritanceConverterTests.When_serializing_discriminator_property_is_overwritten_if_already_present.verified.txt
│ │ │ ├── JsonInheritanceConverterTests.When_serializing_discriminator_property_is_set.verified.txt
│ │ │ ├── EnumTests.When_schema_has_x_enum_names_then_backward_compatibility_works.verified.txt
│ │ │ ├── EnumTests.When_schema_has_x_enum_varnames_then_backward_compatibility_works.verified.txt
│ │ │ ├── EnumTests.When_schema_has_x_enum_descriptions_then_backward_compatibility_works.verified.txt
│ │ │ ├── XmlDocsTests.When_example_xml_docs_is_defined_then_examples_can_be_defined.verified.txt
│ │ │ ├── EnumTests.When_enum_has_no_description_attributes_then_descriptions_are_not_included_in_schema.verified.txt
│ │ │ ├── EnumTests.When_enum_has_description_attributes_then_descriptions_are_included_in_schema.verified.txt
│ │ │ └── SchemaGenerationTests.When_output_schema_contains_reference_then_schema_reference_path_is_human_readable.verified.txt
│ │ ├── SystemTextJson
│ │ │ ├── Snapshots
│ │ │ │ ├── SystemTextJsonTests.When_type_is_excluded_then_it_should_not_be_in_the_schema.verified.txt
│ │ │ │ ├── SystemTextJsonTests.When_type_is_excluded_with_json_schema_ignore_attribute_then_it_should_not_be_in_the_schema.verified.txt
│ │ │ │ └── SystemTextJsonTests.When_property_is_readonly_then_its_in_the_schema.verified.txt
│ │ │ ├── SystemTextJsonEnumTests.WhenIntegerEnumUseEnumNameAttributes_ThenTheyAreIgnored.verified.txt
│ │ │ ├── SystemTextJsonEnumTests.WhenStringEnumUsesEnumMemberAttribute_ThenItIsUsed.verified.txt
│ │ │ └── SystemTextJsonEnumTests.WhenStringEnumUsesJsonStringEnumMemberName_ThenItIsUsed.verified.txt
│ │ ├── RecordTests.cs
│ │ ├── FieldGenerationTests.cs
│ │ ├── ShouldSerializeTests.cs
│ │ ├── ExceptionTypeTests.cs
│ │ ├── DefaultSchemaNameGeneratorTests.cs
│ │ └── XmlDocsTests.cs
│ ├── Schema
│ │ └── Snapshots
│ │ │ └── JsonSchemaTests.When_setting_single_type_then_it_should_be_serialized_correctly.verified.txt
│ ├── Serialization
│ │ └── Snapshots
│ │ │ └── DiscriminatorSerializationTests.When_discriminator_object_is_set_then_schema_is_correctly_serialized.verified.txt
│ ├── Deserialization
│ │ └── DeserializationTests.cs
│ └── VerifyHelper.cs
├── NJsonSchema
│ ├── NuGetIcon.png
│ ├── SampleJsonSchemaGeneratorSettings.cs
│ ├── app.config
│ ├── Generation
│ │ ├── ISchemaProcessor.cs
│ │ ├── ISchemaNameGenerator.cs
│ │ └── ReferenceTypeNullHandling.cs
│ ├── Infrastructure
│ │ └── CollectionExtensions.cs
│ ├── IDocumentPathProvider.cs
│ ├── IJsonExtensionObject.cs
│ ├── SchemaType.cs
│ ├── SampleJsonDataGeneratorSettings.cs
│ ├── References
│ │ ├── IJsonReferenceBase.cs
│ │ └── IJsonReference.cs
│ └── ITypeNameGenerator.cs
├── NJsonSchema.CodeGeneration.CSharp.Tests
│ ├── References
│ │ ├── G.json
│ │ ├── D.json
│ │ ├── C.json
│ │ ├── F.json
│ │ ├── Vehicle.json
│ │ ├── Car.json
│ │ ├── A.json
│ │ ├── B.json
│ │ └── Animal.json
│ ├── Snapshots
│ │ ├── GeneralGeneratorTests.When_type_name_is_missing_then_anonymous_name_is_generated.verified.txt
│ │ ├── GeneralGeneratorTests.When_nullable_property_is_required_then_it_is_not_nullable_in_generated_csharp_code.verified.txt
│ │ ├── UriTests.When_property_is_uri_then_csharp_output_is_also_uri.verified.txt
│ │ ├── GeneralGeneratorTests.When_property_is_byte_then_its_type_is_preserved.verified.txt
│ │ ├── GeneralGeneratorTests.When_property_is_object_then_object_property_is_generated.verified.txt
│ │ ├── GeneralGeneratorTests.Can_generate_type_from_string_property_with_base64_format.verified.txt
│ │ ├── GeneralGeneratorTests.Can_generate_type_from_string_property_with_byte_format.verified.txt
│ │ ├── ObjectPropertyRequiredTests.When_property_is_required_then_required_attribute_is_rendered.verified.txt
│ │ ├── AbstractSchemaTests.When_class_is_abstract_then_is_abstract_CSharp_keyword_is_generated.verified.txt
│ │ ├── ObjectPropertyRequiredTests.When_property_is_not_required_then_required_attribute_is_not_rendered.verified.txt
│ │ ├── StringPropertyRequiredTests.When_property_is_not_required_then_required_attribute_is_not_rendered.verified.txt
│ │ ├── ObjectPropertyRequiredTests.When_property_is_required_then_required_attribute_is_rendered_in_Swagger_mode.verified.txt
│ │ ├── ArrayTests.When_array_item_is_nullable_then_generated_CSharp_is_correct.verified.txt
│ │ ├── ObsoleteTests.When_class_is_obsolete_then_obsolete_attribute_is_rendered.verified.txt
│ │ ├── ArrayTests.When_array_property_is_required_then_array_instance_can_be_changed.verified.txt
│ │ ├── GeneralGeneratorTests.When_property_is_ObservableCollection_then_generated_code_uses_the_same_class.verified.txt
│ │ ├── ObjectPropertyRequiredTests.When_property_is_not_required_then_required_attribute_is_not_rendered_in_Swagger_mode.verified.txt
│ │ ├── StringPropertyRequiredTests.When_property_is_not_required_then_required_attribute_is_not_rendered_in_Swagger_mode.verified.txt
│ │ ├── ObsoleteTests.When_property_is_obsolete_then_obsolete_attribute_is_rendered.verified.txt
│ │ ├── GeneralGeneratorTests.When_enum_has_special_chars_then_they_should_be_converted.verified.txt
│ │ ├── GeneralGeneratorTests.When_enum_has_special_char_questionmark_then_it_should_be_converted.verified.txt
│ │ ├── ValueGeneratorTests.When_schema_contains_range_then_code_is_correctly_generated.verified.txt
│ │ ├── ObsoleteTests.When_class_is_obsolete_with_a_message_then_obsolete_attribute_with_a_message_is_rendered.verified.txt
│ │ ├── ObsoleteTests.When_property_is_obsolete_with_a_message_then_obsolete_attribute_with_a_message_is_rendered.verified.txt
│ │ ├── GeneralGeneratorTests.When_tuple_types_has_ints_then_it_is_generated_correctly_inlineNamedTuples=True.verified.txt
│ │ ├── NullableTests.When_property_is_optional_and_GenerateNullableOptionalProperties_is_set_then_CSharp_property_is_nullable.verified.txt
│ │ ├── NullableTests.When_property_is_optional_and_GenerateNullableOptionalProperties_is_not_set_then_CSharp_property_is_not_nullable.verified.txt
│ │ ├── InterfaceTests.When_interface_has_properties_then_properties_are_included_in_schema.verified.txt
│ │ ├── InterfaceTests.When_class_implements_interface_then_properties_are_included_in_schema.verified.txt
│ │ ├── StringPropertyRequiredTests.When_property_is_required_then_required_attribute_is_rendered.verified.txt
│ │ ├── StringPropertyRequiredTests.When_property_is_required_then_required_attribute_is_rendered_in_Swagger_mode.verified.txt
│ │ ├── InheritanceTests.When_empty_class_inherits_from_dictionary_then_allOf_inheritance_still_works.verified.txt
│ │ ├── NullableReferenceTypesTests.When_property_is_optional_and_GenerateNullableReferenceTypes_is_not_set_then_CSharp_property_is_not_nullable.verified.txt
│ │ ├── GeneralGeneratorTests.When_documentation_present_produces_valid_xml_documentation_syntax.verified.txt
│ │ ├── NullableReferenceTypesTests.When_property_is_optional_and_GenerateNullableOptionalProperties_is_set_then_CSharp_property_is_nullable.verified.txt
│ │ ├── GeneralGeneratorTests.When_enum_type_name_is_missing_then_default_value_is_still_correctly_set.verified.txt
│ │ ├── GeneralGeneratorTests.When_property_is_required_then_CSharp_code_is_correct.verified.txt
│ │ ├── EnumTests.When_class_has_enum_array_property_then_enum_name_is_preserved.verified.txt
│ │ ├── GeneralGeneratorTests.When_record_no_setter_in_class_and_constructor_provided.verified.txt
│ │ ├── GeneralGeneratorTests.When_csharp_record_init_in_record_and_constructor_provided.verified.txt
│ │ ├── GeneralGeneratorTests.When_csharp_record_no_setter_in_record_and_constructor_provided.verified.txt
│ │ ├── GeneralGeneratorTests.When_native_record_no_setter_in_class_and_constructor_provided.verified.txt
│ │ ├── EnumTests.When_enum_is_string_then_generic_StringEnumConverter_is_used_nullable=False.verified.txt
│ │ ├── EnumTests.When_enum_is_string_then_generic_StringEnumConverter_is_used_nullable=True.verified.txt
│ │ ├── GeneralGeneratorTests.When_definition_contains_datetime_and_use_system_text_json_then_converter_should_not_be_added.verified.txt
│ │ ├── GeneralGeneratorTests.When_class_is_abstract_constructor_is_protected_for_record.verified.txt
│ │ ├── NumberTests.When_number_has_double_format_then_double_is_generated.verified.txt
│ │ ├── NumberTests.When_number_has_float_format_then_float_is_generated.verified.txt
│ │ ├── NumberTests.When_number_has_no_format_then_default_is_generated.verified.txt
│ │ ├── NumberTests.When_number_type_setting_is_null_then_double_is_generated.verified.txt
│ │ ├── GeneralGeneratorTests.When_definition_is_named_Object_then_JObject_is_generated.verified.txt
│ │ ├── GeneralGeneratorTests.When_property_has_same_name_as_class_then_it_is_renamed.verified.txt
│ │ ├── NumberTests.When_number_has_decimal_format_then_decimal_is_generated.verified.txt
│ │ ├── GeneralGeneratorTests.When_object_has_generic_name_then_it_is_transformed.verified.txt
│ │ ├── NumberTests.When_number_type_setting_is_whitespace_then_double_is_generated.verified.txt
│ │ ├── DictionaryTests.When_dictionary_property_is_required_then_dictionary_instance_can_be_changed.verified.txt
│ │ ├── GeneralGeneratorTests.When_name_contains_dash_then_it_is_converted_to_upper_case.verified.txt
│ │ ├── GeneralGeneratorTests.When_definition_contains_datetime_converter_should_not_be_added.verified.txt
│ │ ├── GeneralGeneratorTests.When_property_name_is_created_by_custom_fun_then_parameter_name_is_correct_for_record.verified.txt
│ │ ├── NumberTests.When_number_type_setting_is_defined_then_setting_type_is_generated.verified.txt
│ │ ├── NumberTests.When_number_decimal_type_setting_is_defined_then_setting_type_is_generated.verified.txt
│ │ ├── NumberTests.When_number_double_type_setting_is_defined_then_setting_type_is_generated.verified.txt
│ │ ├── NumberTests.When_number_float_type_setting_is_defined_then_setting_type_is_generated.verified.txt
│ │ ├── DefaultPropertyTests.When_property_has_boolean_default_it_is_reflected_in_the_poco.verified.txt
│ │ ├── DefaultPropertyTests.When_property_has_interger_default_it_is_reflected_in_the_poco.verified.txt
│ │ ├── GeneralGeneratorTests.When_property_has_not_supported_characters_then_they_are_removed.verified.txt
│ │ ├── GeneralGeneratorTests.When_schema_has_AdditionProperties_schema_then_JsonExtensionDataAttribute_is_generated.verified.txt
│ │ ├── GeneralGeneratorTests.When_definition_contains_both_min_length_a_string_length_attribute_is_added_only_for_type_string.verified.txt
│ │ ├── GeneralGeneratorTests.When_definition_contains_pattern_but_type_is_not_string_a_regular_expression_should_not_be_added.verified.txt
│ │ ├── ValidationAttributesTests.When_int32_property_has_minimum_then_range_attribute_is_rendered_in_Swagger_mode.verified.txt
│ │ ├── ValidationAttributesTests.When_int_property_has_maximum_then_range_attribute_is_rendered_in_Swagger_mode.verified.txt
│ │ ├── GeneralGeneratorTests.When_definition_contains_maximum_a_range_attribute_is_not_added_for_anything_but_type_number_or_integer.verified.txt
│ │ ├── ValidationAttributesTests.When_int64_property_has_minimum_then_range_attribute_is_rendered_in_Swagger_mode.verified.txt
│ │ ├── GeneralGeneratorTests.When_name_contains_unallowed_characters_then_they_are_converted_to_valid_csharp_jsonPropertyName=FooBar3.verified.txt
│ │ ├── GeneralGeneratorTests.When_name_contains_unallowed_characters_then_they_are_converted_to_valid_csharp_jsonPropertyName=FooBar4.verified.txt
│ │ ├── GeneralGeneratorTests.When_name_contains_unallowed_characters_then_they_are_converted_to_valid_csharp_jsonPropertyName=Foobar1.verified.txt
│ │ ├── GeneralGeneratorTests.When_name_contains_unallowed_characters_then_they_are_converted_to_valid_csharp_jsonPropertyName=Foobar2.verified.txt
│ │ ├── GeneralGeneratorTests.When_name_contains_unallowed_characters_then_they_are_converted_to_valid_csharp_jsonPropertyName=Foo_bar.verified.txt
│ │ ├── GeneralGeneratorTests.When_name_contains_unallowed_characters_then_they_are_converted_to_valid_csharp_jsonPropertyName=Foobars.verified.txt
│ │ ├── DefaultPropertyTests.When_generating_CSharp_code_then_default_value_generates_expected_expression.verified.txt
│ │ ├── GeneralGeneratorTests.When_name_contains_unallowed_characters_then_they_are_converted_to_valid_csharp_jsonPropertyName=FooStarbar.verified.txt
│ │ ├── GeneralGeneratorTests.When_name_contains_unallowed_characters_then_they_are_converted_to_valid_csharp_jsonPropertyName=Fooplusbar.verified.txt
│ │ ├── DefaultPropertyTests.When_property_has_boolean_default_and_default_value_generation_is_disabled_then_default_value_is_not_generated.verified.txt
│ │ └── ValidationAttributesTests.When_integer_property_has_minimum_and_maximum_that_are_too_large_or_small_for_int64.verified.txt
│ ├── NJsonSchema.CodeGeneration.CSharp.Tests.xml
│ └── AbstractSchemaTests.cs
├── NJsonSchema.CodeGeneration.Tests
│ ├── Templates
│ │ ├── sample-1.liquid
│ │ ├── sample-2.liquid
│ │ ├── elseif.liquid
│ │ └── inline-liquid.liquid
│ ├── Templates2
│ │ ├── sample-2.liquid
│ │ └── sample-3.liquid
│ ├── Snapshots
│ │ ├── InheritanceSerializationTests.When_serializer_setting_is_changed_then_converter_uses_correct_settings.verified.txt
│ │ ├── DefaultGenerationTests.When_property_has_default_attribute_then_default_value_is_set_in_generated_Poco_CSharp_code.verified.txt
│ │ ├── EnumGenerationTests.When_enum_has_integer_value_then_CS_code_has_EnumMember_attribute.verified.txt
│ │ ├── EnumGenerationTests.When_enum_has_string_value_then_CS_code_has_JsonStringEnumMemberName_attribute.verified.txt
│ │ └── EnumGenerationTests.When_enum_has_string_value_then_CS_code_has_EnumMember_attribute.verified.txt
│ └── ApiSurfaceGuard.cs
├── NJsonSchema.Yaml
│ ├── NuGetIcon.png
│ └── NJsonSchema.Yaml.csproj
├── NJsonSchema.Annotations
│ ├── NuGetIcon.png
│ ├── NJsonSchema.Annotations.csproj
│ ├── JsonSchemaIgnoreAttribute.cs
│ ├── ItemsCanBeNullAttribute.cs
│ ├── JsonObjectType.cs
│ ├── CanBeNullAttribute.cs
│ ├── NotNullAttribute.cs
│ ├── JsonSchemaDateAttribute.cs
│ └── IJsonSchemaExtensionDataAttribute.cs
├── NJsonSchema.Benchmark
│ ├── Directory.Build.props
│ ├── Schema.json
│ ├── JsonSchemaGeneratorBenchmark.cs
│ ├── ConversionUtilitiesBenchmark.cs
│ ├── CsharpGeneratorBenchmark.cs
│ ├── TypeScriptGeneratorBenchmark.cs
│ ├── JsonSchemaBenchmark.cs
│ ├── CSharpTypeResolverBenchmark.cs
│ └── Program.cs
├── NJsonSchema.Yaml.Tests
│ └── References
│ │ └── YamlReferencesTest
│ │ ├── collection.yaml
│ │ ├── collection.json
│ │ ├── subdir_spec
│ │ ├── enums
│ │ │ └── TaskExecutionStatusEnum.yaml
│ │ └── models
│ │ │ ├── LifeCycleCollectionModel.yaml
│ │ │ └── ExecuteTaskResultModel.yaml
│ │ ├── yaml_schema_with_json_reference.yaml
│ │ ├── yaml_schema_with_yaml_reference.yaml
│ │ ├── json_schema_with_json_reference.json
│ │ ├── json_schema_with_yaml_reference.json
│ │ └── common-items
│ │ ├── headers.yaml
│ │ └── responses.yaml
├── NJsonSchema.CodeGeneration
│ ├── NuGetIcon.png
│ ├── ITemplate.cs
│ ├── CodeArtifactLanguage.cs
│ ├── CodeArtifactCategory.cs
│ ├── IPropertyNameGenerator.cs
│ ├── CodeArtifactType.cs
│ └── ITemplateFactory.cs
├── NJsonSchema.NewtonsoftJson
│ ├── NuGetIcon.png
│ └── NJsonSchema.NewtonsoftJson.csproj
├── NJsonSchema.CodeGeneration.TypeScript
│ ├── NuGetIcon.png
│ ├── Templates
│ │ ├── File.ParseDateOnly.liquid
│ │ ├── File.FormatDate.liquid
│ │ ├── Enum.liquid
│ │ ├── Enum.StringLiteral.liquid
│ │ ├── File.liquid
│ │ └── Interface.liquid
│ ├── TypeScriptEnumStyle.cs
│ ├── TypeScriptNullValue.cs
│ └── TypeScriptTypeStyle.cs
├── NJsonSchema.CodeGeneration.TypeScript.Tests
│ ├── package.json
│ ├── Models
│ │ ├── Gender.cs
│ │ ├── File.cs
│ │ ├── Teacher.cs
│ │ ├── Address.cs
│ │ └── Person.cs
│ ├── Snapshots
│ │ ├── TypeScriptGeneratorTests.When_type_name_is_missing_then_anonymous_name_is_generated.verified.txt
│ │ ├── TypeScriptObjectTests.When_property_is_object_then_jsonProperty_has_no_reference_and_is_any.verified.txt
│ │ ├── DateTimeCodeGenerationTests.When_date_handling_is_date_then_date_property_are_generated_in_interface.verified.txt
│ │ ├── TypeScriptObjectTests.When_dictionary_value_is_object_then_typescript_uses_any.verified.txt
│ │ ├── DateTimeCodeGenerationTests.When_date_handling_is_string_then_string_property_are_generated_in_interface.verified.txt
│ │ ├── DateTimeCodeGenerationTests.When_date_handling_is_moment_then_moment_property_are_generated_in_interface.verified.txt
│ │ ├── TypeScriptGeneratorTests.When_name_contains_dash_then_it_is_converted_to_upper_case.verified.txt
│ │ ├── DictionaryTests.When_class_inherits_from_any_dictionary_then_interface_has_indexer_property.verified.txt
│ │ ├── DictionaryTests.When_class_inherits_from_string_dictionary_then_interface_has_indexer_property.verified.txt
│ │ ├── DateCodeGenerationTests.When_date_handling_is_date_then_date_property_is_generated_in_interface.verified.txt
│ │ ├── DateCodeGenerationTests.When_date_handling_is_string_then_string_property_is_generated_in_interface.verified.txt
│ │ ├── DateCodeGenerationTests.When_date_handling_is_moment_then_moment_property_is_generated_in_interface.verified.txt
│ │ ├── TypeScriptDictionaryTests.When_dictionary_key_is_string_literal_then_typescript_has_string_literal_key_ts_2_1.verified.txt
│ │ ├── TypeScriptDictionaryTests.When_dictionary_value_is_enum_then_typescript_has_enum_value.verified.txt
│ │ ├── TypeScriptDictionaryTests.When_dictionary_key_is_enum_then_typescript_has_string_key.verified.txt
│ │ ├── TypeScriptDictionaryTests.When_dictionary_key_is_enum_then_typescript_has_enum_key_ts_2_1.verified.txt
│ │ ├── TypeScriptDiscriminatorTests.When_generating_interface_contract_add_discriminator_string_literal.verified.txt
│ │ ├── TypeScriptDiscriminatorTests.When_generating_interface_contract_add_discriminator.verified.txt
│ │ ├── TypeScriptDiscriminatorTests.When_parameter_is_abstract_then_generate_union_interface.verified.txt
│ │ ├── ClassGenerationTests.Verify_output_style=Interface.verified.txt
│ │ ├── TypeScriptGeneratorTests.When_property_is_readonly_then_ts_property_is_also_readonly.verified.txt
│ │ ├── TypeScriptGeneratorTests.When_allOf_contains_one_schema_then_csharp_inheritance_is_generated.verified.txt
│ │ ├── TypeScriptGeneratorTests.When_property_is_required_name_then_TypeScript_property_is_not_optional.verified.txt
│ │ ├── TypeScriptGeneratorTests.When_property_name_does_not_match_property_name_then_casing_is_correct_in_output.verified.txt
│ │ └── InheritanceTests.When_interfaces_are_generated_with_inheritance_then_type_check_methods_are_generated.verified.txt
│ ├── tsconfig.json
│ ├── NJsonSchema.CodeGeneration.TypeScript.Tests.xml
│ └── PropertyNameTests.cs
├── .editorconfig
├── NJsonSchema.Demo
│ ├── Tests
│ │ ├── optional
│ │ │ └── zeroTerminatedFloats.json
│ │ ├── minItems.json
│ │ ├── maxItems.json
│ │ ├── minProperties.json
│ │ ├── maxProperties.json
│ │ ├── definitions.json
│ │ ├── pattern.json
│ │ ├── minLength.json
│ │ ├── maxLength.json
│ │ └── required.json
│ └── NJsonSchema.Demo.csproj
├── NJsonSchema.Demo.Performance
│ └── NJsonSchema.Demo.Performance.csproj
└── NJsonSchema.NewtonsoftJson.Tests
│ └── Generation
│ ├── RecordTests.cs
│ └── StructTests.cs
├── assets
├── Icon.png
├── NuGetIcon.png
└── GitHubIcon.png
├── .github
└── FUNDING.yml
├── .nuke
└── parameters.json
├── global.json
├── .gitignore
└── LICENSE.md
/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.Body.liquid:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.Annotations.liquid:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.Constructor.liquid:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp/Templates/Enum.Annotations.liquid:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp/Templates/Enum.Member.Annotations.liquid:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.Property.Annotations.liquid:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/assets/Icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RicoSuter/NJsonSchema/HEAD/assets/Icon.png
--------------------------------------------------------------------------------
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 |
3 | github: RicoSuter
4 |
--------------------------------------------------------------------------------
/assets/NuGetIcon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RicoSuter/NJsonSchema/HEAD/assets/NuGetIcon.png
--------------------------------------------------------------------------------
/src/NJsonSchema.snk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RicoSuter/NJsonSchema/HEAD/src/NJsonSchema.snk
--------------------------------------------------------------------------------
/assets/GitHubIcon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RicoSuter/NJsonSchema/HEAD/assets/GitHubIcon.png
--------------------------------------------------------------------------------
/.nuke/parameters.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "./build.schema.json",
3 | "Solution": "src/NJsonSchema.sln"
4 | }
--------------------------------------------------------------------------------
/global.json:
--------------------------------------------------------------------------------
1 | {
2 | "sdk": {
3 | "version": "10.0.100",
4 | "rollForward": "latestMinor"
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/References/LocalReferencesTests/dir_with_#/first.json:
--------------------------------------------------------------------------------
1 | {
2 | "$ref": "./second.json#/myInt"
3 | }
--------------------------------------------------------------------------------
/src/NJsonSchema/NuGetIcon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RicoSuter/NJsonSchema/HEAD/src/NJsonSchema/NuGetIcon.png
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/References/G.json:
--------------------------------------------------------------------------------
1 | {
2 | "type": "string",
3 | "nullable": true
4 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.Tests/Templates/sample-1.liquid:
--------------------------------------------------------------------------------
1 | {% if true %}
2 | sample-1-in-templates
3 | {% endif %}
4 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.Tests/Templates/sample-2.liquid:
--------------------------------------------------------------------------------
1 | {% if true %}
2 | sample-2-in-templates
3 | {% endif %}
4 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.Tests/Templates2/sample-2.liquid:
--------------------------------------------------------------------------------
1 | {% if true %}
2 | sample-2-in-templates-2
3 | {% endif %}
4 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.Tests/Templates2/sample-3.liquid:
--------------------------------------------------------------------------------
1 | {% if true %}
2 | sample-3-in-templates-2
3 | {% endif %}
4 |
--------------------------------------------------------------------------------
/src/NJsonSchema.Yaml/NuGetIcon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RicoSuter/NJsonSchema/HEAD/src/NJsonSchema.Yaml/NuGetIcon.png
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/References/D.json:
--------------------------------------------------------------------------------
1 | {
2 | "type": "object",
3 | "description": "Possible types"
4 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/References/LocalReferencesTests/dir_with_#/second.json:
--------------------------------------------------------------------------------
1 | {
2 | "myInt": {
3 | "type": "integer"
4 | }
5 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.Tests/Templates/elseif.liquid:
--------------------------------------------------------------------------------
1 | {% if false %}
2 | Wrong
3 | {% elseif true %}
4 | Hello
5 | {% endif %}
6 |
--------------------------------------------------------------------------------
/src/NJsonSchema.Annotations/NuGetIcon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RicoSuter/NJsonSchema/HEAD/src/NJsonSchema.Annotations/NuGetIcon.png
--------------------------------------------------------------------------------
/src/NJsonSchema.Benchmark/Directory.Build.props:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/src/NJsonSchema.Yaml.Tests/References/YamlReferencesTest/collection.yaml:
--------------------------------------------------------------------------------
1 | ---
2 | myInt:
3 | type: integer
4 | myBool:
5 | type: boolean
6 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration/NuGetIcon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RicoSuter/NJsonSchema/HEAD/src/NJsonSchema.CodeGeneration/NuGetIcon.png
--------------------------------------------------------------------------------
/src/NJsonSchema.NewtonsoftJson/NuGetIcon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RicoSuter/NJsonSchema/HEAD/src/NJsonSchema.NewtonsoftJson/NuGetIcon.png
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp/NuGetIcon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RicoSuter/NJsonSchema/HEAD/src/NJsonSchema.CodeGeneration.CSharp/NuGetIcon.png
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript/NuGetIcon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RicoSuter/NJsonSchema/HEAD/src/NJsonSchema.CodeGeneration.TypeScript/NuGetIcon.png
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/References/LocalReferencesTests/collection.json:
--------------------------------------------------------------------------------
1 | {
2 | "myInt": {
3 | "type": "integer"
4 | },
5 | "myBool": {
6 | "type": "boolean"
7 | }
8 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Yaml.Tests/References/YamlReferencesTest/collection.json:
--------------------------------------------------------------------------------
1 | {
2 | "myInt": {
3 | "type": "integer"
4 | },
5 | "myBool": {
6 | "type": "boolean"
7 | }
8 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.Tests/Templates/inline-liquid.liquid:
--------------------------------------------------------------------------------
1 | {%
2 | liquid
3 | echo
4 | 'welcome ' | upcase
5 | echo 'to the liquid tag'
6 | | upcase
7 | %}
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/References/C.json:
--------------------------------------------------------------------------------
1 | {
2 | "type":"string",
3 | "description":"Possible types",
4 | "enum":[
5 | "First",
6 | "Second"
7 | ]
8 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "devDependencies": {
3 | "moment": "^2.30.1",
4 | "dayjs": "^1.11.13",
5 | "typescript": "^5.8.3"
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/VerifyChecksTests.cs:
--------------------------------------------------------------------------------
1 | namespace NJsonSchema.Tests;
2 |
3 | public class VerifyChecksTests
4 | {
5 | [Fact]
6 | public Task Run() =>
7 | VerifyChecks.Run();
8 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/References/LocalReferencesTests/schema_with_reference.json:
--------------------------------------------------------------------------------
1 | {
2 | "type": "object",
3 | "properties": {
4 | "foo": {
5 | "$ref": "./animal.json"
6 | }
7 | }
8 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/Generation/Snapshots/JsonInheritanceConverterTests.When_serializing_discriminator_property_is_overwritten_if_already_present.verified.txt:
--------------------------------------------------------------------------------
1 | {"PropertyB":"defaultB","PropertyA":"ClassA"}
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/Generation/Snapshots/JsonInheritanceConverterTests.When_serializing_discriminator_property_is_set.verified.txt:
--------------------------------------------------------------------------------
1 | {"PropertyB":"defaultB","PropertyA":"defaultA","discriminator":"ClassA"}
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Models/Gender.cs:
--------------------------------------------------------------------------------
1 | namespace NJsonSchema.CodeGeneration.TypeScript.Tests.Models
2 | {
3 | public enum Gender
4 | {
5 | Male,
6 | Female
7 | }
8 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/Schema/Snapshots/JsonSchemaTests.When_setting_single_type_then_it_should_be_serialized_correctly.verified.txt:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json-schema.org/draft-04/schema#",
3 | "type": "integer"
4 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Models/File.cs:
--------------------------------------------------------------------------------
1 | namespace NJsonSchema.CodeGeneration.TypeScript.Tests.Models
2 | {
3 | public class File
4 | {
5 | public byte[] Content { get; set; }
6 | }
7 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Models/Teacher.cs:
--------------------------------------------------------------------------------
1 | namespace NJsonSchema.CodeGeneration.TypeScript.Tests.Models
2 | {
3 | public class Teacher : Person
4 | {
5 | public string Class { get; set; }
6 | }
7 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript/Templates/File.ParseDateOnly.liquid:
--------------------------------------------------------------------------------
1 | function parseDateOnly(s: string) {
2 | const date = new Date(s);
3 | return new Date(date.getTime() +
4 | date.getTimezoneOffset() * 60000);
5 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/References/F.json:
--------------------------------------------------------------------------------
1 | {
2 | "type": "object",
3 | "properties": {
4 | "name": {
5 | "$ref": "./G.json",
6 | "description": "This is the type of G"
7 | }
8 | }
9 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/TypeScriptGeneratorTests.When_type_name_is_missing_then_anonymous_name_is_generated.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/References/LocalReferencesTests/schema_with_indirect_subreference.json:
--------------------------------------------------------------------------------
1 | {
2 | "type": "object",
3 | "properties": {
4 | "foo": {
5 | "$ref": "./animal.json#/definitions/SubAnimal"
6 | }
7 | }
8 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "target": "es2016",
4 | "moduleResolution": "node",
5 | "typeRoots": ["./node_modules/@types"],
6 | "strict": true
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/src/NJsonSchema.Benchmark/Schema.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json-schema.org/draft-04/schema#",
3 | "properties": {
4 | "some_property": {
5 | "description": "Example property",
6 | "type": "string",
7 | "maxLength": 64
8 | }
9 | }
10 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Models/Address.cs:
--------------------------------------------------------------------------------
1 | namespace NJsonSchema.CodeGeneration.TypeScript.Tests.Models
2 | {
3 | public class Address
4 | {
5 | public string Street { get; set; }
6 |
7 | public string City { get; set; }
8 | }
9 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Yaml.Tests/References/YamlReferencesTest/subdir_spec/enums/TaskExecutionStatusEnum.yaml:
--------------------------------------------------------------------------------
1 | TaskExecutionStatusEnum:
2 | description: |-
3 | Status of a task execution
4 | type: string
5 | enum:
6 | - Completed
7 | - Failed
8 | example: Completed
9 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript/Templates/File.FormatDate.liquid:
--------------------------------------------------------------------------------
1 | function formatDate(d: Date) {
2 | return d.getFullYear() + '-' +
3 | (d.getMonth() < 9 ? ('0' + (d.getMonth()+1)) : (d.getMonth()+1)) + '-' +
4 | (d.getDate() < 10 ? ('0' + d.getDate()) : d.getDate());
5 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript/Templates/Enum.liquid:
--------------------------------------------------------------------------------
1 | {%- if HasDescription -%}
2 | /** {{ Description }} */
3 | {%- endif -%}
4 | {% if ExportTypes %}export {% endif %}enum {{ Name }} {
5 | {%- for enumeration in Enums -%}
6 | {{ enumeration.Name }} = {{ enumeration.Value }},
7 | {%- endfor -%}
8 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Yaml.Tests/References/YamlReferencesTest/yaml_schema_with_json_reference.yaml:
--------------------------------------------------------------------------------
1 | ---
2 | type: object
3 | properties:
4 | bar:
5 | "$ref": "./collection.json#/myBool"
6 | foo:
7 | "$ref": "#/definitions/collection/myInt"
8 | definitions:
9 | collection:
10 | "$ref": "./collection.json"
11 |
--------------------------------------------------------------------------------
/src/NJsonSchema.Yaml.Tests/References/YamlReferencesTest/yaml_schema_with_yaml_reference.yaml:
--------------------------------------------------------------------------------
1 | ---
2 | type: object
3 | properties:
4 | bar:
5 | "$ref": "./collection.yaml#/myBool"
6 | foo:
7 | "$ref": "#/definitions/collection/myInt"
8 | definitions:
9 | collection:
10 | "$ref": "./collection.yaml"
11 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript/Templates/Enum.StringLiteral.liquid:
--------------------------------------------------------------------------------
1 | {%- if HasDescription -%}
2 | /** {{ Description }} */
3 | {%- endif -%}
4 | {%- if ExportTypes %}export {% endif %}type {{ Name }} = {% for enumeration in Enums %}{%- if Enums.first.Value != enumeration.Value %} | {% endif %}{{ enumeration.Value }}{% endfor %};
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/Generation/Snapshots/EnumTests.When_schema_has_x_enum_names_then_backward_compatibility_works.verified.txt:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json-schema.org/draft-04/schema#",
3 | "type": "string",
4 | "x-enumNames": [
5 | "Name1",
6 | "Name2"
7 | ],
8 | "enum": [
9 | "value1",
10 | "value2"
11 | ]
12 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Annotations/NJsonSchema.Annotations.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0;net462
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/Generation/Snapshots/EnumTests.When_schema_has_x_enum_varnames_then_backward_compatibility_works.verified.txt:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json-schema.org/draft-04/schema#",
3 | "type": "string",
4 | "x-enumNames": [
5 | "VarName1",
6 | "VarName2"
7 | ],
8 | "enum": [
9 | "value1",
10 | "value2"
11 | ]
12 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/References/LocalReferencesTests/animal.json:
--------------------------------------------------------------------------------
1 | {
2 | "type": "object",
3 | "properties": {
4 | "bar": {
5 | "name": {
6 | "type": "string"
7 | }
8 | }
9 | },
10 | "definitions": {
11 | "SubAnimal": {
12 | "type": "object"
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/References/LocalReferencesTests/schema_with_indirect_reference.json:
--------------------------------------------------------------------------------
1 | {
2 | "type": "object",
3 | "properties": {
4 | "foo": {
5 | "$ref": "#/definitions/FooAnimal"
6 | }
7 | },
8 | "definitions": {
9 | "FooAnimal": {
10 | "$ref": "./animal.json"
11 | }
12 | }
13 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Yaml.Tests/References/YamlReferencesTest/subdir_spec/models/LifeCycleCollectionModel.yaml:
--------------------------------------------------------------------------------
1 | LifeCycleCollectionModel:
2 | description: |-
3 | A list of life cycles.
4 | properties:
5 | items:
6 | description: List of life cycles.
7 | type: array
8 | items:
9 | $ref: 'LifeCycleModel.yaml#/LifeCycleModel'
10 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/References/Vehicle.json:
--------------------------------------------------------------------------------
1 | {
2 | "title": "Vehicle",
3 | "type": "object",
4 | "x-abstract": true,
5 | "description": "This is a vehicle",
6 | "properties": {
7 | "id": {
8 | "type": "string",
9 | "description": "This is the ID of Vehicle"
10 | }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_type_name_is_missing_then_anonymous_name_is_generated.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 |
12 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/TypeScriptObjectTests.When_property_is_object_then_jsonProperty_has_no_reference_and_is_any.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | export interface MyClass {
13 | Test: any | undefined;
14 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/DateTimeCodeGenerationTests.When_date_handling_is_date_then_date_property_are_generated_in_interface.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | export interface MyClass {
13 | MyDateTime: Date;
14 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/TypeScriptObjectTests.When_dictionary_value_is_object_then_typescript_uses_any.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | export interface MyClass {
13 | Test: { [key: string]: any; } | undefined;
14 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/DateTimeCodeGenerationTests.When_date_handling_is_string_then_string_property_are_generated_in_interface.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | export interface MyClass {
13 | MyDateTime: string;
14 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/DateTimeCodeGenerationTests.When_date_handling_is_moment_then_moment_property_are_generated_in_interface.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | export interface MyClass {
13 | MyDateTime: moment.Moment;
14 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/TypeScriptGeneratorTests.When_name_contains_dash_then_it_is_converted_to_upper_case.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | export interface MyClass {
13 | "foo-bar": string;
14 |
15 | [key: string]: any;
16 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/Generation/Snapshots/EnumTests.When_schema_has_x_enum_descriptions_then_backward_compatibility_works.verified.txt:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json-schema.org/draft-04/schema#",
3 | "type": "string",
4 | "x-enum-descriptions": [
5 | "Desc1",
6 | "Desc2",
7 | null
8 | ],
9 | "enum": [
10 | "value1",
11 | "value2",
12 | "value3"
13 | ]
14 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/DictionaryTests.When_class_inherits_from_any_dictionary_then_interface_has_indexer_property.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | export interface MetadataDictionary {
13 | Foo: string;
14 |
15 | [key: string]: any;
16 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/References/LocalReferencesTests/schema_with_collection_reference.json:
--------------------------------------------------------------------------------
1 | {
2 | "type": "object",
3 | "properties": {
4 | "bar": {
5 | "$ref": "./collection.json#/myBool"
6 | },
7 | "foo": {
8 | "$ref": "#/definitions/collection/myInt"
9 | }
10 | },
11 | "definitions": {
12 | "collection": {
13 | "$ref": "./collection.json"
14 | }
15 | }
16 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Yaml.Tests/References/YamlReferencesTest/json_schema_with_json_reference.json:
--------------------------------------------------------------------------------
1 | {
2 | "type": "object",
3 | "properties": {
4 | "bar": {
5 | "$ref": "./collection.json#/myBool"
6 | },
7 | "foo": {
8 | "$ref": "#/definitions/collection/myInt"
9 | }
10 | },
11 | "definitions": {
12 | "collection": {
13 | "$ref": "./collection.json"
14 | }
15 | }
16 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Yaml.Tests/References/YamlReferencesTest/json_schema_with_yaml_reference.json:
--------------------------------------------------------------------------------
1 | {
2 | "type": "object",
3 | "properties": {
4 | "bar": {
5 | "$ref": "./collection.yaml#/myBool"
6 | },
7 | "foo": {
8 | "$ref": "#/definitions/collection/myInt"
9 | }
10 | },
11 | "definitions": {
12 | "collection": {
13 | "$ref": "./collection.yaml"
14 | }
15 | }
16 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.Inheritance.liquid:
--------------------------------------------------------------------------------
1 | {%- if RenderInpc %}
2 | {% if HasInheritance %} : {{ BaseClassName }}{% else %} : System.ComponentModel.INotifyPropertyChanged{% endif %}
3 | {%- elsif RenderPrism %}
4 | {% if HasInheritance %} : {{ BaseClassName }}{% else %} : Prism.Mvvm.BindableBase{% endif %}
5 | {%- else %}
6 | {% if HasInheritance %} : {{ BaseClassName }}{% endif %}
7 | {%- endif %}
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/DictionaryTests.When_class_inherits_from_string_dictionary_then_interface_has_indexer_property.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | export interface MetadataDictionary {
13 | Foo: string;
14 |
15 | [key: string]: string | any;
16 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/DateCodeGenerationTests.When_date_handling_is_date_then_date_property_is_generated_in_interface.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | export interface MyClass {
13 | myDate: Date;
14 | myTimeSpan: string;
15 |
16 | [key: string]: any;
17 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/Generation/SystemTextJson/Snapshots/SystemTextJsonTests.When_type_is_excluded_then_it_should_not_be_in_the_schema.verified.txt:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json-schema.org/draft-04/schema#",
3 | "title": "ContainerType1",
4 | "type": "object",
5 | "additionalProperties": false,
6 | "properties": {
7 | "Property": {
8 | "type": "integer",
9 | "format": "int32"
10 | }
11 | }
12 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/DateCodeGenerationTests.When_date_handling_is_string_then_string_property_is_generated_in_interface.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | export interface MyClass {
13 | myDate: string;
14 | myTimeSpan: string;
15 |
16 | [key: string]: any;
17 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/Generation/Snapshots/XmlDocsTests.When_example_xml_docs_is_defined_then_examples_can_be_defined.verified.txt:
--------------------------------------------------------------------------------
1 | {"$schema":"http://json-schema.org/draft-04/schema#","title":"AbstractClass","type":"object","description":"Foobar.","x-abstract":true,"x-example":{"foo":"bar"},"additionalProperties":false,"properties":{"Foo":{"type":["null","string"],"x-example":{"abc":"def"}},"Bar":{"type":["null","string"],"x-example":"Bar."}}}
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/References/Car.json:
--------------------------------------------------------------------------------
1 | {
2 | "title": "Car",
3 | "type": "object",
4 | "description": "This is a car",
5 | "allOf": [
6 | {
7 | "$ref": "Vehicle.json"
8 | }
9 | ],
10 | "properties": {
11 | "wheels": {
12 | "type": "integer",
13 | "description": "The number of wheels on the car"
14 | }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/DateCodeGenerationTests.When_date_handling_is_moment_then_moment_property_is_generated_in_interface.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | export interface MyClass {
13 | myDate: moment.Moment;
14 | myTimeSpan: moment.Duration;
15 |
16 | [key: string]: any;
17 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/Serialization/Snapshots/DiscriminatorSerializationTests.When_discriminator_object_is_set_then_schema_is_correctly_serialized.verified.txt:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json-schema.org/draft-04/schema#",
3 | "discriminator": {
4 | "propertyName": "discr",
5 | "mapping": {
6 | "Bar": "#/definitions/Foo"
7 | }
8 | },
9 | "definitions": {
10 | "Foo": {
11 | "type": "object"
12 | }
13 | }
14 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/Generation/SystemTextJson/Snapshots/SystemTextJsonTests.When_type_is_excluded_with_json_schema_ignore_attribute_then_it_should_not_be_in_the_schema.verified.txt:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json-schema.org/draft-04/schema#",
3 | "title": "ContainerType2",
4 | "type": "object",
5 | "additionalProperties": false,
6 | "properties": {
7 | "Property": {
8 | "type": "integer",
9 | "format": "int32"
10 | }
11 | }
12 | }
--------------------------------------------------------------------------------
/src/.editorconfig:
--------------------------------------------------------------------------------
1 | ; What is EditorConfig? http://editorconfig.org/
2 |
3 | root = true
4 |
5 | ; general settings
6 | [*]
7 | indent_style = space
8 | indent_size = 4
9 | end_of_line = crlf
10 | insert_final_newline = false
11 |
12 | # Verify
13 | [*.{received,verified}.{txt}]
14 | charset = utf-8-bom
15 | end_of_line = lf
16 | indent_size = unset
17 | indent_style = unset
18 | insert_final_newline = false
19 | tab_width = unset
20 | trim_trailing_whitespace = false
21 |
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/Generation/Snapshots/EnumTests.When_enum_has_no_description_attributes_then_descriptions_are_not_included_in_schema.verified.txt:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json-schema.org/draft-04/schema#",
3 | "title": "EnumWithFlags",
4 | "type": "string",
5 | "description": "",
6 | "x-enumFlags": true,
7 | "x-enumNames": [
8 | "Foo",
9 | "Bar",
10 | "Baz"
11 | ],
12 | "enum": [
13 | "Foo",
14 | "Bar",
15 | "Baz"
16 | ]
17 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Benchmark/JsonSchemaGeneratorBenchmark.cs:
--------------------------------------------------------------------------------
1 | using BenchmarkDotNet.Attributes;
2 | using NJsonSchema.NewtonsoftJson.Generation;
3 |
4 | namespace NJsonSchema.Benchmark
5 | {
6 | [MemoryDiagnoser]
7 | public class JsonSchemaGeneratorBenchmark
8 | {
9 | [Benchmark]
10 | public void GenerateFile()
11 | {
12 | NewtonsoftJsonSchemaGenerator.FromType();
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp/Templates/File.liquid:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | // Generated using the NJsonSchema v{{ ToolchainVersion }} (http://NJsonSchema.org)
4 | //
5 | //----------------------
6 |
7 | {%- if GenerateNullableReferenceTypes %}
8 | #nullable enable
9 |
10 | {%- endif %}
11 | namespace {{ Namespace }}
12 | {
13 | #pragma warning disable // Disable all warnings
14 |
15 | {{ TypesCode | tab }}
16 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Demo/Tests/optional/zeroTerminatedFloats.json:
--------------------------------------------------------------------------------
1 | [
2 | {
3 | "description": "some languages do not distinguish between different types of numeric value",
4 | "schema": {
5 | "type": "integer"
6 | },
7 | "tests": [
8 | {
9 | "description": "a float is not an integer even without fractional part",
10 | "data": 1.0,
11 | "valid": false
12 | }
13 | ]
14 | }
15 | ]
16 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp/CSharpJsonPolymorphicSerializationStyle.cs:
--------------------------------------------------------------------------------
1 | namespace NJsonSchema.CodeGeneration.CSharp
2 | {
3 | /// The CSharp JSON polymorphic serialization style.
4 | public enum CSharpJsonPolymorphicSerializationStyle
5 | {
6 | /// Use NJsonSchema polymorphic serialization
7 | NJsonSchema,
8 |
9 | /// Use System.Text.Json polymorphic serialization
10 | SystemTextJson
11 | }
12 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/References/A.json:
--------------------------------------------------------------------------------
1 | {
2 | "type": "object",
3 | "required": [
4 | "signalPaths"
5 | ],
6 | "description": "This is the class A",
7 | "properties": {
8 | "type": {
9 | "$ref": "./C.json",
10 | "description": "This is the type of A"
11 | },
12 | "signalPaths": {
13 | "children": "array",
14 | "items": {
15 | "$ref": "./B.json",
16 | "description": "This is a list of children"
17 | }
18 | }
19 | }
20 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/References/B.json:
--------------------------------------------------------------------------------
1 | {
2 | "type": "object",
3 | "description": "This is the class B",
4 | "properties": {
5 | "id": {
6 | "type": "string",
7 | "description": "This is the ID of B"
8 | },
9 | "type": {
10 | "$ref": "./C.json",
11 | "description": "This is the type of B"
12 | },
13 | "empty": {
14 | "$ref": "./D.json",
15 | "description": "This one should be of type D and not object"
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.Tests/Snapshots/InheritanceSerializationTests.When_serializer_setting_is_changed_then_converter_uses_correct_settings.verified.txt:
--------------------------------------------------------------------------------
1 | {
2 | "animal": {
3 | "bar": "bar",
4 | "subElements": [
5 | {
6 | "prop1": "x",
7 | "discriminator": "SubClass1"
8 | },
9 | {
10 | "prop3": "y",
11 | "prop2": "x",
12 | "discriminator": "SubClass3"
13 | }
14 | ],
15 | "foo": "foo",
16 | "discriminator": "Dog"
17 | }
18 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/Deserialization/DeserializationTests.cs:
--------------------------------------------------------------------------------
1 | using NJsonSchema.CodeGeneration.Tests;
2 |
3 | namespace NJsonSchema.Tests.Deserialization;
4 |
5 | public class DeserializationTests
6 | {
7 | [Fact]
8 | public async Task CanRoundTripPayPalOpenApi()
9 | {
10 | var schema = await JsonSchema.FromJsonAsync(File.OpenRead(Path.Combine("Deserialization", "TestData", "paypal_billing_subscriptions_v1.json")));
11 |
12 | await VerifyHelper.Verify(schema.ToJson());
13 | }
14 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/TypeScriptDictionaryTests.When_dictionary_key_is_string_literal_then_typescript_has_string_literal_key_ts_2_1.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | export type PropertyName = 0 | 1;
13 |
14 | export interface MyClass {
15 | Mapping: { [key in PropertyName]?: string; } | undefined;
16 | Mapping2: { [key in PropertyName]?: string; } | undefined;
17 | }
--------------------------------------------------------------------------------
/src/NJsonSchema/SampleJsonSchemaGeneratorSettings.cs:
--------------------------------------------------------------------------------
1 | namespace NJsonSchema
2 | {
3 | ///
4 | /// Settings for generating sample json schema
5 | ///
6 | public class SampleJsonSchemaGeneratorSettings
7 | {
8 | ///
9 | /// Gets or sets a value indicating whether to generate optional properties (default: ).
10 | ///
11 | public SchemaType SchemaType { get; set; } = SchemaType.JsonSchema;
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/TypeScriptDictionaryTests.When_dictionary_value_is_enum_then_typescript_has_enum_value.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | export enum Gender {
13 | Male = "Male",
14 | Female = "Female",
15 | }
16 |
17 | export interface MyClass {
18 | Mapping: { [key: string]: Gender; } | undefined;
19 | Mapping2: { [key: string]: Gender; } | undefined;
20 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Yaml.Tests/References/YamlReferencesTest/subdir_spec/models/ExecuteTaskResultModel.yaml:
--------------------------------------------------------------------------------
1 | ExecuteTaskResultModel:
2 | description: |-
3 | Contains the status of an ad hoc or system task executon.
4 | type: object
5 | properties:
6 | status:
7 | $ref: '../enums/TaskExecutionStatusEnum.yaml#/TaskExecutionStatusEnum'
8 | discriminator:
9 | propertyName: status
10 | mapping:
11 | Completed: '../yaml_spec_with_yaml_schema_with_relative_subdir_refs.yaml#/components/schemas/ExecuteTaskCompletedModel'
12 |
--------------------------------------------------------------------------------
/src/NJsonSchema.Yaml.Tests/References/YamlReferencesTest/common-items/headers.yaml:
--------------------------------------------------------------------------------
1 | components:
2 | headers:
3 | Content-Language:
4 | description: |-
5 | The Content-Language header field describes the natural language(s)
6 | of the intended audience for the representation. Note that this
7 | might not be equivalent to all the languages used within the
8 | representation.
9 |
10 | Reference: https://tools.ietf.org/html/rfc7231#section-3.1.3.2
11 | schema:
12 | type: string
13 | example: 'en'
--------------------------------------------------------------------------------
/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0;net462;net8.0
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | **/bin/**
2 | **/obj/**
3 | src/packages/Newtonsoft.Json**
4 |
5 | **.suo
6 | **.user
7 | **.DotSettings
8 |
9 | **.sln.ide/**
10 | **.vs/**
11 | .vscode/
12 | .idea/
13 |
14 | /src/NJsonSchema.sln.GhostDoc.xml
15 |
16 | [Bb]in/
17 | [Oo]bj/
18 | /src/packages
19 | .vs
20 | /src/NJsonSchema.CodeGeneration.Tests2
21 | *.vspx
22 | /src/TestResults
23 | /src/.cr/*
24 | /src/NJsonSchema.Benchmark/BenchmarkDotNet.Artifacts*
25 | _ReSharper.Caches
26 | /.nuke/temp
27 | /artifacts
28 |
29 | node_modules
30 |
31 | # Verify
32 | *.received.*
33 | *.received/
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/TypeScriptDictionaryTests.When_dictionary_key_is_enum_then_typescript_has_string_key.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | export enum PropertyName {
13 | Name = 0,
14 | Gender = 1,
15 | }
16 |
17 | export interface MyClass {
18 | Mapping: { [key in keyof typeof PropertyName]?: string; } | undefined;
19 | Mapping2: { [key in keyof typeof PropertyName]?: string; } | undefined;
20 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/Generation/SystemTextJson/Snapshots/SystemTextJsonTests.When_property_is_readonly_then_its_in_the_schema.verified.txt:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json-schema.org/draft-04/schema#",
3 | "title": "HealthCheckResult",
4 | "type": "object",
5 | "additionalProperties": false,
6 | "required": [
7 | "Name"
8 | ],
9 | "properties": {
10 | "Name": {
11 | "type": "string",
12 | "minLength": 1
13 | },
14 | "Description": {
15 | "type": [
16 | "null",
17 | "string"
18 | ]
19 | }
20 | }
21 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/TypeScriptDictionaryTests.When_dictionary_key_is_enum_then_typescript_has_enum_key_ts_2_1.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | export enum PropertyName {
13 | Name = 0,
14 | Gender = 1,
15 | }
16 |
17 | export interface MyClass {
18 | Mapping: { [key in keyof typeof PropertyName]?: string; } | undefined;
19 | Mapping2: { [key in keyof typeof PropertyName]?: string; } | undefined;
20 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_nullable_property_is_required_then_it_is_not_nullable_in_generated_csharp_code.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyRequiredNullableTest
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Foo", Required = Newtonsoft.Json.Required.Always)]
15 | public int Foo { get; set; }
16 |
17 | }
18 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.Inpc.liquid:
--------------------------------------------------------------------------------
1 | {% if HasInheritance == false -%}
2 | public event System.ComponentModel.PropertyChangedEventHandler{% if GenerateNullableReferenceTypes %}?{% endif %} PropertyChanged;
3 |
4 | protected virtual void RaisePropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string{% if GenerateNullableReferenceTypes %}?{% endif %} propertyName = null)
5 | {
6 | var handler = PropertyChanged;
7 | if (handler != null)
8 | handler(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
9 | }
10 | {% endif -%}
11 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/UriTests.When_property_is_uri_then_csharp_output_is_also_uri.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("MyUri", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public System.Uri MyUri { get; set; }
16 |
17 | }
18 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Demo.Performance/NJsonSchema.Demo.Performance.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net8.0
6 | false
7 | $(NoWarn);1591;CS8618
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/Generation/Snapshots/EnumTests.When_enum_has_description_attributes_then_descriptions_are_included_in_schema.verified.txt:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json-schema.org/draft-04/schema#",
3 | "title": "EnumWithDescriptions",
4 | "type": "string",
5 | "description": "",
6 | "x-enumNames": [
7 | "FirstValue",
8 | "SecondValue",
9 | "ThirdValue"
10 | ],
11 | "x-enum-descriptions": [
12 | "First value description",
13 | "Second value description",
14 | null
15 | ],
16 | "enum": [
17 | "FirstValue",
18 | "SecondValue",
19 | "ThirdValue"
20 | ]
21 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_property_is_byte_then_its_type_is_preserved.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyByteTest
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Cell", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public byte? Cell { get; set; }
16 |
17 | }
18 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_property_is_object_then_object_property_is_generated.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Foo", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public object Foo { get; set; }
16 |
17 | }
18 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.Can_generate_type_from_string_property_with_base64_format.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Content", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public byte[] Content { get; set; }
16 |
17 | }
18 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.Can_generate_type_from_string_property_with_byte_format.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Content", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public byte[] Content { get; set; }
16 |
17 | }
18 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/ObjectPropertyRequiredTests.When_property_is_required_then_required_attribute_is_rendered.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Property", Required = Newtonsoft.Json.Required.Always)]
15 | [System.ComponentModel.DataAnnotations.Required]
16 | public object Property { get; set; }
17 |
18 | }
19 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/AbstractSchemaTests.When_class_is_abstract_then_is_abstract_CSharp_keyword_is_generated.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public abstract partial class AbstractClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Foo", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public string Foo { get; set; }
16 |
17 | }
18 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/ObjectPropertyRequiredTests.When_property_is_not_required_then_required_attribute_is_not_rendered.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Property", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public object Property { get; set; }
16 |
17 | }
18 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/StringPropertyRequiredTests.When_property_is_not_required_then_required_attribute_is_not_rendered.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Property", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public string Property { get; set; }
16 |
17 | }
18 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.ToJson.liquid:
--------------------------------------------------------------------------------
1 | public string ToJson()
2 | {
3 | {%- if UseSystemTextJson %}
4 | var options = {{ JsonSerializerParameterCode }};
5 | {%- if JsonConvertersArrayCode contains "System.Text.Json.Serialization.JsonConverter[]" %}
6 | var converters = {{ JsonConvertersArrayCode }};
7 | foreach(var converter in converters)
8 | options.Converters.Add(converter);
9 | {%- endif %}
10 | return System.Text.Json.JsonSerializer.Serialize(this, options);
11 | {%- else %}
12 | return Newtonsoft.Json.JsonConvert.SerializeObject(this, {{ JsonSerializerParameterCode }});
13 | {%- endif %}
14 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/ObjectPropertyRequiredTests.When_property_is_required_then_required_attribute_is_rendered_in_Swagger_mode.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Property", Required = Newtonsoft.Json.Required.Always)]
15 | [System.ComponentModel.DataAnnotations.Required]
16 | public object Property { get; set; }
17 |
18 | }
19 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp/Templates/JsonInheritanceAttribute.liquid:
--------------------------------------------------------------------------------
1 | {%- if UseSystemTextJsonPolymorphicSerialization == false -%}
2 | [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "{{ ToolchainVersion }}")]
3 | [System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface, AllowMultiple = true)]
4 | internal class JsonInheritanceAttribute : System.Attribute
5 | {
6 | public JsonInheritanceAttribute(string key, System.Type type)
7 | {
8 | Key = key;
9 | Type = type;
10 | }
11 |
12 | public string Key { get; }
13 |
14 | public System.Type Type { get; }
15 | }
16 | {%- endif -%}
--------------------------------------------------------------------------------
/src/NJsonSchema.Yaml.Tests/References/YamlReferencesTest/common-items/responses.yaml:
--------------------------------------------------------------------------------
1 | components:
2 | responses:
3 | 401-GENERIC-RESPONSE:
4 | description: |-
5 | Response when the user does not supply valid authorization credentials.
6 | content:
7 | application/problem+json:
8 | schema:
9 | $ref: 'schemas.yaml#/components/schemas/Problem-Detail'
10 | example:
11 | type: 'https://example.net/unauthorized'
12 | title: 'Unauthorized'
13 | status: 401
14 | detail: 'Not authorized to perform this action.'
15 | instance: '/example-resource'
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/References/Animal.json:
--------------------------------------------------------------------------------
1 | {
2 | "title": "Animal",
3 | "type": "object",
4 | "x-abstract": true,
5 | "discriminator": "discriminator",
6 |
7 | "definitions": {
8 | "Cat": {
9 | "type": "object",
10 | "allOf": [
11 | {
12 | "$ref": "Animal.json"
13 | }
14 | ]
15 | },
16 | "PersianCat": {
17 | "type": "object",
18 | "allOf": [
19 | {
20 | "$ref": "#/definitions/Cat"
21 | }
22 | ]
23 | }
24 | }
25 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/ArrayTests.When_array_item_is_nullable_then_generated_CSharp_is_correct.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Items", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public System.Collections.Generic.ICollection Items { get; set; }
16 |
17 | }
18 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/ObsoleteTests.When_class_is_obsolete_then_obsolete_attribute_is_rendered.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | [System.Obsolete]
12 | public partial class ObsoleteTestClass
13 | {
14 |
15 | [Newtonsoft.Json.JsonProperty("Property", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
16 | public string Property { get; set; }
17 |
18 | }
19 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.Tests/Snapshots/DefaultGenerationTests.When_property_has_default_attribute_then_default_value_is_set_in_generated_Poco_CSharp_code.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Test", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public string Test { get; set; } = "foo";
16 |
17 | }
18 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript/Templates/File.liquid:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | // Generated using the NSwag toolchain v{{ ToolchainVersion }} (http://NJsonSchema.org)
4 | //
5 | //----------------------
6 |
7 | {{ ExtensionCode.ImportCode }}
8 |
9 | {%- if HasModuleName %}
10 | module {{ ModuleName }} {
11 | {%- endif %}
12 | {%- if HasNamespace %}
13 | namespace {{ Namespace }} {
14 | {%- endif %}
15 | {{ ExtensionCode.TopCode }}
16 |
17 | {{ Types }}
18 |
19 | {{ ExtensionCode.BottomCode }}
20 |
21 | {%- if HasNamespace %}
22 | }
23 | {%- endif %}
24 | {%- if HasModuleName %}
25 | }
26 | {%- endif %}
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/Generation/RecordTests.cs:
--------------------------------------------------------------------------------
1 | namespace NJsonSchema.Tests.Generation
2 | {
3 | public class RecordTests
4 | {
5 | public record Address
6 | {
7 | public string Street { get; set; }
8 | }
9 |
10 | [Fact]
11 | public void Should_have_only_one_property()
12 | {
13 | // Arrange
14 |
15 | // Act
16 | var schema = JsonSchema.FromType();
17 | var data = schema.ToJson();
18 |
19 | var add = new Address();
20 |
21 | // Assert
22 | Assert.Single(schema.Properties);
23 | }
24 | }
25 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/ArrayTests.When_array_property_is_required_then_array_instance_can_be_changed.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("ArrayProperty", Required = Newtonsoft.Json.Required.Always)]
15 | [System.ComponentModel.DataAnnotations.Required]
16 | public Foo ArrayProperty { get; set; } = new Bar();
17 |
18 | }
19 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_property_is_ObservableCollection_then_generated_code_uses_the_same_class.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Test", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public ObservableCollection Test { get; set; }
16 |
17 | }
18 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/ObjectPropertyRequiredTests.When_property_is_not_required_then_required_attribute_is_not_rendered_in_Swagger_mode.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Property", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public object Property { get; set; }
16 |
17 | }
18 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/StringPropertyRequiredTests.When_property_is_not_required_then_required_attribute_is_not_rendered_in_Swagger_mode.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Property", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public string Property { get; set; }
16 |
17 | }
18 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/ObsoleteTests.When_property_is_obsolete_then_obsolete_attribute_is_rendered.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class ObsoletePropertyTestClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Property", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | [System.Obsolete]
16 | public string Property { get; set; }
17 |
18 | }
19 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_enum_has_special_chars_then_they_should_be_converted.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public enum MyClass
12 | {
13 |
14 | [System.Runtime.Serialization.EnumMember(Value = @"application/json")]
15 | Application_json = 0,
16 |
17 |
18 | [System.Runtime.Serialization.EnumMember(Value = @"application/vnd.ms-excel")]
19 | Application_vnd_msExcel = 1,
20 |
21 |
22 | }
23 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/NJsonSchema.CodeGeneration.CSharp.Tests.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | NJsonSchema.CodeGeneration.CSharp.Tests
5 |
6 |
7 |
8 |
9 | Foobar.
10 |
11 |
12 |
13 |
14 | Foobar.
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/References/Snapshots/LocalReferencesTests.When_schema_references_external_schema_then_it_is_inlined_with_ToJson.verified.txt:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json-schema.org/draft-04/schema#",
3 | "type": "object",
4 | "properties": {
5 | "foo": {
6 | "$ref": "#/definitions/Animal"
7 | }
8 | },
9 | "definitions": {
10 | "Animal": {
11 | "type": "object",
12 | "properties": {
13 | "bar": {
14 | "name": {
15 | "type": "string"
16 | }
17 | }
18 | },
19 | "definitions": {
20 | "SubAnimal": {
21 | "type": "object"
22 | }
23 | }
24 | }
25 | }
26 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_enum_has_special_char_questionmark_then_it_should_be_converted.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public enum MyClass
12 | {
13 |
14 | [System.Runtime.Serialization.EnumMember(Value = @"application/json")]
15 | Application_json = 0,
16 |
17 |
18 | [System.Runtime.Serialization.EnumMember(Value = @"application/vnd.ms-excel?2")]
19 | Application_vnd_msExcel_2 = 1,
20 |
21 |
22 | }
23 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/ValueGeneratorTests.When_schema_contains_range_then_code_is_correctly_generated.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Foo", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | [System.ComponentModel.DataAnnotations.Range(2, int.MaxValue)]
16 | public int? Foo { get; set; }
17 |
18 | }
19 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/NJsonSchema.CodeGeneration.TypeScript.Tests.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | NJsonSchema.CodeGeneration.TypeScript.Tests
5 |
6 |
7 |
8 |
9 | Foobar.
10 |
11 |
12 |
13 |
14 | Foobar.
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.FromJson.liquid:
--------------------------------------------------------------------------------
1 | public static {{ ClassName }} FromJson(string data)
2 | {
3 | {%- if UseSystemTextJson %}
4 | var options = {{ JsonSerializerParameterCode }};
5 | {%- if JsonConvertersArrayCode contains "System.Text.Json.Serialization.JsonConverter[]" %}
6 | var converters = {{ JsonConvertersArrayCode }};
7 | foreach(var converter in converters)
8 | options.Converters.Add(converter);
9 | {%- endif %}
10 | return System.Text.Json.JsonSerializer.Deserialize<{{ ClassName }}>(data, options);
11 | {%- else %}
12 | return Newtonsoft.Json.JsonConvert.DeserializeObject<{{ ClassName }}>(data, {{ JsonSerializerParameterCode }});
13 | {%- endif %}
14 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/ObsoleteTests.When_class_is_obsolete_with_a_message_then_obsolete_attribute_with_a_message_is_rendered.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | [System.Obsolete("Reason class is \"obsolete\"")]
12 | public partial class ObsoleteWithMessageTestClass
13 | {
14 |
15 | [Newtonsoft.Json.JsonProperty("Property", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
16 | public string Property { get; set; }
17 |
18 | }
19 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.NewtonsoftJson.Tests/Generation/RecordTests.cs:
--------------------------------------------------------------------------------
1 | using NJsonSchema.NewtonsoftJson.Generation;
2 | using Xunit;
3 |
4 | namespace NJsonSchema.NewtonsoftJson.Tests.Generation
5 | {
6 | public class RecordTests
7 | {
8 | public record Address
9 | {
10 | public string Street { get; set; }
11 | }
12 |
13 | [Fact]
14 | public void Should_have_only_one_property()
15 | {
16 | // Arrange
17 |
18 | // Act
19 | var schema = NewtonsoftJsonSchemaGenerator.FromType();
20 | var data = schema.ToJson();
21 |
22 | // Assert
23 | Assert.Single(schema.Properties);
24 | }
25 | }
26 | }
--------------------------------------------------------------------------------
/src/NJsonSchema/app.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonEnumTests.WhenIntegerEnumUseEnumNameAttributes_ThenTheyAreIgnored.verified.txt:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json-schema.org/draft-04/schema#",
3 | "title": "IntegerEnumContainer",
4 | "type": "object",
5 | "additionalProperties": false,
6 | "properties": {
7 | "IntegerCloudCover": {
8 | "$ref": "#/definitions/IntegerCloudCover"
9 | }
10 | },
11 | "definitions": {
12 | "IntegerCloudCover": {
13 | "type": "integer",
14 | "description": "",
15 | "x-enumNames": [
16 | "Clear",
17 | "Partial",
18 | "Overcast"
19 | ],
20 | "enum": [
21 | 0,
22 | 1,
23 | 2
24 | ]
25 | }
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/ObsoleteTests.When_property_is_obsolete_with_a_message_then_obsolete_attribute_with_a_message_is_rendered.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class ObsoletePropertyWithMessageTestClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Property", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | [System.Obsolete("Reason property is \"obsolete\"")]
16 | public string Property { get; set; }
17 |
18 | }
19 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.Tests/Snapshots/EnumGenerationTests.When_enum_has_integer_value_then_CS_code_has_EnumMember_attribute.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public enum NumberEnum
12 | {
13 |
14 | _0562 = 10,
15 |
16 |
17 | _0532 = 15,
18 |
19 |
20 | }
21 |
22 | public partial class MyClass
23 | {
24 |
25 | [Newtonsoft.Json.JsonProperty("Bar", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
26 | public NumberEnum Bar { get; set; }
27 |
28 | }
29 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/TypeScriptDiscriminatorTests.When_generating_interface_contract_add_discriminator_string_literal.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | export interface Base {
13 | Type: EBase;
14 | }
15 |
16 | export type EBase = "OneChild" | "SecondChild";
17 |
18 | export interface OneChild extends Base {
19 | A: string | undefined;
20 | Type: 'OneChild';
21 | }
22 |
23 | export interface SecondChild extends Base {
24 | B: string | undefined;
25 | Type: 'SecondChild';
26 | }
27 |
28 | export interface MyClass {
29 | Child: Base | undefined;
30 | Children: Base[] | undefined;
31 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Benchmark/ConversionUtilitiesBenchmark.cs:
--------------------------------------------------------------------------------
1 | using BenchmarkDotNet.Attributes;
2 |
3 | namespace NJsonSchema.Benchmark;
4 |
5 | [MemoryDiagnoser]
6 | public class ConversionUtilitiesBenchmark
7 | {
8 | [Params("example_string", "1another_example", "ConversionUtilities", "ConvertToUpperCamelCase", "/foo/bar/baz", "")]
9 | public string Input { get; set; }
10 |
11 | [Benchmark]
12 | public string ConvertToUpperCamelCase()
13 | {
14 | return ConversionUtilities.ConvertToUpperCamelCase(Input, firstCharacterMustBeAlpha: true);
15 | }
16 |
17 | [Benchmark]
18 | public string ConvertToLowerCamelCase()
19 | {
20 | return ConversionUtilities.ConvertToLowerCamelCase(Input, firstCharacterMustBeAlpha: true);
21 | }
22 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Annotations/JsonSchemaIgnoreAttribute.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | namespace NJsonSchema.Annotations;
10 |
11 | /// Indicates that the marked class is ignored during the JSON Schema generation.
12 | [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)]
13 | public class JsonSchemaIgnoreAttribute : Attribute
14 | {
15 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonEnumTests.WhenStringEnumUsesEnumMemberAttribute_ThenItIsUsed.verified.txt:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json-schema.org/draft-04/schema#",
3 | "title": "StringEnumMemberContainer",
4 | "type": "object",
5 | "additionalProperties": false,
6 | "properties": {
7 | "CloudCover": {
8 | "$ref": "#/definitions/CloudCoverWithEnumMember"
9 | }
10 | },
11 | "definitions": {
12 | "CloudCoverWithEnumMember": {
13 | "type": "string",
14 | "description": "",
15 | "x-enumNames": [
16 | "Clear",
17 | "Partial",
18 | "Overcast"
19 | ],
20 | "enum": [
21 | "Clear",
22 | "Partly cloudy",
23 | "Overcast"
24 | ]
25 | }
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration/ITemplate.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | namespace NJsonSchema.CodeGeneration
10 | {
11 | /// Interface for a template.
12 | public interface ITemplate
13 | {
14 | /// Renders the template.
15 | /// The output.
16 | string Render();
17 | }
18 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/TypeScriptDiscriminatorTests.When_generating_interface_contract_add_discriminator.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | export interface Base {
13 | Type: EBase;
14 | }
15 |
16 | export enum EBase {
17 | OneChild = "OneChild",
18 | SecondChild = "SecondChild",
19 | }
20 |
21 | export interface OneChild extends Base {
22 | A: string | undefined;
23 | Type: EBase.OneChild;
24 | }
25 |
26 | export interface SecondChild extends Base {
27 | B: string | undefined;
28 | Type: EBase.SecondChild;
29 | }
30 |
31 | export interface MyClass {
32 | Child: Base | undefined;
33 | Children: Base[] | undefined;
34 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptEnumStyle.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | //-----------------------------------------------------------------------
7 |
8 | namespace NJsonSchema.CodeGeneration.TypeScript
9 | {
10 | /// The TypeScript enum styles.
11 | public enum TypeScriptEnumStyle
12 | {
13 | /// Generates enum.
14 | Enum,
15 |
16 | /// Generates enum as a string literal.
17 | StringLiteral,
18 | }
19 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_tuple_types_has_ints_then_it_is_generated_correctly_inlineNamedTuples=True.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class OuterList : System.Collections.ObjectModel.Collection>
12 | {
13 |
14 | }
15 |
16 | public partial class MyClass
17 | {
18 |
19 | [Newtonsoft.Json.JsonProperty("OuterList", Required = Newtonsoft.Json.Required.Always)]
20 | [System.ComponentModel.DataAnnotations.Required]
21 | public OuterList OuterList { get; set; } = new OuterList();
22 |
23 | }
24 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.NewtonsoftJson/NJsonSchema.NewtonsoftJson.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0;net462;net8.0
5 | bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/src/NJsonSchema.Benchmark/CsharpGeneratorBenchmark.cs:
--------------------------------------------------------------------------------
1 | using BenchmarkDotNet.Attributes;
2 | using NJsonSchema.CodeGeneration.CSharp;
3 |
4 | namespace NJsonSchema.Benchmark
5 | {
6 | [MemoryDiagnoser]
7 | public class CsharpGeneratorBenchmark
8 | {
9 | private string _json;
10 | private JsonSchema _schema;
11 |
12 | [GlobalSetup]
13 | public async Task Setup()
14 | {
15 | _json = await JsonSchemaBenchmark.ReadJson();
16 | _schema = await JsonSchema.FromJsonAsync(_json);
17 | }
18 |
19 | [Benchmark]
20 | public void GenerateFile()
21 | {
22 | var generator = new CSharpGenerator(_schema, new CSharpGeneratorSettings());
23 | generator.GenerateFile();
24 | }
25 | }
26 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/NullableTests.When_property_is_optional_and_GenerateNullableOptionalProperties_is_set_then_CSharp_property_is_nullable.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Property", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public int? Property { get; set; }
16 |
17 | [Newtonsoft.Json.JsonProperty("Property2", Required = Newtonsoft.Json.Required.Always)]
18 | public int Property2 { get; set; }
19 |
20 | }
21 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/Generation/FieldGenerationTests.cs:
--------------------------------------------------------------------------------
1 | using NJsonSchema.NewtonsoftJson.Generation;
2 |
3 | namespace NJsonSchema.Tests.Generation
4 | {
5 | public class FieldGenerationTests
6 | {
7 | public class MyTest
8 | {
9 | public string MyField;
10 | }
11 |
12 | [Fact]
13 | public async Task When_public_field_is_available_then_it_is_added_as_property()
14 | {
15 | // Arrange
16 |
17 |
18 | // Act
19 | var schema = NewtonsoftJsonSchemaGenerator.FromType();
20 | var json = schema.ToJson();
21 |
22 | // Assert
23 | Assert.True(schema.Properties["MyField"].Type.HasFlag(JsonObjectType.String));
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonEnumTests.WhenStringEnumUsesJsonStringEnumMemberName_ThenItIsUsed.verified.txt:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json-schema.org/draft-04/schema#",
3 | "title": "StringJsonStringEnumMemberNameContainer",
4 | "type": "object",
5 | "additionalProperties": false,
6 | "properties": {
7 | "CloudCover": {
8 | "$ref": "#/definitions/CloudCoverWithJsonStringEnumMemberName"
9 | }
10 | },
11 | "definitions": {
12 | "CloudCoverWithJsonStringEnumMemberName": {
13 | "type": "string",
14 | "description": "",
15 | "x-enumNames": [
16 | "Clear",
17 | "Partial",
18 | "Overcast"
19 | ],
20 | "enum": [
21 | "Clear",
22 | "Partly cloudy",
23 | "Overcast"
24 | ]
25 | }
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/NullableTests.When_property_is_optional_and_GenerateNullableOptionalProperties_is_not_set_then_CSharp_property_is_not_nullable.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Property", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public int Property { get; set; }
16 |
17 | [Newtonsoft.Json.JsonProperty("Property2", Required = Newtonsoft.Json.Required.Always)]
18 | public int Property2 { get; set; }
19 |
20 | }
21 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/Generation/ShouldSerializeTests.cs:
--------------------------------------------------------------------------------
1 | using NJsonSchema.NewtonsoftJson.Generation;
2 |
3 | namespace NJsonSchema.Tests.Generation
4 | {
5 | public class ShouldSerializeTests
6 | {
7 | public class Test
8 | {
9 | public string Name { get; set; }
10 |
11 | public bool ShouldSerializeName()
12 | {
13 | return !string.IsNullOrEmpty(Name);
14 | }
15 | }
16 |
17 | [Fact]
18 | public void When_ShouldSerialize_method_exists_then_schema_is_generated()
19 | {
20 | // Arrange
21 | var schema = NewtonsoftJsonSchemaGenerator.FromType();
22 |
23 | // Act
24 |
25 |
26 | // Assert
27 | Assert.NotNull(schema);
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/NJsonSchema.Benchmark/TypeScriptGeneratorBenchmark.cs:
--------------------------------------------------------------------------------
1 | using BenchmarkDotNet.Attributes;
2 | using NJsonSchema.CodeGeneration.TypeScript;
3 |
4 | namespace NJsonSchema.Benchmark
5 | {
6 | [MemoryDiagnoser]
7 | public class TypeScriptGeneratorBenchmark
8 | {
9 | private string _json;
10 | private JsonSchema _schema;
11 |
12 | [GlobalSetup]
13 | public async Task Setup()
14 | {
15 | _json = await JsonSchemaBenchmark.ReadJson();
16 | _schema = await JsonSchema.FromJsonAsync(_json);
17 | }
18 |
19 | [Benchmark]
20 | public void GenerateFile()
21 | {
22 | var generator = new TypeScriptGenerator(_schema, new TypeScriptGeneratorSettings());
23 | generator.GenerateFile();
24 | }
25 | }
26 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/InterfaceTests.When_interface_has_properties_then_properties_are_included_in_schema.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class Person
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("LastName", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public string LastName { get; set; }
16 |
17 | [Newtonsoft.Json.JsonProperty("FirstName", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
18 | public string FirstName { get; set; }
19 |
20 | }
21 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp/CSharpJsonLibrary.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | namespace NJsonSchema.CodeGeneration.CSharp
10 | {
11 | /// The CSharp JSON library to use.
12 | public enum CSharpJsonLibrary
13 | {
14 | /// Use Newtonsoft.Json
15 | NewtonsoftJson,
16 |
17 | /// Use System.Text.Json
18 | SystemTextJson
19 | }
20 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/TypeScriptDiscriminatorTests.When_parameter_is_abstract_then_generate_union_interface.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | export interface Base {
13 | Type: string;
14 | }
15 |
16 | export interface OneChild extends Base {
17 | A: string | undefined;
18 | Type: EBase.OneChild;
19 | }
20 |
21 | export enum EBase {
22 | OneChild = "OneChild",
23 | SecondChild = "SecondChild",
24 | }
25 |
26 | export interface SecondChild extends Base {
27 | B: string | undefined;
28 | Type: EBase.SecondChild;
29 | }
30 |
31 | export interface MyClass {
32 | Child: OneChild | SecondChild | undefined;
33 | Children: (OneChild | SecondChild)[] | undefined;
34 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Demo/Tests/minItems.json:
--------------------------------------------------------------------------------
1 | [
2 | {
3 | "description": "minItems validation",
4 | "schema": {"minItems": 1},
5 | "tests": [
6 | {
7 | "description": "longer is valid",
8 | "data": [1, 2],
9 | "valid": true
10 | },
11 | {
12 | "description": "exact length is valid",
13 | "data": [1],
14 | "valid": true
15 | },
16 | {
17 | "description": "too short is invalid",
18 | "data": [],
19 | "valid": false
20 | },
21 | {
22 | "description": "ignores non-arrays",
23 | "data": "",
24 | "valid": true
25 | }
26 | ]
27 | }
28 | ]
29 |
--------------------------------------------------------------------------------
/src/NJsonSchema/Generation/ISchemaProcessor.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | namespace NJsonSchema.Generation
10 | {
11 | /// The schema processor interface.
12 | public interface ISchemaProcessor
13 | {
14 | /// Processes the specified JSON Schema.
15 | /// The schema context.
16 | void Process(SchemaProcessorContext context);
17 | }
18 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/InterfaceTests.When_class_implements_interface_then_properties_are_included_in_schema.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class Person
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("LastName", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public string LastName { get; set; }
16 |
17 | [Newtonsoft.Json.JsonProperty("FirstName", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
18 | public string FirstName { get; set; }
19 |
20 | }
21 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Annotations/ItemsCanBeNullAttribute.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | namespace NJsonSchema.Annotations;
10 |
11 | /// Annotation to specify that array items or dictionary values are nullable.
12 | [AttributeUsage(AttributeTargets.Property | AttributeTargets.Parameter | AttributeTargets.ReturnValue |
13 | AttributeTargets.Field)]
14 | public class ItemsCanBeNullAttribute : Attribute
15 | {
16 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Demo/Tests/maxItems.json:
--------------------------------------------------------------------------------
1 | [
2 | {
3 | "description": "maxItems validation",
4 | "schema": {"maxItems": 2},
5 | "tests": [
6 | {
7 | "description": "shorter is valid",
8 | "data": [1],
9 | "valid": true
10 | },
11 | {
12 | "description": "exact length is valid",
13 | "data": [1, 2],
14 | "valid": true
15 | },
16 | {
17 | "description": "too long is invalid",
18 | "data": [1, 2, 3],
19 | "valid": false
20 | },
21 | {
22 | "description": "ignores non-arrays",
23 | "data": "foobar",
24 | "valid": true
25 | }
26 | ]
27 | }
28 | ]
29 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptNullValue.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | namespace NJsonSchema.CodeGeneration.TypeScript
10 | {
11 | /// The TypeScript null value used in object initializers.
12 | public enum TypeScriptNullValue
13 | {
14 | /// JavaScript null.
15 | Null,
16 |
17 | /// JavaScript undefined.
18 | Undefined
19 | }
20 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/VerifyHelper.cs:
--------------------------------------------------------------------------------
1 | using System.Runtime.CompilerServices;
2 |
3 | namespace NJsonSchema.CodeGeneration.Tests;
4 |
5 | public static class VerifyHelper
6 | {
7 | ///
8 | /// Helper to run verify tests with sane defaults.
9 | ///
10 | public static SettingsTask Verify(string output, [CallerFilePath] string sourceFile = "")
11 | {
12 | return Verifier
13 | .Verify(output, sourceFile: sourceFile)
14 | .ScrubLinesContaining(
15 | StringComparison.OrdinalIgnoreCase,
16 | "Generated using the NSwag toolchain",
17 | "Generated using the NJsonSchema",
18 | "[System.CodeDom.Compiler.GeneratedCode(\"NJsonSchema\"")
19 | .UseDirectory("Snapshots")
20 | .AutoVerify(includeBuildServer: false);
21 | }
22 | }
--------------------------------------------------------------------------------
/src/NJsonSchema/Infrastructure/CollectionExtensions.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Collections.ObjectModel;
3 |
4 | namespace NJsonSchema;
5 |
6 | internal static class CollectionExtensions
7 | {
8 | public static int IndexOf(this ICollection collection, T item)
9 | {
10 | if (collection is List l)
11 | {
12 | return l.IndexOf(item);
13 | }
14 |
15 | if (collection is Collection c)
16 | {
17 | return c.IndexOf(item);
18 | }
19 |
20 | int index = 0;
21 | foreach (var element in collection)
22 | {
23 | if (EqualityComparer.Default.Equals(element, item))
24 | {
25 | return index;
26 | }
27 | index++;
28 | }
29 | return -1; // Item not found
30 | }
31 | }
--------------------------------------------------------------------------------
/src/NJsonSchema/IDocumentPathProvider.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | using Newtonsoft.Json;
10 |
11 | namespace NJsonSchema
12 | {
13 | /// Provides a property to get a documents path or base URI.
14 | public interface IDocumentPathProvider
15 | {
16 | /// Gets the document path (URI or file path).
17 | [JsonIgnore]
18 | string? DocumentPath { get; set; }
19 | }
20 | }
--------------------------------------------------------------------------------
/src/NJsonSchema/IJsonExtensionObject.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | namespace NJsonSchema
10 | {
11 | /// The base JSON interface with extension data.
12 | public interface IJsonExtensionObject
13 | {
14 | /// Gets or sets the extension data (i.e. additional properties which are not directly defined by the JSON object).
15 | IDictionary? ExtensionData { get; set; }
16 | }
17 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/StringPropertyRequiredTests.When_property_is_required_then_required_attribute_is_rendered.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Property", Required = Newtonsoft.Json.Required.Always)]
15 | [System.ComponentModel.DataAnnotations.Required]
16 | public string Property { get; set; }
17 |
18 | [Newtonsoft.Json.JsonProperty("Property2", Required = Newtonsoft.Json.Required.Always)]
19 | [System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
20 | public string Property2 { get; set; }
21 |
22 | }
23 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.Tests/ApiSurfaceGuard.cs:
--------------------------------------------------------------------------------
1 | namespace NJsonSchema.CodeGeneration.Tests;
2 |
3 | public class ApiSurfaceGuard
4 | {
5 | private abstract class TypeResolverBaseApiGuard : TypeResolverBase
6 | {
7 | protected TypeResolverBaseApiGuard(CodeGeneratorSettingsBase settings) : base(settings)
8 | {
9 | }
10 |
11 | // dummy implementation making sure this method stays overridable
12 | public override string GetOrGenerateTypeName(JsonSchema schema, string typeNameHint)
13 | {
14 | throw new System.NotImplementedException();
15 | }
16 |
17 | // dummy implementation making sure this method stays overridable
18 | public override JsonSchema RemoveNullability(JsonSchema schema)
19 | {
20 | throw new System.NotImplementedException();
21 | }
22 | }
23 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Demo/NJsonSchema.Demo.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net8.0
5 | false
6 | true
7 |
8 | Exe
9 |
10 | disable
11 | $(NoWarn);CS1591;IDE0005
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/src/NJsonSchema.Demo/Tests/minProperties.json:
--------------------------------------------------------------------------------
1 | [
2 | {
3 | "description": "minProperties validation",
4 | "schema": {"minProperties": 1},
5 | "tests": [
6 | {
7 | "description": "longer is valid",
8 | "data": {"foo": 1, "bar": 2},
9 | "valid": true
10 | },
11 | {
12 | "description": "exact length is valid",
13 | "data": {"foo": 1},
14 | "valid": true
15 | },
16 | {
17 | "description": "too short is invalid",
18 | "data": {},
19 | "valid": false
20 | },
21 | {
22 | "description": "ignores non-objects",
23 | "data": "",
24 | "valid": true
25 | }
26 | ]
27 | }
28 | ]
29 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/StringPropertyRequiredTests.When_property_is_required_then_required_attribute_is_rendered_in_Swagger_mode.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Property", Required = Newtonsoft.Json.Required.Always)]
15 | [System.ComponentModel.DataAnnotations.Required]
16 | public string Property { get; set; }
17 |
18 | [Newtonsoft.Json.JsonProperty("Property2", Required = Newtonsoft.Json.Required.Always)]
19 | [System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
20 | public string Property2 { get; set; }
21 |
22 | }
23 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/InheritanceTests.When_empty_class_inherits_from_dictionary_then_allOf_inheritance_still_works.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | ///
12 | /// Foobar.
13 | ///
14 | public partial class EmptyClassInheritingDictionary : System.Collections.Generic.Dictionary
15 | {
16 |
17 | }
18 |
19 | public partial class MyContainer
20 | {
21 |
22 | [Newtonsoft.Json.JsonProperty("CustomDictionary", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
23 | public EmptyClassInheritingDictionary CustomDictionary { get; set; }
24 |
25 | }
26 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/NullableReferenceTypesTests.When_property_is_optional_and_GenerateNullableReferenceTypes_is_not_set_then_CSharp_property_is_not_nullable.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Property", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public object Property { get; set; }
16 |
17 | [Newtonsoft.Json.JsonProperty("property2", Required = Newtonsoft.Json.Required.Always)]
18 | [System.ComponentModel.DataAnnotations.Required]
19 | public object Property2 { get; set; }
20 |
21 | }
22 | }
--------------------------------------------------------------------------------
/src/NJsonSchema/Generation/ISchemaNameGenerator.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | namespace NJsonSchema.Generation
10 | {
11 | /// The schema name generator.
12 | public interface ISchemaNameGenerator
13 | {
14 | /// Generates the name of the JSON Schema for the given type.
15 | /// The type.
16 | /// The new name.
17 | string Generate(Type type);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_documentation_present_produces_valid_xml_documentation_syntax.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | ///
15 | /// Summary is here
16 | ///
17 | ///
spanning multiple lines
18 | ///
19 | ///
like this.
20 | ///
21 | ///
22 | [Newtonsoft.Json.JsonProperty("HelloMessage", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
23 | public string HelloMessage { get; set; }
24 |
25 | }
26 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Demo/Tests/maxProperties.json:
--------------------------------------------------------------------------------
1 | [
2 | {
3 | "description": "maxProperties validation",
4 | "schema": {"maxProperties": 2},
5 | "tests": [
6 | {
7 | "description": "shorter is valid",
8 | "data": {"foo": 1},
9 | "valid": true
10 | },
11 | {
12 | "description": "exact length is valid",
13 | "data": {"foo": 1, "bar": 2},
14 | "valid": true
15 | },
16 | {
17 | "description": "too long is invalid",
18 | "data": {"foo": 1, "bar": 2, "baz": 3},
19 | "valid": false
20 | },
21 | {
22 | "description": "ignores non-objects",
23 | "data": "foobar",
24 | "valid": true
25 | }
26 | ]
27 | }
28 | ]
29 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/ClassGenerationTests.Verify_output_style=Interface.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | export interface Person {
13 | FirstName: string | undefined;
14 | LastName: string | undefined;
15 | }
16 |
17 | export interface Student extends Person {
18 | Study: string | undefined;
19 | }
20 |
21 | export interface MyClass {
22 | Name: string | undefined;
23 | DateOfBirth: Date;
24 | PrimitiveArray: number[] | undefined;
25 | PrimitiveDictionary: { [key: string]: number; } | undefined;
26 | DateArray: Date[] | undefined;
27 | DateDictionary: { [key: string]: Date; } | undefined;
28 | Reference: Student | undefined;
29 | Array: Student[] | undefined;
30 | Dictionary: { [key: string]: Student; } | undefined;
31 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Benchmark/JsonSchemaBenchmark.cs:
--------------------------------------------------------------------------------
1 | using BenchmarkDotNet.Attributes;
2 |
3 | namespace NJsonSchema.Benchmark
4 | {
5 | [MemoryDiagnoser]
6 | public class JsonSchemaBenchmark
7 | {
8 | private string _json;
9 |
10 | [GlobalSetup]
11 | public async Task Setup()
12 | {
13 | _json = await ReadJson();
14 | }
15 |
16 | internal static Task ReadJson()
17 | {
18 | var assembly = typeof(JsonSchemaBenchmark).Assembly;
19 | var file = assembly.GetManifestResourceNames().First(x => x.Contains("LargeSchema.json"));
20 | return new StreamReader(assembly.GetManifestResourceStream(file)).ReadToEndAsync();
21 | }
22 |
23 | [Benchmark]
24 | public Task FromJsonAsync()
25 | {
26 | return JsonSchema.FromJsonAsync(_json);
27 | }
28 | }
29 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Annotations/JsonObjectType.cs:
--------------------------------------------------------------------------------
1 | namespace NJsonSchema;
2 |
3 | ///
4 | /// Enumeration of the possible object types.
5 | ///
6 | /// Keep in sync with NJsonSchema.JsonObjectType
7 | ///
8 | [Flags]
9 | public enum JsonObjectType
10 | {
11 | /// No object type.
12 | None = 0,
13 |
14 | /// An array.
15 | Array = 1,
16 |
17 | /// A boolean value.
18 | Boolean = 2,
19 |
20 | /// An integer value.
21 | Integer = 4,
22 |
23 | /// A null.
24 | Null = 8,
25 |
26 | /// An number value.
27 | Number = 16,
28 |
29 | /// An object.
30 | Object = 32,
31 |
32 | /// A string.
33 | String = 64,
34 |
35 | /// A file (used in Swagger specifications).
36 | File = 128,
37 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration/CodeArtifactLanguage.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | namespace NJsonSchema.CodeGeneration
10 | {
11 | /// The code artifact type.
12 | public enum CodeArtifactLanguage
13 | {
14 | /// Undefined.
15 | Undefined,
16 |
17 | /// C#.
18 | CSharp,
19 |
20 | /// TypeScript.
21 | TypeScript,
22 |
23 | /// Html.
24 | Html
25 | }
26 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Annotations/CanBeNullAttribute.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | namespace NJsonSchema.Annotations;
10 |
11 | /// Indicates that the value of the marked element is nullable.
12 | [AttributeUsage(
13 | AttributeTargets.Method |
14 | AttributeTargets.Parameter |
15 | AttributeTargets.Property |
16 | AttributeTargets.ReturnValue |
17 | AttributeTargets.Delegate |
18 | AttributeTargets.Field |
19 | AttributeTargets.Event)]
20 | public class CanBeNullAttribute : Attribute
21 | {
22 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration/CodeArtifactCategory.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | namespace NJsonSchema.CodeGeneration
10 | {
11 | /// The code artifact category.
12 | public enum CodeArtifactCategory
13 | {
14 | /// Undefined.
15 | Undefined,
16 |
17 | /// Client.
18 | Client,
19 |
20 | /// Contract.
21 | Contract,
22 |
23 | /// Utility.
24 | Utility
25 | }
26 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Annotations/NotNullAttribute.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | namespace NJsonSchema.Annotations;
10 |
11 | /// Indicates that the value of the marked element could never be null.
12 | [AttributeUsage(
13 | AttributeTargets.Method |
14 | AttributeTargets.Parameter |
15 | AttributeTargets.Property |
16 | AttributeTargets.ReturnValue |
17 | AttributeTargets.Delegate |
18 | AttributeTargets.Field |
19 | AttributeTargets.Event)]
20 | public class NotNullAttribute : Attribute
21 | {
22 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Benchmark/CSharpTypeResolverBenchmark.cs:
--------------------------------------------------------------------------------
1 | using BenchmarkDotNet.Attributes;
2 | using NJsonSchema.CodeGeneration.CSharp;
3 |
4 | namespace NJsonSchema.Benchmark;
5 |
6 | [MemoryDiagnoser]
7 | public class CSharpTypeResolverBenchmark
8 | {
9 | private Dictionary _definitions;
10 | private CSharpGeneratorSettings _settings;
11 |
12 | [GlobalSetup]
13 | public async Task Setup()
14 | {
15 | var json = await JsonSchemaBenchmark.ReadJson();
16 | var schema = await JsonSchema.FromJsonAsync(json);
17 | _definitions = schema.Definitions.ToDictionary(p => p.Key, p => p.Value);
18 | _settings = new CSharpGeneratorSettings();
19 | }
20 |
21 | [Benchmark]
22 | public void RegisterSchemaDefinitions()
23 | {
24 | var resolver = new CSharpTypeResolver(_settings, exceptionSchema: null);
25 | resolver.RegisterSchemaDefinitions(_definitions);
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/NullableReferenceTypesTests.When_property_is_optional_and_GenerateNullableOptionalProperties_is_set_then_CSharp_property_is_nullable.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | #nullable enable
8 |
9 |
10 | namespace MyNamespace
11 | {
12 | #pragma warning disable // Disable all warnings
13 |
14 | public partial class MyClass
15 | {
16 |
17 | [Newtonsoft.Json.JsonProperty("Property", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
18 | public object? Property { get; set; } = default!;
19 |
20 | [Newtonsoft.Json.JsonProperty("property2", Required = Newtonsoft.Json.Required.Always)]
21 | [System.ComponentModel.DataAnnotations.Required]
22 | public object Property2 { get; set; } = default!;
23 |
24 | }
25 | }
--------------------------------------------------------------------------------
/src/NJsonSchema/Generation/ReferenceTypeNullHandling.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | namespace NJsonSchema.Generation
10 | {
11 | /// Specifies the default null handling for reference types when no nullability information is available.
12 | public enum ReferenceTypeNullHandling
13 | {
14 | /// Reference types are nullable by default (C# default).
15 | Null,
16 |
17 | /// Reference types cannot be null by default.
18 | NotNull
19 | }
20 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript/Templates/Interface.liquid:
--------------------------------------------------------------------------------
1 | {%- if HasDescription -%}
2 | /** {{ Description }} */
3 | {%- endif -%}
4 | {% if ExportTypes %}export {% endif %}interface {{ ClassName }}{{ Inheritance }} {
5 | {%- for property in Properties -%}
6 | {%- if property.HasDescription -%}
7 | /** {{ property.Description }} */
8 | {%- endif -%}
9 | {% if property.IsReadOnly %}readonly {% endif %}{{ property.InterfaceName }}{% if property.IsOptional %}?{% endif %}: {{ property.Type }}{{ property.TypePostfix }};
10 | {%- endfor -%}
11 | {%- if HasIndexerProperty -%}
12 |
13 | [key: string]: {{ IndexerPropertyValueType }};
14 | {%- endif -%}
15 | }
16 | {%- if GenerateTypeCheckFunctions and HasInheritance -%}
17 |
18 | {% if ExportTypes %}export {% endif %}function is{{ ClassName }}(object: any): object is {{ ClassName }} {
19 | return object && object['{{ BaseDiscriminator }}'] === '{{ DiscriminatorName }}';
20 | }
21 | {%- endif -%}
--------------------------------------------------------------------------------
/src/NJsonSchema.Annotations/JsonSchemaDateAttribute.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | namespace NJsonSchema.Annotations;
10 |
11 | /// Annotation to mark a property or class as string type with format 'date'.
12 | public class JsonSchemaDateAttribute : JsonSchemaAttribute
13 | {
14 | /// Initializes a new instance of the class.
15 | public JsonSchemaDateAttribute()
16 | : base(JsonObjectType.String)
17 | {
18 | Format = JsonFormatStrings.Date;
19 | }
20 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_enum_type_name_is_missing_then_default_value_is_still_correctly_set.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace Foo
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("ConstructionCode", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public MyClassConstructionCode ConstructionCode { get; set; } = Foo.MyClassConstructionCode.JOIST_MAS;
16 |
17 | }
18 |
19 | public enum MyClassConstructionCode
20 | {
21 |
22 | FIRE_RSTV = FIRE_RSTV,
23 |
24 |
25 | FRAME = FRAME,
26 |
27 |
28 | JOIST_MAS = JOIST_MAS,
29 |
30 |
31 | NON_CBST = NON_CBST,
32 |
33 |
34 | }
35 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration/IPropertyNameGenerator.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | namespace NJsonSchema.CodeGeneration
10 | {
11 | /// Generates the property name for a given .
12 | public interface IPropertyNameGenerator
13 | {
14 | /// Generates the property name.
15 | /// The property.
16 | /// The new name.
17 | string Generate(JsonSchemaProperty property);
18 | }
19 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.Tests/Snapshots/EnumGenerationTests.When_enum_has_string_value_then_CS_code_has_JsonStringEnumMemberName_attribute.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public enum StringEnum
12 | {
13 |
14 | [System.Text.Json.Serialization.JsonStringEnumMemberName(@"0562")]
15 | _0562 = 0,
16 |
17 |
18 | [System.Text.Json.Serialization.JsonStringEnumMemberName(@"0532")]
19 | _0532 = 1,
20 |
21 |
22 | }
23 |
24 | public partial class MyClass
25 | {
26 |
27 | [System.Text.Json.Serialization.JsonPropertyName("Bar")]
28 | [System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.JsonStringEnumConverter))]
29 | public StringEnum Bar { get; set; }
30 |
31 | }
32 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_property_is_required_then_CSharp_code_is_correct.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("FirstName", Required = Newtonsoft.Json.Required.Always)]
15 | [System.ComponentModel.DataAnnotations.Required]
16 | public string FirstName { get; set; }
17 |
18 | [Newtonsoft.Json.JsonProperty("MiddleName", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
19 | public string MiddleName { get; set; }
20 |
21 | [Newtonsoft.Json.JsonProperty("Age", Required = Newtonsoft.Json.Required.AllowNull)]
22 | public int? Age { get; set; }
23 |
24 | }
25 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.Tests/Snapshots/EnumGenerationTests.When_enum_has_string_value_then_CS_code_has_EnumMember_attribute.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public enum StringEnum
12 | {
13 |
14 | [System.Runtime.Serialization.EnumMember(Value = @"0562")]
15 | _0562 = 0,
16 |
17 |
18 | [System.Runtime.Serialization.EnumMember(Value = @"0532")]
19 | _0532 = 1,
20 |
21 |
22 | }
23 |
24 | public partial class MyClass
25 | {
26 |
27 | [Newtonsoft.Json.JsonProperty("Bar", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
28 | [Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
29 | public StringEnum Bar { get; set; }
30 |
31 | }
32 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/TypeScriptGeneratorTests.When_property_is_readonly_then_ts_property_is_also_readonly.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | export interface Person {
13 | FirstName: string;
14 | lastName: string | undefined;
15 | readonly Birthday: Date;
16 | TimeSpan: string;
17 | TimeSpanOrNull: string | undefined;
18 | Gender: Gender;
19 | GenderOrNull: Gender | undefined;
20 | address: Address;
21 | AddressOrNull: Address | undefined;
22 | Array: string[] | undefined;
23 | Dictionary: { [key: string]: number; } | undefined;
24 | }
25 |
26 | export enum Gender {
27 | Male = 0,
28 | Female = 1,
29 | }
30 |
31 | export interface Address {
32 | Street: string | undefined;
33 | City: string | undefined;
34 | }
35 |
36 | export interface MyClass extends Person {
37 | Class: string | undefined;
38 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/TypeScriptGeneratorTests.When_allOf_contains_one_schema_then_csharp_inheritance_is_generated.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | export interface Person {
13 | FirstName: string;
14 | lastName: string | undefined;
15 | readonly Birthday: Date;
16 | TimeSpan: string;
17 | TimeSpanOrNull: string | undefined;
18 | Gender: Gender;
19 | GenderOrNull: Gender | undefined;
20 | address: Address;
21 | AddressOrNull: Address | undefined;
22 | Array: string[] | undefined;
23 | Dictionary: { [key: string]: number; } | undefined;
24 | }
25 |
26 | export enum Gender {
27 | Male = 0,
28 | Female = 1,
29 | }
30 |
31 | export interface Address {
32 | Street: string | undefined;
33 | City: string | undefined;
34 | }
35 |
36 | export interface Teacher extends Person {
37 | Class: string | undefined;
38 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/TypeScriptGeneratorTests.When_property_is_required_name_then_TypeScript_property_is_not_optional.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | export interface Person {
13 | FirstName: string;
14 | lastName: string | undefined;
15 | readonly Birthday: Date;
16 | TimeSpan: string;
17 | TimeSpanOrNull: string | undefined;
18 | Gender: Gender;
19 | GenderOrNull: Gender | undefined;
20 | address: Address;
21 | AddressOrNull: Address | undefined;
22 | Array: string[] | undefined;
23 | Dictionary: { [key: string]: number; } | undefined;
24 | }
25 |
26 | export enum Gender {
27 | Male = 0,
28 | Female = 1,
29 | }
30 |
31 | export interface Address {
32 | Street: string | undefined;
33 | City: string | undefined;
34 | }
35 |
36 | export interface Teacher extends Person {
37 | Class: string | undefined;
38 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Demo/Tests/definitions.json:
--------------------------------------------------------------------------------
1 | [
2 | {
3 | "description": "valid definition",
4 | "schema": {"$ref": "http://json-schema.org/draft-04/schema#"},
5 | "tests": [
6 | {
7 | "description": "valid definition schema",
8 | "data": {
9 | "definitions": {
10 | "foo": {"type": "integer"}
11 | }
12 | },
13 | "valid": true
14 | }
15 | ]
16 | },
17 | {
18 | "description": "invalid definition",
19 | "schema": {"$ref": "http://json-schema.org/draft-04/schema#"},
20 | "tests": [
21 | {
22 | "description": "invalid definition schema",
23 | "data": {
24 | "definitions": {
25 | "foo": {"type": 1}
26 | }
27 | },
28 | "valid": false
29 | }
30 | ]
31 | }
32 | ]
33 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/EnumTests.When_class_has_enum_array_property_then_enum_name_is_preserved.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public enum SomeEnum
12 | {
13 |
14 | Thing1 = 0,
15 |
16 |
17 | Thing2 = 1,
18 |
19 |
20 | }
21 |
22 | public partial class SomeClass
23 | {
24 |
25 | [Newtonsoft.Json.JsonProperty("SomeProperty", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
26 | public int SomeProperty { get; set; }
27 |
28 | [Newtonsoft.Json.JsonProperty("SomeEnums", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
29 | public System.Collections.Generic.ICollection SomeEnums { get; set; }
30 |
31 | }
32 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_record_no_setter_in_class_and_constructor_provided.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class Address
12 | {
13 | [Newtonsoft.Json.JsonConstructor]
14 | public Address(string @city, string @street)
15 | {
16 | this.Street = @street;
17 | this.City = @city;
18 | }
19 |
20 | [Newtonsoft.Json.JsonProperty("Street", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
21 | public string Street { get; }
22 |
23 | [Newtonsoft.Json.JsonProperty("City", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
24 | public string City { get; }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/TypeScriptGeneratorTests.When_property_name_does_not_match_property_name_then_casing_is_correct_in_output.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | export interface Person {
13 | FirstName: string;
14 | lastName: string | undefined;
15 | readonly Birthday: Date;
16 | TimeSpan: string;
17 | TimeSpanOrNull: string | undefined;
18 | Gender: Gender;
19 | GenderOrNull: Gender | undefined;
20 | address: Address;
21 | AddressOrNull: Address | undefined;
22 | Array: string[] | undefined;
23 | Dictionary: { [key: string]: number; } | undefined;
24 | }
25 |
26 | export enum Gender {
27 | Male = 0,
28 | Female = 1,
29 | }
30 |
31 | export interface Address {
32 | Street: string | undefined;
33 | City: string | undefined;
34 | }
35 |
36 | export interface Teacher extends Person {
37 | Class: string | undefined;
38 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Demo/Tests/pattern.json:
--------------------------------------------------------------------------------
1 | [
2 | {
3 | "description": "pattern validation",
4 | "schema": {"pattern": "^a*$"},
5 | "tests": [
6 | {
7 | "description": "a matching pattern is valid",
8 | "data": "aaa",
9 | "valid": true
10 | },
11 | {
12 | "description": "a non-matching pattern is invalid",
13 | "data": "abc",
14 | "valid": false
15 | },
16 | {
17 | "description": "ignores non-strings",
18 | "data": true,
19 | "valid": true
20 | }
21 | ]
22 | },
23 | {
24 | "description": "pattern is not anchored",
25 | "schema": {"pattern": "a+"},
26 | "tests": [
27 | {
28 | "description": "matches a substring",
29 | "data": "xxaayy",
30 | "valid": true
31 | }
32 | ]
33 | }
34 | ]
35 |
--------------------------------------------------------------------------------
/src/NJsonSchema.Annotations/IJsonSchemaExtensionDataAttribute.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | namespace NJsonSchema.Annotations;
10 |
11 | /// Interface to add an extension data property to a class or property, implementation needs to inherit from System.Attribute.
12 | #pragma warning disable CA1711 // Rename type name IJsonSchemaExtensionDataAttribute so that it does not end in 'Attribute'
13 | public interface IJsonSchemaExtensionDataAttribute
14 | #pragma warning restore CA1711
15 | {
16 | /// Gets the extension data.
17 | IReadOnlyDictionary ExtensionData { get; }
18 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_csharp_record_init_in_record_and_constructor_provided.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial record Address
12 | {
13 | [Newtonsoft.Json.JsonConstructor]
14 | public Address(string @street, string @city)
15 | {
16 | this.Street = @street;
17 | this.City = @city;
18 | }
19 |
20 | [Newtonsoft.Json.JsonProperty("Street", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
21 | public string Street { get; init; }
22 |
23 | [Newtonsoft.Json.JsonProperty("City", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
24 | public string City { get; init; }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/PropertyNameTests.cs:
--------------------------------------------------------------------------------
1 | using NJsonSchema.CodeGeneration.Tests;
2 | using NJsonSchema.NewtonsoftJson.Generation;
3 |
4 | namespace NJsonSchema.CodeGeneration.TypeScript.Tests;
5 |
6 | public class PropertyNameTests
7 | {
8 | private class TypeWithRestrictedProperties
9 | {
10 | public string Constructor { get; set; }
11 | public string Init { get; set; }
12 | public string FromJS { get; set; }
13 | public string ToJSON { get; set; }
14 | }
15 |
16 | [Fact]
17 | public async Task When_class_has_restricted_properties_they_are_escaped()
18 | {
19 | var schema = NewtonsoftJsonSchemaGenerator.FromType();
20 |
21 | var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings());
22 | var output = generator.GenerateFile(nameof(TypeWithRestrictedProperties));
23 |
24 | await VerifyHelper.Verify(output);
25 | TypeScriptCompiler.AssertCompile(output);
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptTypeStyle.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | namespace NJsonSchema.CodeGeneration.TypeScript
10 | {
11 | /// The TypeScript type styles.
12 | public enum TypeScriptTypeStyle
13 | {
14 | /// Generates interfaces which can add typings to existing JSON.
15 | Interface,
16 |
17 | /// Generates classes which can be constructed from JSON.
18 | Class,
19 |
20 | /// Generates classes with KnockoutJS observable properties.
21 | KnockoutClass
22 | }
23 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_csharp_record_no_setter_in_record_and_constructor_provided.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial record Address
12 | {
13 | [Newtonsoft.Json.JsonConstructor]
14 | public Address(string @street, string @city)
15 | {
16 | this.Street = @street;
17 | this.City = @city;
18 | }
19 |
20 | [Newtonsoft.Json.JsonProperty("Street", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
21 | public string Street { get; init; }
22 |
23 | [Newtonsoft.Json.JsonProperty("City", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
24 | public string City { get; init; }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_native_record_no_setter_in_class_and_constructor_provided.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial record Address
12 | {
13 | [Newtonsoft.Json.JsonConstructor]
14 | public Address(string @city, string @street)
15 | {
16 | this.Street = @street;
17 | this.City = @city;
18 | }
19 |
20 | [Newtonsoft.Json.JsonProperty("Street", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
21 | public string Street { get; init; }
22 |
23 | [Newtonsoft.Json.JsonProperty("City", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
24 | public string City { get; init; }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp/NJsonSchema.CodeGeneration.CSharp.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0;net462;net8.0
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/Generation/ExceptionTypeTests.cs:
--------------------------------------------------------------------------------
1 | using Newtonsoft.Json;
2 | using NJsonSchema.NewtonsoftJson.Generation;
3 |
4 | namespace NJsonSchema.Tests.Generation
5 | {
6 | public class ExceptionTypeTests
7 | {
8 | public class MyException : Exception
9 | {
10 | [JsonProperty("foo")]
11 | public string Foo { get; set; }
12 | }
13 |
14 | [Fact]
15 | public async Task When_exception_schema_is_generated_then_special_properties_are_generated_and_JsonProperty_attribute_used()
16 | {
17 | // Arrange
18 | var schema = NewtonsoftJsonSchemaGenerator.FromType();
19 | var json = schema.ToJson();
20 |
21 | // Act
22 | var exceptionSchema = schema.InheritedSchema.ActualSchema;
23 |
24 | // Assert
25 | Assert.True(schema.ActualProperties.ContainsKey("foo"));
26 | Assert.True(exceptionSchema.ActualProperties.ContainsKey("InnerException"));
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/NJsonSchema/SchemaType.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | namespace NJsonSchema
10 | {
11 | /// Defines how to express the nullability of a property.
12 | public enum SchemaType
13 | {
14 | /// Uses oneOf with null schema and null type to express the nullability of a property (valid JSON Schema draft v4).
15 | JsonSchema,
16 |
17 | /// Uses required to express the nullability of a property (not valid in JSON Schema draft v4).
18 | Swagger2,
19 |
20 | /// Uses null handling of Open API 3.
21 | OpenApi3
22 | }
23 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Benchmark/Program.cs:
--------------------------------------------------------------------------------
1 | using System.Runtime.CompilerServices;
2 |
3 | namespace NJsonSchema.Benchmark
4 | {
5 | public static class Program
6 | {
7 | public static void Main(string[] args)
8 | {
9 | // RunCsharpBenchmark();
10 | BenchmarkDotNet.Running.BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run();
11 | }
12 |
13 | #pragma warning disable IDE0051
14 | private static void RunCsharpBenchmark()
15 | #pragma warning restore IDE0051
16 | {
17 | var benchmark = new CsharpGeneratorBenchmark();
18 | benchmark.Setup().GetAwaiter().GetResult();
19 | benchmark.GenerateFile();
20 | RunCode(benchmark);
21 | }
22 |
23 | [MethodImpl(MethodImplOptions.NoInlining)]
24 | private static void RunCode(CsharpGeneratorBenchmark benchmark)
25 | {
26 | for (int i = 0; i < 100; ++i)
27 | {
28 | benchmark.GenerateFile();
29 | }
30 | }
31 | }
32 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/AbstractSchemaTests.cs:
--------------------------------------------------------------------------------
1 | using NJsonSchema.CodeGeneration.CSharp;
2 | using NJsonSchema.CodeGeneration.CSharp.Tests;
3 | using NJsonSchema.NewtonsoftJson.Generation;
4 |
5 | namespace NJsonSchema.CodeGeneration.Tests.CSharp.Generation
6 | {
7 | public class AbstractSchemaTests
8 | {
9 | public abstract class AbstractClass
10 | {
11 | public string Foo { get; set; }
12 | }
13 |
14 | [Fact]
15 | public async Task When_class_is_abstract_then_is_abstract_CSharp_keyword_is_generated()
16 | {
17 | // Arrange
18 | var schema = NewtonsoftJsonSchemaGenerator.FromType();
19 |
20 | // Act
21 | var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings());
22 | var code = generator.GenerateFile("AbstractClass");
23 |
24 | // Assert
25 | await VerifyHelper.Verify(code);
26 | CSharpCompiler.AssertCompile(code);
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/NJsonSchema.Demo/Tests/minLength.json:
--------------------------------------------------------------------------------
1 | [
2 | {
3 | "description": "minLength validation",
4 | "schema": {"minLength": 2},
5 | "tests": [
6 | {
7 | "description": "longer is valid",
8 | "data": "foo",
9 | "valid": true
10 | },
11 | {
12 | "description": "exact length is valid",
13 | "data": "fo",
14 | "valid": true
15 | },
16 | {
17 | "description": "too short is invalid",
18 | "data": "f",
19 | "valid": false
20 | },
21 | {
22 | "description": "ignores non-strings",
23 | "data": 1,
24 | "valid": true
25 | },
26 | {
27 | "description": "one supplementary Unicode code point is not long enough",
28 | "data": "\uD83D\uDCA9",
29 | "valid": false
30 | }
31 | ]
32 | }
33 | ]
34 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/EnumTests.When_enum_is_string_then_generic_StringEnumConverter_is_used_nullable=False.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [System.Text.Json.Serialization.JsonPropertyName("size")]
15 | [System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.JsonStringEnumConverter))]
16 | public MyClassSize Size { get; set; }
17 |
18 | }
19 |
20 | public enum MyClassSize
21 | {
22 |
23 | [System.Runtime.Serialization.EnumMember(Value = @"small")]
24 | Small = 0,
25 |
26 |
27 | [System.Runtime.Serialization.EnumMember(Value = @"medium")]
28 | Medium = 1,
29 |
30 |
31 | [System.Runtime.Serialization.EnumMember(Value = @"large")]
32 | Large = 2,
33 |
34 |
35 | }
36 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/EnumTests.When_enum_is_string_then_generic_StringEnumConverter_is_used_nullable=True.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [System.Text.Json.Serialization.JsonPropertyName("size")]
15 | [System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.JsonStringEnumConverter))]
16 | public MyClassSize? Size { get; set; }
17 |
18 | }
19 |
20 | public enum MyClassSize
21 | {
22 |
23 | [System.Runtime.Serialization.EnumMember(Value = @"small")]
24 | Small = 0,
25 |
26 |
27 | [System.Runtime.Serialization.EnumMember(Value = @"medium")]
28 | Medium = 1,
29 |
30 |
31 | [System.Runtime.Serialization.EnumMember(Value = @"large")]
32 | Large = 2,
33 |
34 |
35 | }
36 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Demo/Tests/maxLength.json:
--------------------------------------------------------------------------------
1 | [
2 | {
3 | "description": "maxLength validation",
4 | "schema": {"maxLength": 2},
5 | "tests": [
6 | {
7 | "description": "shorter is valid",
8 | "data": "f",
9 | "valid": true
10 | },
11 | {
12 | "description": "exact length is valid",
13 | "data": "fo",
14 | "valid": true
15 | },
16 | {
17 | "description": "too long is invalid",
18 | "data": "foo",
19 | "valid": false
20 | },
21 | {
22 | "description": "ignores non-strings",
23 | "data": 100,
24 | "valid": true
25 | },
26 | {
27 | "description": "two supplementary Unicode code points is long enough",
28 | "data": "\uD83D\uDCA9\uD83D\uDCA9",
29 | "valid": true
30 | }
31 | ]
32 | }
33 | ]
34 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_definition_contains_datetime_and_use_system_text_json_then_converter_should_not_be_added.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [System.Text.Json.Serialization.JsonPropertyName("a")]
15 | public System.DateTimeOffset? A { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [System.Text.Json.Serialization.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_class_is_abstract_constructor_is_protected_for_record.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public abstract partial class AbstractAddress
12 | {
13 | [Newtonsoft.Json.JsonConstructor]
14 | protected AbstractAddress(string @city, string @streetName)
15 | {
16 | this.City = @city;
17 | this.StreetName = @streetName;
18 | }
19 |
20 | [Newtonsoft.Json.JsonProperty("city", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
21 | public string City { get; }
22 |
23 | [Newtonsoft.Json.JsonProperty("streetName", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
24 | public string StreetName { get; }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/Generation/DefaultSchemaNameGeneratorTests.cs:
--------------------------------------------------------------------------------
1 | using NJsonSchema.Generation;
2 |
3 | namespace NJsonSchema.Tests.Generation
4 | {
5 | public class DefaultSchemaNameGeneratorTests
6 | {
7 | [Theory]
8 | [InlineData(typeof(Dictionary), "DictionaryOfStringAndInteger")]
9 | [InlineData(typeof(Dictionary), "DictionaryOfBooleanAndLong")]
10 | [InlineData(typeof(Dictionary), "DictionaryOfDecimalAndShort")]
11 | [InlineData(typeof(Dictionary), "DictionaryOfGuidAndDateTime")]
12 | [InlineData(typeof(Dictionary), "DictionaryOfNullableDecimalAndNullableShort")]
13 | public void When_display_name_is_retrieved_then_string_is_correct(Type type, string expectedName)
14 | {
15 | // Act
16 | var generator = new DefaultSchemaNameGenerator();
17 | var name = generator.Generate(type);
18 |
19 | // Assert
20 | Assert.Equal(expectedName, name);
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/NumberTests.When_number_has_double_format_then_double_is_generated.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("amount", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public double Amount { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/NumberTests.When_number_has_float_format_then_float_is_generated.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("amount", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public float Amount { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/NumberTests.When_number_has_no_format_then_default_is_generated.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("amount", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public double Amount { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/NumberTests.When_number_type_setting_is_null_then_double_is_generated.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("amount", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public double? Amount { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.NewtonsoftJson.Tests/Generation/StructTests.cs:
--------------------------------------------------------------------------------
1 | using Newtonsoft.Json;
2 | using NJsonSchema.Annotations;
3 | using NJsonSchema.NewtonsoftJson.Generation;
4 | using Xunit;
5 |
6 | namespace NJsonSchema.NewtonsoftJson.Tests.Generation
7 | {
8 | public class StructTests
9 | {
10 | [JsonSchema("UserDefinedStruct")]
11 | public struct UserDefinedStruct
12 | {
13 | }
14 |
15 | public class UserDefinedClass
16 | {
17 | [JsonProperty]
18 | public readonly UserDefinedStruct NonNullableField;
19 |
20 | [JsonProperty]
21 | public readonly UserDefinedStruct? NullableField;
22 | }
23 |
24 | [Fact]
25 | public void Should_have_a_shared_struct_schema()
26 | {
27 | // Arrange
28 |
29 | // Act
30 | var schema = NewtonsoftJsonSchemaGenerator.FromType();
31 | var data = schema.ToJson();
32 |
33 | // Assert
34 | Assert.Single(schema.Definitions);
35 | }
36 | }
37 | }
--------------------------------------------------------------------------------
/src/NJsonSchema/SampleJsonDataGeneratorSettings.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | namespace NJsonSchema
10 | {
11 | /// Settings for generating sample json data.
12 | public class SampleJsonDataGeneratorSettings
13 | {
14 | /// Gets or sets a value indicating whether to generate optional properties (default: true).
15 | public bool GenerateOptionalProperties { get; set; } = true;
16 |
17 | /// Gets or sets a value indicating the max level of recursion the generator is allowed to perform (default: 3)
18 | public int MaxRecursionLevel { get; set; } = 3;
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_definition_is_named_Object_then_JObject_is_generated.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("foo", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public object Foo { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_property_has_same_name_as_class_then_it_is_renamed.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class Foo
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Foo", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public string Foo1 { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/NumberTests.When_number_has_decimal_format_then_decimal_is_generated.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("amount", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public decimal Amount { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_object_has_generic_name_then_it_is_transformed.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class FooOfBarOfInner
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("foo", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public double Foo { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/NumberTests.When_number_type_setting_is_whitespace_then_double_is_generated.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("amount", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public double Amount { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/DictionaryTests.When_dictionary_property_is_required_then_dictionary_instance_can_be_changed.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public enum PropertyName
12 | {
13 |
14 | Name = 0,
15 |
16 |
17 | Gender = 1,
18 |
19 |
20 | }
21 |
22 | public partial class MyClass
23 | {
24 |
25 | [Newtonsoft.Json.JsonProperty("EnumDictionary", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
26 | public Foo EnumDictionary { get; set; }
27 |
28 | [Newtonsoft.Json.JsonProperty("EnumInterfaceDictionary", Required = Newtonsoft.Json.Required.Always)]
29 | [System.ComponentModel.DataAnnotations.Required]
30 | public Foo EnumInterfaceDictionary { get; set; } = new Bar();
31 |
32 | }
33 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_name_contains_dash_then_it_is_converted_to_upper_case.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("foo-bar", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public string FooBar { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/Generation/Snapshots/SchemaGenerationTests.When_output_schema_contains_reference_then_schema_reference_path_is_human_readable.verified.txt:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json-schema.org/draft-04/schema#",
3 | "title": "Foo",
4 | "type": "object",
5 | "additionalProperties": false,
6 | "properties": {
7 | "Dictionary": {
8 | "type": [
9 | "null",
10 | "object"
11 | ],
12 | "additionalProperties": {
13 | "type": "string"
14 | }
15 | },
16 | "Bar": {
17 | "oneOf": [
18 | {
19 | "type": "null"
20 | },
21 | {
22 | "$ref": "#/definitions/Bar"
23 | }
24 | ]
25 | },
26 | "Time": {
27 | "type": "string",
28 | "format": "date-time"
29 | }
30 | },
31 | "definitions": {
32 | "Bar": {
33 | "type": "object",
34 | "additionalProperties": false,
35 | "properties": {
36 | "Name": {
37 | "type": [
38 | "null",
39 | "string"
40 | ]
41 | }
42 | }
43 | }
44 | }
45 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_definition_contains_datetime_converter_should_not_be_added.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("a", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public System.DateTimeOffset? A { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_property_name_is_created_by_custom_fun_then_parameter_name_is_correct_for_record.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class Address
12 | {
13 | [Newtonsoft.Json.JsonConstructor]
14 | public Address(string @myCustomCity, string @myCustomStreet)
15 | {
16 | this.MyCustomStreet = @myCustomStreet;
17 | this.MyCustomCity = @myCustomCity;
18 | }
19 |
20 | [Newtonsoft.Json.JsonProperty("Street", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
21 | public string MyCustomStreet { get; }
22 |
23 | [Newtonsoft.Json.JsonProperty("City", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
24 | public string MyCustomCity { get; }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/NumberTests.When_number_type_setting_is_defined_then_setting_type_is_generated.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("amount", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public customNumberType Amount { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration/CodeArtifactType.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | namespace NJsonSchema.CodeGeneration
10 | {
11 | /// The code artifact type.
12 | public enum CodeArtifactType
13 | {
14 | /// Undefined.
15 | Undefined,
16 |
17 | /// A class (e.g. C# or TypeScript class).
18 | Class,
19 |
20 | /// An interface (e.g. C# or TypeScript interface).
21 | Interface,
22 |
23 | /// An enum (e.g. C# or TypeScript interface).
24 | Enum,
25 |
26 | /// An internal function.
27 | Function
28 | }
29 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/NumberTests.When_number_decimal_type_setting_is_defined_then_setting_type_is_generated.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("amount", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public customDecimalType Amount { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/NumberTests.When_number_double_type_setting_is_defined_then_setting_type_is_generated.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("amount", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public customDoubleType Amount { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/NumberTests.When_number_float_type_setting_is_defined_then_setting_type_is_generated.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("amount", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public customFloatType Amount { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema/References/IJsonReferenceBase.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | using Newtonsoft.Json;
10 |
11 | namespace NJsonSchema.References
12 | {
13 | /// A JSON object which may reference other objects with $ref.
14 | public interface IJsonReferenceBase : IDocumentPathProvider
15 | {
16 | /// Gets or sets the type reference path ($ref).
17 | [JsonProperty("$ref", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
18 | string? ReferencePath { get; set; }
19 |
20 | /// Gets or sets the referenced object.
21 | [JsonIgnore]
22 | IJsonReference? Reference { get; set; }
23 | }
24 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/DefaultPropertyTests.When_property_has_boolean_default_it_is_reflected_in_the_poco.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace ns
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("boolWithDefault", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public bool BoolWithDefault { get; set; } = false;
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/DefaultPropertyTests.When_property_has_interger_default_it_is_reflected_in_the_poco.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace ns
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("intergerWithDefault", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public int IntergerWithDefault { get; set; } = 5;
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_property_has_not_supported_characters_then_they_are_removed.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("@odata.context", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public string OdataContext { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.Demo/Tests/required.json:
--------------------------------------------------------------------------------
1 | [
2 | {
3 | "description": "required validation",
4 | "schema": {
5 | "properties": {
6 | "foo": {},
7 | "bar": {}
8 | },
9 | "required": ["foo"]
10 | },
11 | "tests": [
12 | {
13 | "description": "present required property is valid",
14 | "data": {"foo": 1},
15 | "valid": true
16 | },
17 | {
18 | "description": "non-present required property is invalid",
19 | "data": {"bar": 1},
20 | "valid": false
21 | }
22 | ]
23 | },
24 | {
25 | "description": "required default validation",
26 | "schema": {
27 | "properties": {
28 | "foo": {}
29 | }
30 | },
31 | "tests": [
32 | {
33 | "description": "not required by default",
34 | "data": {},
35 | "valid": true
36 | }
37 | ]
38 | }
39 | ]
40 |
--------------------------------------------------------------------------------
/src/NJsonSchema.Tests/Generation/XmlDocsTests.cs:
--------------------------------------------------------------------------------
1 | using Newtonsoft.Json;
2 | using NJsonSchema.CodeGeneration.Tests;
3 | using NJsonSchema.NewtonsoftJson.Generation;
4 |
5 | namespace NJsonSchema.Tests.Generation
6 | {
7 | public class XmlDocsTests
8 | {
9 | /// Foobar.
10 | ///
11 | /// { "foo": "bar" }
12 | ///
13 | public abstract class AbstractClass
14 | {
15 | ///
16 | /// { "abc": "def" }
17 | ///
18 | public string Foo { get; set; }
19 |
20 | /// Bar.
21 | public string Bar { get; set; }
22 | }
23 |
24 | [Fact]
25 | public async Task When_example_xml_docs_is_defined_then_examples_can_be_defined()
26 | {
27 | // Act
28 | var schema = NewtonsoftJsonSchemaGenerator.FromType();
29 | var json = schema.ToJson(Formatting.None);
30 |
31 | // Assert
32 | await VerifyHelper.Verify(json);
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_schema_has_AdditionProperties_schema_then_JsonExtensionDataAttribute_is_generated.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class PersonAddress
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("Foo", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public string Foo { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_definition_contains_both_min_length_a_string_length_attribute_is_added_only_for_type_string.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("foo", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public double? Foo { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_definition_contains_pattern_but_type_is_not_string_a_regular_expression_should_not_be_added.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("foo", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public double? Foo { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/ValidationAttributesTests.When_int32_property_has_minimum_then_range_attribute_is_rendered_in_Swagger_mode.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class Message
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("value", Required = Newtonsoft.Json.Required.Always)]
15 | [System.ComponentModel.DataAnnotations.Range(10, int.MaxValue)]
16 | public int Value { get; set; }
17 |
18 | private System.Collections.Generic.IDictionary _additionalProperties;
19 |
20 | [Newtonsoft.Json.JsonExtensionData]
21 | public System.Collections.Generic.IDictionary AdditionalProperties
22 | {
23 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
24 | set { _additionalProperties = value; }
25 | }
26 |
27 | }
28 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/ValidationAttributesTests.When_int_property_has_maximum_then_range_attribute_is_rendered_in_Swagger_mode.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class Message
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("value", Required = Newtonsoft.Json.Required.Always)]
15 | [System.ComponentModel.DataAnnotations.Range(int.MinValue, 20)]
16 | public int Value { get; set; }
17 |
18 | private System.Collections.Generic.IDictionary _additionalProperties;
19 |
20 | [Newtonsoft.Json.JsonExtensionData]
21 | public System.Collections.Generic.IDictionary AdditionalProperties
22 | {
23 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
24 | set { _additionalProperties = value; }
25 | }
26 |
27 | }
28 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_definition_contains_maximum_a_range_attribute_is_not_added_for_anything_but_type_number_or_integer.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("foo", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public string Foo { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/ValidationAttributesTests.When_int64_property_has_minimum_then_range_attribute_is_rendered_in_Swagger_mode.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class Message
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("value", Required = Newtonsoft.Json.Required.Always)]
15 | [System.ComponentModel.DataAnnotations.Range(10L, long.MaxValue)]
16 | public long Value { get; set; }
17 |
18 | private System.Collections.Generic.IDictionary _additionalProperties;
19 |
20 | [Newtonsoft.Json.JsonExtensionData]
21 | public System.Collections.Generic.IDictionary AdditionalProperties
22 | {
23 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
24 | set { _additionalProperties = value; }
25 | }
26 |
27 | }
28 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Snapshots/InheritanceTests.When_interfaces_are_generated_with_inheritance_then_type_check_methods_are_generated.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | export interface Exception {
13 | Message: string;
14 | InnerException: Exception | undefined;
15 | Source: string | undefined;
16 | StackTrace: string | undefined;
17 | }
18 |
19 | export interface ExceptionBase extends Exception {
20 | Foo: string | undefined;
21 | kind: 'ExceptionBase';
22 | }
23 |
24 | export function isExceptionBase(object: any): object is ExceptionBase {
25 | return object && object['kind'] === 'ExceptionBase';
26 | }
27 |
28 | /** Foobar. */
29 | export interface MyException extends ExceptionBase {
30 | Bar: string | undefined;
31 | }
32 |
33 | export function isMyException(object: any): object is MyException {
34 | return object && object['kind'] === 'MyException';
35 | }
36 |
37 | export interface ExceptionContainer {
38 | Exception: ExceptionBase | undefined;
39 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp/Models/FileTemplateModel.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | namespace NJsonSchema.CodeGeneration.CSharp.Models
10 | {
11 | /// The CSharp file template model.
12 | public class FileTemplateModel
13 | {
14 | /// Gets or sets the namespace.
15 | public required string Namespace { get; set; }
16 |
17 | /// Gets or sets a value indicating whether the C#8 nullable reference types are enabled for this file.
18 | public bool GenerateNullableReferenceTypes { get; set; }
19 |
20 | /// Gets or sets the types code.
21 | public required string TypesCode { get; set; }
22 | }
23 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration/ITemplateFactory.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | namespace NJsonSchema.CodeGeneration
10 | {
11 | /// The interface of a code generator template factory.
12 | public interface ITemplateFactory
13 | {
14 | /// Creates a template for the given language, template name and template model.
15 | /// The language (i.e. 'CSharp' or 'TypeScript').
16 | /// The template name.
17 | /// The template model.
18 | /// The template.
19 | ITemplate CreateTemplate(string language, string template, object model);
20 | }
21 | }
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 Rico Suter
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of
6 | this software and associated documentation files (the "Software"), to deal in
7 | the Software without restriction, including without limitation the rights to
8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 | the Software, and to permit persons to whom the Software is furnished to do so,
10 | subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 |
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_name_contains_unallowed_characters_then_they_are_converted_to_valid_csharp_jsonPropertyName=FooBar3.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("foo.bar", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public string FooBar { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_name_contains_unallowed_characters_then_they_are_converted_to_valid_csharp_jsonPropertyName=FooBar4.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("foo=bar", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public string FooBar { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_name_contains_unallowed_characters_then_they_are_converted_to_valid_csharp_jsonPropertyName=Foobar1.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("foo@bar", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public string Foobar { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_name_contains_unallowed_characters_then_they_are_converted_to_valid_csharp_jsonPropertyName=Foobar2.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("foo$bar", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public string Foobar { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema/ITypeNameGenerator.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | namespace NJsonSchema
10 | {
11 | /// Generates the type name for a given .
12 | public interface ITypeNameGenerator
13 | {
14 | /// Generates the type name.
15 | /// The property.
16 | /// The type name hint (the property name or definition key).
17 | /// The reserved type names.
18 | /// The new name.
19 | string Generate(JsonSchema schema, string? typeNameHint, IEnumerable reservedTypeNames);
20 | }
21 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_name_contains_unallowed_characters_then_they_are_converted_to_valid_csharp_jsonPropertyName=Foo_bar.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("foo:bar", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public string Foo_bar { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_name_contains_unallowed_characters_then_they_are_converted_to_valid_csharp_jsonPropertyName=Foobars.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("foobars[]", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public string Foobars { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/DefaultPropertyTests.When_generating_CSharp_code_then_default_value_generates_expected_expression.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class Anonymous
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("someOptionalProperty", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public double SomeOptionalProperty { get; set; } = 123D;
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_name_contains_unallowed_characters_then_they_are_converted_to_valid_csharp_jsonPropertyName=FooStarbar.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("foo*bar", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public string FooStarbar { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/GeneralGeneratorTests.When_name_contains_unallowed_characters_then_they_are_converted_to_valid_csharp_jsonPropertyName=Fooplusbar.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("foo+bar", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public string Fooplusbar { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.TypeScript.Tests/Models/Person.cs:
--------------------------------------------------------------------------------
1 | using System.ComponentModel;
2 | using System.ComponentModel.DataAnnotations;
3 | using Newtonsoft.Json;
4 | using NJsonSchema.Annotations;
5 |
6 | namespace NJsonSchema.CodeGeneration.TypeScript.Tests.Models
7 | {
8 | public class Person
9 | {
10 | [Required]
11 | public string FirstName { get; set; }
12 |
13 | [JsonProperty("lastName")]
14 | public string LastName { get; set; }
15 |
16 | [ReadOnly(true)]
17 | public DateTime Birthday { get; set; }
18 |
19 | public TimeSpan TimeSpan { get; set; }
20 |
21 | public TimeSpan? TimeSpanOrNull { get; set; }
22 |
23 | public Gender Gender { get; set; }
24 |
25 | public Gender? GenderOrNull { get; set; }
26 |
27 | [JsonProperty("address", Required = Required.Always)]
28 | public Address Address { get; set; }
29 |
30 | [CanBeNull]
31 | public Address AddressOrNull { get; set; }
32 |
33 | public List Array { get; set; }
34 |
35 | public Dictionary Dictionary { get; set; }
36 | }
37 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/DefaultPropertyTests.When_property_has_boolean_default_and_default_value_generation_is_disabled_then_default_value_is_not_generated.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace ns
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class MyClass
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("boolWithDefault", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
15 | public bool BoolWithDefault { get; set; }
16 |
17 | private System.Collections.Generic.IDictionary _additionalProperties;
18 |
19 | [Newtonsoft.Json.JsonExtensionData]
20 | public System.Collections.Generic.IDictionary AdditionalProperties
21 | {
22 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
23 | set { _additionalProperties = value; }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/ValidationAttributesTests.When_integer_property_has_minimum_and_maximum_that_are_too_large_or_small_for_int64.verified.txt:
--------------------------------------------------------------------------------
1 | //----------------------
2 | //
3 | //
4 | //----------------------
5 |
6 |
7 | namespace MyNamespace
8 | {
9 | #pragma warning disable // Disable all warnings
10 |
11 | public partial class Message
12 | {
13 |
14 | [Newtonsoft.Json.JsonProperty("value", Required = Newtonsoft.Json.Required.Always)]
15 | [System.ComponentModel.DataAnnotations.Range(-9223372036854775808L, 9223372036854775807L)]
16 | public long Value { get; set; }
17 |
18 | private System.Collections.Generic.IDictionary _additionalProperties;
19 |
20 | [Newtonsoft.Json.JsonExtensionData]
21 | public System.Collections.Generic.IDictionary AdditionalProperties
22 | {
23 | get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
24 | set { _additionalProperties = value; }
25 | }
26 |
27 | }
28 | }
--------------------------------------------------------------------------------
/src/NJsonSchema/References/IJsonReference.cs:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------
2 | //
3 | // Copyright (c) Rico Suter. All rights reserved.
4 | //
5 | // https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md
6 | // Rico Suter, mail@rsuter.com
7 | //-----------------------------------------------------------------------
8 |
9 | using Newtonsoft.Json;
10 |
11 | namespace NJsonSchema.References
12 | {
13 | /// A JSON object which may reference other objects with $ref.
14 | /// The methods should be implemented explicitly to hide them from the API.
15 | public interface IJsonReference : IJsonReferenceBase
16 | {
17 | /// Gets the actual referenced object, either this or the reference object.
18 | [JsonIgnore]
19 | IJsonReference ActualObject { get; }
20 |
21 | /// Gets the parent object which may be the root.
22 | [JsonIgnore]
23 | object? PossibleRoot { get; }
24 | }
25 | }
--------------------------------------------------------------------------------