Skip to content

Commit c699250

Browse files
authored
Fix setting cosmos configuration at runtime by checking whether the graphql schema is set (#1739)
## 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.
1 parent 9b0304e commit c699250

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
@@ -128,17 +128,22 @@ public static void ValidateDatabaseType(
128128
HttpStatusCode.ServiceUnavailable,
129129
DataApiBuilderException.SubStatusCodes.ErrorInInitialization);
130130

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

139-
if (!fileSystem.File.Exists(cosmosDbNoSql.Schema))
140-
{
141-
throw new FileNotFoundException($"The GraphQL schema file at '{cosmosDbNoSql.Schema}' could not be found. Ensure that it is a path relative to the runtime.");
143+
if (!fileSystem.File.Exists(cosmosDbNoSql.Schema))
144+
{
145+
throw new FileNotFoundException($"The GraphQL schema file at '{cosmosDbNoSql.Schema}' could not be found. Ensure that it is a path relative to the runtime.");
146+
}
142147
}
143148
}
144149
}

src/Service.Tests/Configuration/ConfigurationTests.cs

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

19481948
private static ConfigurationPostParameters GetCosmosConfigurationParameters()
19491949
{
1950-
string cosmosFile = $"{CONFIGFILE_NAME}.{COSMOS_ENVIRONMENT}{CONFIG_EXTENSION}";
1950+
RuntimeConfig configuration = ReadCosmosConfigurationFromFile();
19511951
return new(
1952-
File.ReadAllText(cosmosFile),
1952+
configuration.ToJson(),
19531953
File.ReadAllText("schema.gql"),
19541954
$"AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==;Database={COSMOS_DATABASE_NAME}",
19551955
AccessToken: null);
19561956
}
19571957

19581958
private static ConfigurationPostParametersV2 GetCosmosConfigurationParametersV2()
19591959
{
1960-
string cosmosFile = $"{CONFIGFILE_NAME}.{COSMOS_ENVIRONMENT}{CONFIG_EXTENSION}";
1960+
RuntimeConfig configuration = ReadCosmosConfigurationFromFile();
19611961
RuntimeConfig overrides = new(
19621962
null,
19631963
new DataSource(DatabaseType.CosmosDB_NoSQL, $"AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==;Database={COSMOS_DATABASE_NAME}", new()),
19641964
null,
19651965
null);
19661966

19671967
return new(
1968-
File.ReadAllText(cosmosFile),
1968+
configuration.ToJson(),
19691969
overrides.ToJson(),
19701970
File.ReadAllText("schema.gql"),
19711971
AccessToken: null);
19721972
}
19731973

1974+
private static RuntimeConfig ReadCosmosConfigurationFromFile()
1975+
{
1976+
string cosmosFile = $"{CONFIGFILE_NAME}.{COSMOS_ENVIRONMENT}{CONFIG_EXTENSION}";
1977+
1978+
string configurationFileContents = File.ReadAllText(cosmosFile);
1979+
if (!RuntimeConfigLoader.TryParseConfig(configurationFileContents, out RuntimeConfig config))
1980+
{
1981+
throw new Exception("Failed to parse configuration file.");
1982+
}
1983+
1984+
// The Schema file isn't provided in the configuration file when going through the configuration endpoint so we're removing it.
1985+
config.DataSource.Options.Remove("Schema");
1986+
return config;
1987+
}
1988+
19741989
/// <summary>
19751990
/// Helper used to create the post-startup configuration payload sent to configuration controller.
19761991
/// Adds entity used to hydrate authorization resolver post-startup and validate that hydration succeeds.

0 commit comments

Comments
 (0)