public void ThenResponseShouldContainAsField(string response, string field)
        {
            string temp = string.Empty;

            Newtonsoft.Json.Linq.JToken token = JObject.Parse(restApi().GetResponseData.Content);

            var name = token.SelectTokens(response);

            Console.WriteLine(name);
            foreach (var respons in name)
            {
                temp = JsonConvert.DeserializeObject(respons.ToString(Formatting.None), new JsonSerializerSettings {
                    DateParseHandling = DateParseHandling.None
                }).ToString();
                if (temp == field)
                {
                    Assert.AreEqual(field, temp);
                }
                else
                {
                    Assert.Fail("Value not matched. Actual Value is " + temp);
                }
            }
        }
Пример #2
0
        private static T Populate <T>(Newtonsoft.Json.Linq.JToken jsonToken, T target, JsonSerializer serializer)
        {
            // Si on exploite "target" sous sa forme typée alors le SetValue n'impacte pas l'instance de notre objet
            object targetObject = (object)target;

            Type targetType = target.GetType();

            Dictionary <List <string>, MemberInfo> lstMembers = new Dictionary <List <string>, MemberInfo>();

            List <MemberInfo> memberInfosAtCheck = new List <MemberInfo>();

            memberInfosAtCheck.AddRange(targetType.GetProperties());
            memberInfosAtCheck.AddRange(targetType.GetFields());

            foreach (MemberInfo memberInfo in memberInfosAtCheck)
            {
                JsonPropertyAttribute  propAtt   = null;
                JsonConverterAttribute convAtt   = null;
                JsonPathAttribute      pathAtt   = null;
                List <string>          jsonPaths = null;

                ToolsJsonNet.GetJsonAttributeAndPathsByMemberInfo(memberInfo, out propAtt, out pathAtt, out convAtt, out jsonPaths);

                #region Application du Converter

                JsonConverter conv = null;
                if (serializer != null && convAtt != null)
                {
                    conv = (JsonConverter)Activator.CreateInstance(convAtt.ConverterType);

                    serializer.Converters.Add(conv);
                }

                #endregion

                lstMembers.Add(jsonPaths, memberInfo);
            }

            foreach (List <string> memberPaths in lstMembers.Keys)
            {
                JToken jToken = null;
                foreach (string memberPath in memberPaths)
                {
                    IEnumerator <JToken> jTokens = jsonToken.SelectTokens(memberPath).GetEnumerator();

                    // Récupération du 1er element de la liste (si existant)
                    if (jTokens.MoveNext())
                    {
                        jToken = jTokens.Current;
                    }

                    // On exploite le 1er path qui match avec notre Json
                    if (jToken != null)
                    {
                        break;
                    }
                }

                if (jToken != null && jToken.Type != JTokenType.Null)
                {
                    if (lstMembers[memberPaths] is PropertyInfo)
                    {
                        PropertyInfo propertyInfo = (PropertyInfo)lstMembers[memberPaths];

                        object value = ExtractValue(serializer, jToken, propertyInfo.PropertyType);

                        propertyInfo.SetValue(targetObject, value, null);
                    }
                    else
                    {
                        FieldInfo fieldInfo = (FieldInfo)lstMembers[memberPaths];

                        object value = ExtractValue(serializer, jToken, fieldInfo.FieldType);

                        fieldInfo.SetValue(targetObject, value);
                    }
                }
            }

            return((T)targetObject);
        }
Пример #3
0
        public void MakeOpenApiDocumentNotExternal()
        {
            Console.WriteLine("Cleaning the openAPI outputs");
            var files = System.IO.Directory.EnumerateFiles(_directory + "\\output", "*.openapi.json", SearchOption.AllDirectories).ToArray();

            foreach (string file in files)
            {
                string igJsonText = System.IO.File.ReadAllText(file);
                var    js         = new JsonSerializer();
                js.Formatting = Newtonsoft.Json.Formatting.Indented;
                var igJson = js.Deserialize(new JsonTextReader(new StringReader(igJsonText)));
                Newtonsoft.Json.Linq.JToken t = igJson as Newtonsoft.Json.Linq.JToken;

                // remove all of the application/fhir+xml content
                var schemasXml = t.SelectTokens("..application/fhir+xml");
                foreach (var schemaXml in schemasXml.ToArray())
                {
                    schemaXml.Parent.Remove();
                }

                Dictionary <string, JToken> convertedTypes = new Dictionary <string, JToken>();

                var schemas = t.SelectTokens("..schema.$ref");
                foreach (JValue schema in schemas)
                {
                    string value = schema.Value as string;
                    if (!value.StartsWith("#") && value.Contains("#"))
                    {
                        if (!convertedTypes.ContainsKey(value.Replace("https://hl7.org/fhir/STU3/fhir.schema.json", "")))
                        {
                            convertedTypes.Add(value.Replace("https://hl7.org/fhir/STU3/fhir.schema.json", ""), null);
                        }
                        if (!convertedTypes.ContainsKey(value.Replace("https://hl7.org/fhir/R4/fhir.schema.json", "")))
                        {
                            convertedTypes.Add(value.Replace("https://hl7.org/fhir/R4/fhir.schema.json", ""), null);
                        }
                        schema.Value = value.Substring(value.IndexOf("#"));
                    }
                }

                JObject definitions = t["definitions"] as JObject;
                if (definitions != null)
                {
                    definitions.Remove();
                }

                string fhirJsonSchemaText = System.IO.File.ReadAllText(@"C:\temp\fhir.schema.json");
                var    fhirJsonSchema     = js.Deserialize(new JsonTextReader(new StringReader(fhirJsonSchemaText))) as JToken;
                while (convertedTypes.Where(ct => ct.Value == null).Count() > 0)
                {
                    foreach (var value in convertedTypes.Keys.ToArray())
                    {
                        var search    = value.Substring(2).Replace("/", ".");
                        var typeToken = fhirJsonSchema.SelectToken(search).Parent;
                        // Console.WriteLine($"{value}");
                        // Console.WriteLine($"{search}");
                        // Console.WriteLine($"{typeToken}");
                        convertedTypes[value] = typeToken;

                        // Scan the token for nested types
                        if (search == "definitions.ResourceList")
                        {
                            foreach (JValue ct in typeToken.SelectTokens("..$ref").ToArray())
                            {
                                if (ct.Value is string s)
                                {
                                    if (!convertedTypes.ContainsKey(s))
                                    {
                                        System.Diagnostics.Trace.WriteLine($"Removing unused type {s}");
                                        ct.Parent.Parent.Remove();
                                    }
                                }
                            }

                            continue;
                        }
                        foreach (JValue ct in typeToken.SelectTokens("..$ref"))
                        {
                            if (ct.Value is string s)
                            {
                                if (!convertedTypes.ContainsKey(s))
                                {
                                    // System.Diagnostics.Trace.WriteLine($"{s}");
                                    convertedTypes.Add(s, null);
                                }
                            }
                        }

                        // and remove all the extension properties
                        foreach (JToken ct in typeToken.SelectTokens("..properties").Children().ToArray())
                        {
                            // System.Diagnostics.Trace.WriteLine($"{ct.Path.Replace(ct.Parent.Path+".", "")}");
                            if (value == "#/definitions/BackboneElement")
                            {
                                ct.Parent.Parent.Parent.Remove();
                                continue;
                            }
                            if (ct.Path.Replace(ct.Parent.Path + ".", "").StartsWith("_"))
                            {
                                ct.Remove();
                            }
                            else if (ct.Path.Replace(ct.Parent.Path + ".", "") == "modifierExtension")
                            {
                                ct.Remove();
                            }
                            else if (ct.Path.Replace(ct.Parent.Path + ".", "") == "extension")
                            {
                                ct.Remove();
                            }
                            else if (value == "#/definitions/Element")
                            {
                                ct.Remove();
                            }
                        }
                    }
                }
                t.Children().Last().AddAfterSelf(new JProperty("definitions", new JObject(convertedTypes.Values.ToArray())));

                //foreach (var pair in fileResources)
                //{
                //    FileInfo fi = new FileInfo(pair.Key);
                //    string htmlName = $"{pair.Value.ResourceType.GetLiteral()}-{fi.Name.Substring(0, fi.Name.Length - fi.Extension.Length)}.html";
                //    htmlName = htmlName.Replace(pair.Value.ResourceType.GetLiteral().ToLower(), "");
                //    htmlName = htmlName.Replace("--", "-");
                //    string content = $"{{\r\n  \"{pair.Value.ResourceType.GetLiteral()}/{pair.Value.Id}\": {{\r\n" +
                //        $"    \"source\" : \"{fi.Name}\",\r\n" +
                //        $"    \"base\" : \"{htmlName}\"\r\n" +
                //        $"  }}\r\n}}";
                //    var newNode = Newtonsoft.Json.Linq.JObject.Parse(content) as JToken;
                //    igJsonResources.Add(newNode.Children().First());
                //}
                var sb = new StringBuilder();
                js.Serialize(new JsonTextWriter(new StringWriter(sb)), igJson);

                if (sb.ToString() != igJsonText)
                {
                    System.IO.File.WriteAllText(file + ".json", sb.ToString());
                }
                // Console.WriteLine(sb.ToString());
            }
        }