Esempio n. 1
0
        public static KindSchema FromKindDefinition(ResourceDefinition resourceDefinition)
        {
            var reader         = new OpenApiStringReader();
            var metadataSchema = reader.ReadFragment <OpenApiSchema>(resourceDefinition["metadataSchema"]?.GetString() ?? throw new MissingResourceFieldException("metadataSchema"),
                                                                     Microsoft.OpenApi.OpenApiSpecVersion.OpenApi3_0, out var _);

            var definitionSchema = reader.ReadFragment <OpenApiSchema>(resourceDefinition["definitionSchema"]?.GetString() ?? throw new MissingResourceFieldException("definitionSchema"),
                                                                       Microsoft.OpenApi.OpenApiSpecVersion.OpenApi3_0, out var _);

            var specSchema  = default(OpenApiSchema);
            var stateSchema = default(OpenApiSchema);

            if (resourceDefinition["specSchema"] != null)
            {
                specSchema = reader.ReadFragment <OpenApiSchema>(resourceDefinition["specSchema"].GetString(),
                                                                 Microsoft.OpenApi.OpenApiSpecVersion.OpenApi3_0, out var _);
            }

            if (resourceDefinition["stateSchema"] != null)
            {
                stateSchema = reader.ReadFragment <OpenApiSchema>(resourceDefinition["stateSchema"].GetString(),
                                                                  Microsoft.OpenApi.OpenApiSpecVersion.OpenApi3_0, out var _);
            }

            return(new KindSchema(metadataSchema, definitionSchema, specSchema, stateSchema));
        }
Esempio n. 2
0
        public void ParsePrimitiveStringSchemaFragmentShouldSucceed()
        {
            var input      = @"
{ ""type"": ""integer"",
""format"": ""int64"",
""default"": 88
}
";
            var reader     = new OpenApiStringReader();
            var diagnostic = new OpenApiDiagnostic();

            // Act
            var schema = reader.ReadFragment <OpenApiSchema>(input, OpenApiSpecVersion.OpenApi3_0, out diagnostic);

            // Assert
            diagnostic.ShouldBeEquivalentTo(new OpenApiDiagnostic());

            schema.ShouldBeEquivalentTo(
                new OpenApiSchema
            {
                Type    = "integer",
                Format  = "int64",
                Default = new OpenApiInteger(88)
            });
        }
Esempio n. 3
0
        public void ParseExampleStringFragmentShouldSucceed()
        {
            var input      = @"
{ 
  ""foo"": ""bar"",
  ""baz"": [ 1,2]
}";
            var reader     = new OpenApiStringReader();
            var diagnostic = new OpenApiDiagnostic();

            // Act
            var openApiAny = reader.ReadFragment <IOpenApiAny>(input, OpenApiSpecVersion.OpenApi3_0, out diagnostic);

            // Assert
            diagnostic.ShouldBeEquivalentTo(new OpenApiDiagnostic());

            openApiAny.ShouldBeEquivalentTo(
                new OpenApiObject
            {
                ["foo"] = new OpenApiString("bar"),
                ["baz"] = new OpenApiArray()
                {
                    new OpenApiInteger(1),
                    new OpenApiInteger(2)
                }
            });
        }
Esempio n. 4
0
        public void GivenTheSchema(string schema)
        {
            var           reader        = new OpenApiStringReader();
            OpenApiSchema openApiSchema = reader.ReadFragment <OpenApiSchema>(schema, OpenApiSpecVersion.OpenApi3_0, out OpenApiDiagnostic diagnostic);

            if (diagnostic.Errors.Count > 0)
            {
                throw new ArgumentException("Invalid schema", nameof(schema));
            }

            this.scenarioContext.Set(openApiSchema);
        }
Esempio n. 5
0
        /// <summary>
        /// Converts to <see cref="GenerationContext"/>.
        /// </summary>
        /// <returns><see cref="GenerationContext"/></returns>
        public GenerationContext ToGenerationContext()
        {
            var generationContext = new GenerationContext
            {
                CrefToFieldValueMap = this.CrefToFieldValueMap
            };

            foreach (var key in this.CrefToSchemaMap.Keys)
            {
                InternalSchemaGenerationInfo schemaInfo = this.CrefToSchemaMap[key];
                var internalSchemaInfo = new SchemaGenerationInfo
                {
                    Schema = schemaInfo.Schema == null
                        ? null
                        : openApiStringReader.ReadFragment <OpenApiSchema>(
                        schemaInfo.Schema,
                        OpenApiSpecVersion.OpenApi3_0,
                        out OpenApiDiagnostic diagnostic),
                    Error = schemaInfo.Error
                };

                generationContext.CrefToSchemaMap.Add(key, internalSchemaInfo);
            }

            foreach (var key in this.VariantSchemaReferenceMap.Keys)
            {
                var documentVariantInfo = JsonConvert.DeserializeObject <DocumentVariantInfo>(key);
                Dictionary <string, OpenApiSchema> schemaCrefMap = this.VariantSchemaReferenceMap[key].ToDictionary(
                    k => k.Key, k => openApiStringReader.ReadFragment <OpenApiSchema>(
                        k.Value,
                        OpenApiSpecVersion.OpenApi3_0,
                        out OpenApiDiagnostic diagnostic));

                generationContext.VariantSchemaReferenceMap.Add(documentVariantInfo, schemaCrefMap);
            }

            return(generationContext);
        }
    }
Esempio n. 6
0
        public void ParseEnumFragmentShouldSucceed()
        {
            var input      = @"
[ 
  ""foo"",
  ""baz""
]";
            var reader     = new OpenApiStringReader();
            var diagnostic = new OpenApiDiagnostic();

            // Act
            var openApiAny = reader.ReadFragment <IOpenApiAny>(input, OpenApiSpecVersion.OpenApi3_0, out diagnostic);

            // Assert
            diagnostic.ShouldBeEquivalentTo(new OpenApiDiagnostic());

            openApiAny.ShouldBeEquivalentTo(
                new OpenApiArray
            {
                new OpenApiString("foo"),
                new OpenApiString("baz")
            });
        }
Esempio n. 7
0
        public void ParsePathFragmentShouldSucceed()
        {
            var input      = @"
summary: externally referenced path item
get:
  responses:
    '200':
      description: Ok
";
            var reader     = new OpenApiStringReader();
            var diagnostic = new OpenApiDiagnostic();

            // Act
            var openApiAny = reader.ReadFragment <OpenApiPathItem>(input, OpenApiSpecVersion.OpenApi3_0, out diagnostic);

            // Assert
            diagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic());

            openApiAny.Should().BeEquivalentTo(
                new OpenApiPathItem
            {
                Summary    = "externally referenced path item",
                Operations = new Dictionary <OperationType, OpenApiOperation>
                {
                    [OperationType.Get] = new OpenApiOperation()
                    {
                        Responses = new OpenApiResponses
                        {
                            ["200"] = new OpenApiResponse
                            {
                                Description = "Ok"
                            }
                        }
                    }
                }
            });
        }