spring-attic / spring-cloud-gcp

Integration for Google Cloud Platform APIs with Spring
Apache License 2.0
705 stars 693 forks source link

NullPointerException when a configuration is empty #2459

Closed pijpijpij closed 4 years ago

pijpijpij commented 4 years ago

Describe the bug At start-up, with GCP config enabled, the GCP code requires a configuration (myapp_myprofile). It is probably a design decision to require the existence of the configuration in GCP Config. Fair enough. In that particular myprofile profile, myapp does not need any extra property, so I did not create one. That results in an NPE when your framework retrieves the configuration.

As a workaround, I created a property named placeholder with a value of boff!. My app now starts.

Sample I don't think there's a need for that as the code is pretty clear in version 1.2.2.RELEASE which I am using. The NPE occurs on line 50 of GoogleConfigEnvironment. Either variables hadn't been set or it had been set to null. Defensive coding here would address the problem (use getVariables() and have it return an empty list instead of null). Should you also want to address the value returned by the API, I am including the stack trace here:

2020-07-11 10:26:02 ERROR o.s.b.SpringApplication:826 - Application run failed java.lang.RuntimeException: Error loading configuration for firmwareupdate-9d157/nurvv-api_firmware at org.springframework.cloud.gcp.autoconfigure.config.GoogleConfigPropertySourceLocator.locate(GoogleConfigPropertySourceLocator.ja va:146) at org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.initialize(PropertySourceBootstrapConfiguration. java:98) at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:626) at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:370) at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:152) at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitiali zer.java:132) at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:92) at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:172) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5135) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:717) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:690) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:705) at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:978) at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1849) at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:118) at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:773) at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:427) at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1576) at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:309) at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123) at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:423) at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:366) at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:936) at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:841) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:140) at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909) at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.core.StandardService.startInternal(StandardService.java:421) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:930) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.startup.Catalina.start(Catalina.java:633) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:343) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:478) Caused by: java.lang.NullPointerException: null at org.springframework.cloud.gcp.autoconfigure.config.GoogleConfigEnvironment.getConfig(GoogleConfigEnvironment.java:50) at org.springframework.cloud.gcp.autoconfigure.config.GoogleConfigPropertySourceLocator.locate(GoogleConfigPropertySourceLocator.ja va:141) ... 48 common frames omitted

meltsufin commented 4 years ago

@pijpijpij Thanks for the bug report. The fix seems pretty simple as you've suggested. Contributions are welcomed!

pijpijpij commented 4 years ago

Thanks @kioie for stepping in. :)