Ejemplo n.º 1
0
        /// <summary>
        /// Builds a JSON schema based on the uploaded XSD.
        /// </summary>
        /// <remarks>
        /// This operation is using the new data modelling library.
        /// </remarks>
        /// <param name="org">Organization owning the repository identified by it's short name.</param>
        /// <param name="repository">Repository name to search for schema files.</param>
        /// <param name="developer">Developers short name</param>
        /// <param name="fileName">The name of the file being uploaded.</param>
        /// <param name="xsdStream">Stream representing the XSD.</param>
        public async Task <string> BuildSchemaFromXsd(
            string org, string repository, string developer, string fileName, Stream xsdStream)
        {
            var altinnAppGitRepository = _altinnGitRepositoryFactory.GetAltinnAppGitRepository(
                org, repository, developer);

            MemoryStream xsdMemoryStream = new MemoryStream();

            xsdStream.CopyTo(xsdMemoryStream);

            AltinnRepositoryType altinnRepositoryType = await altinnAppGitRepository.GetRepositoryType();

            if (altinnRepositoryType == AltinnRepositoryType.Datamodels)
            {
                xsdMemoryStream.Position = 0;
                Json.Schema.JsonSchema jsonSchema = GenerateJsonSchemaFromXsd(xsdMemoryStream);
                var jsonContent = SerializeJson(jsonSchema);

                await altinnAppGitRepository.WriteTextByRelativePathAsync(
                    Path.ChangeExtension(fileName, "schema.json"), jsonContent, true);

                return(jsonContent);
            }

            /* From here repository is assumed to be for an app. Validate with a Directory.Exist check? */

            string filePath = Path.Combine(altinnAppGitRepository.GetRelativeModelFolder(), fileName);

            DatamodellingPreference datamodellingPreference =
                await altinnAppGitRepository.GetDatamodellingPreference();

            switch (datamodellingPreference)
            {
            case DatamodellingPreference.JsonSchema:
                /* Using the NEW model processing. */

                xsdMemoryStream.Position = 0;
                await SaveOriginalXsd(org, repository, developer, filePath, xsdMemoryStream);

                xsdMemoryStream.Position = 0;
                string jsonContent = await ProcessNewXsd(altinnAppGitRepository, xsdMemoryStream, filePath);

                return(jsonContent);

            case DatamodellingPreference.Xsd:
            default:
                /* Using the OLD model processing. */

                xsdMemoryStream.Position = 0;
                Manatee.Json.Schema.JsonSchema schemaJsonSchema = GenerateJsonSchema(xsdMemoryStream);
                string jsonSerialized = SerializeJson(schemaJsonSchema);
                await UpdateAllAppModelFiles(org, repository, developer, Path.ChangeExtension(filePath, "schema.json"), jsonSerialized);

                return(jsonSerialized);
            }
        }
Ejemplo n.º 2
0
 private static string SerializeJson(Json.Schema.JsonSchema jsonSchema)
 {
     return(JsonSerializer.Serialize(
                jsonSchema,
                new JsonSerializerOptions
     {
         Encoder = JavaScriptEncoder.Create(UnicodeRanges.BasicLatin, UnicodeRanges.Latin1Supplement),
         WriteIndented = true
     }));
 }
Ejemplo n.º 3
0
        private Json.Schema.JsonSchema GenerateJsonSchemaFromXsd(Stream xsdStream)
        {
            XmlSchema originalXsd = XmlSchema.Read(xsdStream, (_, _) => { });

            var xsdToJsonConverter = new XmlSchemaToJsonSchemaConverter();

            Json.Schema.JsonSchema convertedJsonSchema = xsdToJsonConverter.Convert(originalXsd);

            return(convertedJsonSchema);
        }
Ejemplo n.º 4
0
        private async Task <string> ProcessNewXsd(
            AltinnAppGitRepository altinnAppGitRepository,
            MemoryStream xsdMemoryStream,
            string filePath)
        {
            Json.Schema.JsonSchema jsonSchema = GenerateJsonSchemaFromXsd(xsdMemoryStream);
            var jsonContent = SerializeJson(jsonSchema);

            await altinnAppGitRepository.WriteTextByRelativePathAsync(
                Path.ChangeExtension(filePath, "schema.json"), jsonContent, true);

            var jsonSchemaConverterStrategy = JsonSchemaConverterStrategyFactory.SelectStrategy(jsonSchema);
            var metamodelConverter          =
                new JsonSchemaToMetamodelConverter(jsonSchemaConverterStrategy.GetAnalyzer());
            var schemaName    = GetSchemaName(filePath);
            var modelMetadata = metamodelConverter.Convert(schemaName, jsonContent);
            await altinnAppGitRepository.UpdateModelMetadata(modelMetadata, schemaName);

            await UpdateCSharpClasses(altinnAppGitRepository, modelMetadata, schemaName);

            await UpdateApplicationMetadata(altinnAppGitRepository, schemaName, schemaName);

            return(jsonContent);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Creates a new <see cref="IfKeyword"/>.
 /// </summary>
 /// <param name="value">The schema to match.</param>
 public IfKeyword(JsonSchema value)
 {
     Schema = value ?? throw new ArgumentNullException(nameof(value));
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Creates a new <see cref="IfKeyword"/>.
 /// </summary>
 /// <param name="value">The schema to match.</param>
 public IfKeyword(JsonSchema value)
 {
     Schema = value;
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Provides validation for the keyword.
        /// </summary>
        /// <param name="context">Contextual details for the validation process.</param>
        public void Validate(ValidationContext context)
        {
            var parts    = Reference.OriginalString.Split(new [] { '#' }, StringSplitOptions.None);
            var baseUri  = parts[0];
            var fragment = parts.Length > 1 ? parts[1] : null;

            Uri        newUri;
            JsonSchema baseSchema = null;

            if (!string.IsNullOrEmpty(baseUri))
            {
                if (Uri.TryCreate(baseUri, UriKind.Absolute, out newUri))
                {
                    baseSchema = context.Options.SchemaRegistry.Get(newUri);
                }
                else if (context.CurrentUri != null)
                {
                    var uriFolder = context.CurrentUri.OriginalString.EndsWith("/")
                                                ? context.CurrentUri
                                                : context.CurrentUri.GetParentUri();
                    newUri = uriFolder;
                    var newBaseUri = new Uri(uriFolder, baseUri);
                    if (!string.IsNullOrEmpty(fragment))
                    {
                        newUri = newBaseUri;
                    }
                    baseSchema = context.Options.SchemaRegistry.Get(newBaseUri);
                }
            }
            else
            {
                baseSchema = context.SchemaRoot;
                newUri     = context.CurrentUri;
            }

            JsonSchema schema;

            if (!string.IsNullOrEmpty(fragment) && AnchorKeyword.AnchorPattern.IsMatch(fragment))
            {
                schema = context.Options.SchemaRegistry.Get(newUri, fragment);
            }
            else
            {
                if (baseSchema == null)
                {
                    context.IsValid = false;
                    context.Message = $"Could not resolve base URI `{baseUri}`";
                    return;
                }

                if (!string.IsNullOrEmpty(fragment))
                {
                    fragment = $"#{fragment}";
                    if (!JsonPointer.TryParse(fragment, out var pointer))
                    {
                        context.IsValid = false;
                        context.Message = $"Could not parse pointer `{fragment}`";
                        return;
                    }
                    (schema, newUri) = baseSchema.FindSubschema(pointer, newUri);
                }
                else
                {
                    schema = baseSchema;
                }
            }

            if (schema == null)
            {
                context.IsValid = false;
                context.Message = $"Could not resolve reference `{Reference}`";
                return;
            }

            var subContext = ValidationContext.From(context, newUri: newUri);

            if (!ReferenceEquals(baseSchema, context.SchemaRoot))
            {
                subContext.SchemaRoot = baseSchema;
            }
            schema.ValidateSubschema(subContext);
            context.NestedContexts.Add(subContext);
            context.ConsolidateAnnotations();
            context.IsValid = subContext.IsValid;
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Creates a new <see cref="ContentSchemaKeyword"/>.
 /// </summary>
 /// <param name="value">The schema against which to validate the content.</param>
 public ContentSchemaKeyword(JsonSchema value)
 {
     Schema = value;
 }
 /// <summary>
 /// Creates a new <see cref="AdditionalItemsKeyword"/>.
 /// </summary>
 /// <param name="value">The keyword's schema.</param>
 public AdditionalItemsKeyword(JsonSchema value)
 {
     Schema = value;
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Creates a new <see cref="ItemsKeyword"/>.
 /// </summary>
 /// <param name="value">The schema for the "single schema" form.</param>
 public ItemsKeyword(JsonSchema value)
 {
     SingleSchema = value;
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Creates a new <see cref="UnevaluatedPropertiesKeyword"/>.
 /// </summary>
 /// <param name="value"></param>
 public UnevaluatedPropertiesKeyword(JsonSchema value)
 {
     Schema = value;
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Creates a new <see cref="ElseKeyword"/>.
 /// </summary>
 /// <param name="value">The schema to match.</param>
 public ElseKeyword(JsonSchema value)
 {
     Schema = value;
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Creates a schema dependency.
 /// </summary>
 /// <param name="schema">The schema dependency.</param>
 public SchemaOrPropertyList(JsonSchema schema)
 {
     Schema = schema;
 }