private ObjectTypeConstructor BuildSerializationConstructor() { bool ownsDiscriminatorProperty = false; List <Parameter> serializationConstructorParameters = new List <Parameter>(); List <ObjectPropertyInitializer> serializationInitializers = new List <ObjectPropertyInitializer>(); ObjectTypeConstructor? baseSerializationCtor = null; if (Inherits != null && !Inherits.IsFrameworkType && Inherits.Implementation is ObjectType objectType) { baseSerializationCtor = objectType.Constructors.Last(); serializationConstructorParameters.AddRange(baseSerializationCtor.Parameters); } foreach (var property in Properties) { var deserializationParameter = new Parameter( property.Declaration.Name.ToVariableName(), property.Description, property.Declaration.Type, null, false ); ownsDiscriminatorProperty |= property == Discriminator?.Property; serializationConstructorParameters.Add(deserializationParameter); serializationInitializers.Add(new ObjectPropertyInitializer(property, deserializationParameter, GetPropertyDefaultValue(property))); } if (Discriminator != null) { // Add discriminator initializer to constructor at every level of hierarchy if (!ownsDiscriminatorProperty && baseSerializationCtor != null) { var discriminatorParameter = baseSerializationCtor.FindParameterByInitializedProperty(Discriminator.Property); Debug.Assert(discriminatorParameter != null); serializationInitializers.Add(new ObjectPropertyInitializer(Discriminator.Property, discriminatorParameter, BuilderHelpers.StringConstant(Discriminator.Value))); } } return(new ObjectTypeConstructor( BuilderHelpers.CreateMemberDeclaration(Type.Name, Type, "internal", null, _typeFactory), serializationConstructorParameters.ToArray(), serializationInitializers.ToArray(), baseSerializationCtor )); }
private List <EnumTypeValue> BuildValues() { var values = new List <EnumTypeValue>(); foreach (var c in _choices) { var name = BuilderHelpers.DisambiguateName(Type, c.CSharpName()); var memberMapping = _typeMapping?.GetForMember(name); values.Add(new EnumTypeValue( BuilderHelpers.CreateMemberDeclaration(name, Type, "public", memberMapping?.ExistingMember, _context.TypeFactory), CreateDescription(c), BuilderHelpers.ParseConstant(c.Value, BaseType))); } return(values); }
private IEnumerable <ObjectTypeProperty> BuildProperties() { // WORKAROUND: https://github.com/Azure/autorest.modelerfour/issues/261 var existingProperties = EnumerateHierarchy() .Skip(1) .SelectMany(type => type.Properties) .Select(p => p.SchemaProperty?.Language.Default.Name) .ToHashSet(); foreach (var objectSchema in GetCombinedSchemas()) { foreach (Property property in objectSchema.Properties !) { if (existingProperties.Contains(property.Language.Default.Name)) { // WORKAROUND: https://github.com/Azure/autorest.modelerfour/issues/261 continue; } var name = BuilderHelpers.DisambiguateName(Type, property.CSharpName()); SourceMemberMapping?memberMapping = _sourceTypeMapping?.GetForMember(name); bool isNotInput = (objectSchema.IsOutput || objectSchema.IsException) && !objectSchema.IsInput; bool isReadOnly = IsStruct || isNotInput || property.ReadOnly == true || // Required properties of input objects should be readonly (property.Required == true && objectSchema.IsInputOnly); if (property.IsDiscriminator == true) { // Discriminator properties should be writeable isReadOnly = false; } CSharpType type = _typeFactory.CreateType( property.Schema, property.IsNullable()); if (!_objectSchema.IsInput) { type = TypeFactory.GetOutputType(type); } var accessibility = property.IsDiscriminator == true ? "internal" : "public"; CSharpType?initializeWithType = memberMapping?.Initialize == true?TypeFactory.GetImplementationType(type) : null; yield return(new ObjectTypeProperty( BuilderHelpers.CreateMemberDeclaration(name, type, accessibility, memberMapping?.ExistingMember, _typeFactory), BuilderHelpers.EscapeXmlDescription(property.Language.Default.Description), isReadOnly, property, initializeWithType, memberMapping?.EmptyAsUndefined ?? false)); } } if (AdditionalPropertiesProperty is ObjectTypeProperty additionalPropertiesProperty) { yield return(additionalPropertiesProperty); } }
private ObjectTypeConstructor BuildInitializationConstructor() { List <Parameter> defaultCtorParameters = new List <Parameter>(); List <ObjectPropertyInitializer> defaultCtorInitializers = new List <ObjectPropertyInitializer>(); ObjectTypeConstructor?baseCtor = null; if (Inherits != null && !Inherits.IsFrameworkType && Inherits.Implementation is ObjectType objectType) { baseCtor = objectType.Constructors.First(); defaultCtorParameters.AddRange(baseCtor.Parameters); } foreach (var property in Properties) { // Only required properties that are not discriminators go into default ctor if (property == Discriminator?.Property) { continue; } ReferenceOrConstant?initializationValue; if (property.SchemaProperty?.Schema is ConstantSchema constantSchema) { // Turn constants into initializers initializationValue = constantSchema.Value.Value != null? BuilderHelpers.ParseConstant(constantSchema.Value.Value, property.Declaration.Type) : Constant.NewInstanceOf(property.Declaration.Type); } else if (IsStruct || property.SchemaProperty?.Required == true) { // For structs all properties become required Constant?defaultParameterValue = null; if (property.SchemaProperty?.ClientDefaultValue is object defaultValueObject) { defaultParameterValue = BuilderHelpers.ParseConstant(defaultValueObject, property.Declaration.Type); } var defaultCtorParameter = new Parameter( property.Declaration.Name.ToVariableName(), property.Description, TypeFactory.GetInputType(property.Declaration.Type), defaultParameterValue, true ); defaultCtorParameters.Add(defaultCtorParameter); initializationValue = defaultCtorParameter; } else { initializationValue = GetPropertyDefaultValue(property); } if (initializationValue != null) { defaultCtorInitializers.Add(new ObjectPropertyInitializer(property, initializationValue.Value)); } } if (Discriminator != null) { defaultCtorInitializers.Add(new ObjectPropertyInitializer(Discriminator.Property, BuilderHelpers.StringConstant(Discriminator.Value))); } return(new ObjectTypeConstructor( BuilderHelpers.CreateMemberDeclaration( Type.Name, Type, // inputs have public ctor by default _objectSchema.IsInput ? "public" : "internal", null, _typeFactory), defaultCtorParameters.ToArray(), defaultCtorInitializers.ToArray(), baseCtor)); }