/// <summary> /// Constructs blueprint of the given <paramref name="type"/>. /// </summary> /// <param name="type">Type for which mapper being generated.</param> /// <param name="serializedName">Serialized name to be used.</param> /// <param name="parameter">Parameter of the composite type to construct the parameter constraints.</param> /// <param name="expandComposite">Expand composite type if <c>true</c> otherwise specify class_name in the mapper.</param> /// <returns>Mapper for the <paramref name="type"/> as string.</returns> /// <exception cref="ArgumentNullException">Thrown when a required parameter is null.</exception> /// <example> /// One of the example of the mapper is /// { /// required: false, /// serialized_name: 'Fish', /// type: { /// name: 'Composite', /// polymorphic_discriminator: 'fishtype', /// uber_parent: 'Fish', /// class_name: 'Fish', /// model_properties: { /// species: { /// required: false, /// serialized_name: 'species', /// type: { /// name: 'String' /// } /// }, /// length: { /// required: true, /// serialized_name: 'length', /// type: { /// name: 'Double' /// } /// }, /// siblings: { /// required: false, /// serialized_name: 'siblings', /// type: { /// name: 'Sequence', /// element: { /// required: false, /// serialized_name: 'FishElementType', /// type: { /// name: 'Composite', /// polymorphic_discriminator: 'fishtype', /// uber_parent: 'Fish', /// class_name: 'Fish' /// } /// } /// } /// } /// } /// } /// } /// </example> public static string ConstructMapper(this IModelType type, string serializedName, IVariable parameter, bool expandComposite) { if (type == null) { throw new ArgumentNullException(nameof(type)); } var builder = new IndentedStringBuilder(" "); CompositeType composite = type as CompositeType; SequenceType sequence = type as SequenceType; DictionaryType dictionary = type as DictionaryType; PrimaryType primary = type as PrimaryType; EnumType enumType = type as EnumType; if (enumType != null && enumType.ModelAsString) { primary = New<PrimaryType>(KnownPrimaryType.String); } builder.AppendLine("").Indent(); builder.AppendLine(type.AddMetaData(serializedName, parameter)); if (primary != null) { builder.AppendLine(primary.ContructMapperForPrimaryType()); } else if (enumType != null && enumType.Name != null) { builder.AppendLine(((EnumTypeRb)enumType).ContructMapperForEnumType()); } else if (sequence != null) { builder.AppendLine(sequence.ContructMapperForSequenceType()); } else if (dictionary != null) { builder.AppendLine(dictionary.ContructMapperForDictionaryType()); } else if (composite != null) { builder.AppendLine(composite.ContructMapperForCompositeType(expandComposite)); } else { throw new NotImplementedException(string.Format(CultureInfo.InvariantCulture, "{0} is not a supported Type.", type)); } return builder.ToString(); }
/// <summary> /// Adds metadata to the given <paramref name="type"/>. /// </summary> /// <param name="type">Type for which metadata being generated.</param> /// <param name="serializedName">Serialized name to be used.</param> /// <param name="parameter">Parameter of the composite type to construct the parameter constraints.</param> /// <returns>Metadata as string.</returns> /// <exception cref="ArgumentNullException">Thrown when a required parameter is null.</exception> /// <example> /// The below example shows possible mapper string for IParameter for IModelType. /// required: true | false, -- whether this property is required or not /// read_only: true | false, -- whether this property is read only or not. Default is false /// is_constant: true | false, -- whether this property is constant or not. Default is false /// serialized_name: 'name' -- serialized name of the property if provided /// default_value: 'value' -- default value of the property if provided /// constraints: { -- constraints of the property /// key: value, -- constraint name and value if any /// ***: ***** /// } /// </example> private static string AddMetaData(this IModelType type, string serializedName, IVariable parameter) { if (type == null) { throw new ArgumentNullException(nameof(type)); } IndentedStringBuilder builder = new IndentedStringBuilder(" "); Dictionary<Constraint, string> constraints = null; string defaultValue = null; bool isRequired = false; bool isConstant = false; bool isReadOnly = false; var property = parameter as Property; if (property != null) { isReadOnly = property.IsReadOnly; } if (parameter != null) { defaultValue = parameter.DefaultValue; isRequired = parameter.IsRequired; isConstant = parameter.IsConstant; constraints = parameter.Constraints; } CompositeType composite = type as CompositeType; if (composite != null && composite.ContainsConstantProperties && isRequired) { defaultValue = "{}"; } if (isRequired) { builder.AppendLine("required: true,"); } else { builder.AppendLine("required: false,"); } if (isReadOnly) { builder.AppendLine("read_only: true,"); } if (isConstant) { builder.AppendLine("is_constant: true,"); } if (serializedName != null) { builder.AppendLine("serialized_name: '{0}',", serializedName); } if (defaultValue != null) { builder.AppendLine("default_value: {0},", defaultValue); } if (constraints != null && constraints.Count > 0) { builder.AppendLine("constraints: {").Indent(); var keys = constraints.Keys.ToList<Constraint>(); for (int j = 0; j < keys.Count; j++) { var constraintValue = constraints[keys[j]]; if (keys[j] == Constraint.Pattern) { constraintValue = string.Format(CultureInfo.InvariantCulture, "'{0}'", constraintValue); } if (j != keys.Count - 1) { builder.AppendLine("{0}: {1},", keys[j], constraintValue); } else { builder.AppendLine("{0}: {1}", keys[j], constraintValue); } } builder.Outdent() .AppendLine("},"); } return builder.ToString(); }
public static void PopulateParameter(IVariable parameter, SwaggerObject swaggerObject) { if (swaggerObject == null) { throw new ArgumentNullException("swaggerObject"); } if (parameter == null) { throw new ArgumentNullException("parameter"); } parameter.IsRequired = swaggerObject.IsRequired; parameter.DefaultValue = swaggerObject.Default; if (IsSwaggerObjectConstant(swaggerObject)) { parameter.DefaultValue = swaggerObject.Enum[0]; parameter.IsConstant = true; } parameter.Documentation = swaggerObject.Description; parameter.CollectionFormat = swaggerObject.CollectionFormat; // tag the paramter with all the extensions from the swagger object parameter.Extensions.AddRange(swaggerObject.Extensions); SetConstraints(parameter.Constraints, swaggerObject); }