Skip to content

Commit 73a02dc

Browse files
seantleonardMathos1432Aniruddh25
authored
[Cherrypick] Fix setting cosmos configuration at runtime by checking whether the graphql schema is set (#1746)
## Context Cherry-picking #1739 from `main` to `release/0.9` which includes the merge conflicts resolved for merging into main. Remainder of description copied from #1739 PR description. ## Why make this change? Failing to set the configuration at runtime if there is not "schema" file set in the config. This is not required when setting the config at runtime since the graphql schema is set already. ## How was this tested? - [x] Validated locally - [x] Updated the tests so the schema property is removed from the config. Verified that the tests were failing before the fix and passing after. Co-authored-by: Mathieu Tremblay <[email protected]> Co-authored-by: Aniruddh Munde <[email protected]>
1 parent 7c4bac2 commit 73a02dc

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

src/Core/Configurations/RuntimeConfigValidator.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,22 @@ public static void ValidateDatabaseType(
133133
HttpStatusCode.ServiceUnavailable,
134134
DataApiBuilderException.SubStatusCodes.ErrorInInitialization);
135135

136-
if (string.IsNullOrEmpty(cosmosDbNoSql.Schema))
136+
// The schema is provided through GraphQLSchema and not the Schema file when the configuration
137+
// is received after startup.
138+
if (string.IsNullOrEmpty(cosmosDbNoSql.GraphQLSchema))
137139
{
138-
throw new DataApiBuilderException(
139-
"No GraphQL schema file has been provided for CosmosDB_NoSql. Ensure you provide a GraphQL schema containing the GraphQL object types to expose.",
140-
HttpStatusCode.ServiceUnavailable,
141-
DataApiBuilderException.SubStatusCodes.ErrorInInitialization);
142-
}
140+
if (string.IsNullOrEmpty(cosmosDbNoSql.Schema))
141+
{
142+
throw new DataApiBuilderException(
143+
"No GraphQL schema file has been provided for CosmosDB_NoSql. Ensure you provide a GraphQL schema containing the GraphQL object types to expose.",
144+
HttpStatusCode.ServiceUnavailable,
145+
DataApiBuilderException.SubStatusCodes.ErrorInInitialization);
146+
}
143147

144-
if (!fileSystem.File.Exists(cosmosDbNoSql.Schema))
145-
{
146-
throw new FileNotFoundException($"The GraphQL schema file at '{cosmosDbNoSql.Schema}' could not be found. Ensure that it is a path relative to the runtime.");
148+
if (!fileSystem.File.Exists(cosmosDbNoSql.Schema))
149+
{
150+
throw new FileNotFoundException($"The GraphQL schema file at '{cosmosDbNoSql.Schema}' could not be found. Ensure that it is a path relative to the runtime.");
151+
}
147152
}
148153
}
149154
}

src/Service.Tests/Configuration/ConfigurationTests.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2305,30 +2305,45 @@ private static string GenerateMockJwtToken()
23052305

23062306
private static ConfigurationPostParameters GetCosmosConfigurationParameters()
23072307
{
2308-
string cosmosFile = $"{CONFIGFILE_NAME}.{COSMOS_ENVIRONMENT}{CONFIG_EXTENSION}";
2308+
RuntimeConfig configuration = ReadCosmosConfigurationFromFile();
23092309
return new(
2310-
File.ReadAllText(cosmosFile),
2310+
configuration.ToJson(),
23112311
File.ReadAllText("schema.gql"),
23122312
$"AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==;Database={COSMOS_DATABASE_NAME}",
23132313
AccessToken: null);
23142314
}
23152315

23162316
private static ConfigurationPostParametersV2 GetCosmosConfigurationParametersV2()
23172317
{
2318-
string cosmosFile = $"{CONFIGFILE_NAME}.{COSMOS_ENVIRONMENT}{CONFIG_EXTENSION}";
2318+
RuntimeConfig configuration = ReadCosmosConfigurationFromFile();
23192319
RuntimeConfig overrides = new(
23202320
Schema: null,
23212321
DataSource: new DataSource(DatabaseType.CosmosDB_NoSQL, $"AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==;Database={COSMOS_DATABASE_NAME}", new()),
23222322
Runtime: null,
23232323
Entities: new(new Dictionary<string, Entity>()));
23242324

23252325
return new(
2326-
File.ReadAllText(cosmosFile),
2326+
configuration.ToJson(),
23272327
overrides.ToJson(),
23282328
File.ReadAllText("schema.gql"),
23292329
AccessToken: null);
23302330
}
23312331

2332+
private static RuntimeConfig ReadCosmosConfigurationFromFile()
2333+
{
2334+
string cosmosFile = $"{CONFIGFILE_NAME}.{COSMOS_ENVIRONMENT}{CONFIG_EXTENSION}";
2335+
2336+
string configurationFileContents = File.ReadAllText(cosmosFile);
2337+
if (!RuntimeConfigLoader.TryParseConfig(configurationFileContents, out RuntimeConfig config))
2338+
{
2339+
throw new Exception("Failed to parse configuration file.");
2340+
}
2341+
2342+
// The Schema file isn't provided in the configuration file when going through the configuration endpoint so we're removing it.
2343+
config.DataSource.Options.Remove("Schema");
2344+
return config;
2345+
}
2346+
23322347
/// <summary>
23332348
/// Helper used to create the post-startup configuration payload sent to configuration controller.
23342349
/// Adds entity used to hydrate authorization resolver post-startup and validate that hydration succeeds.

0 commit comments

Comments
 (0)